Convert object to DateRange

Posted by user655832 on Stack Overflow See other posts from Stack Overflow or by user655832
Published on 2012-07-05T03:10:24Z Indexed on 2012/07/05 3:15 UTC
Read the original article Hit count: 260

Filed under:
|
|

I'm querying an underlying PostgreSQL database using Pandas 0.8. Pandas is returning the DataFrame properly but the underlying timestamp column in my database is being returned as a generic "object" type in Pandas. As I would eventually like to seasonal normalization of my data I am curious as to how to convert this generic "object" column to something that is appropriate for analysis.

Here is my current code to retrieve the data: # get records from db example import pandas.io.sql as psql import psycopg2

# define query to get all subs created this year
QRY = """
    select 
        i i, 
        i * random() f,
        case when random() > 0.5 
        then 
            true 
        else 
            false 
        end t, 
        (current_date - (i*random())::int)::timestamp with time zone tsz 
    from 
        generate_series(1,1000) as s(i)
    order by
        4
    ;
"""
CONN_STRING = "host='localhost' port=5432 dbname='postgres' user='postgres'"

# connect to db
conn = psycopg2.connect(CONN_STRING)

# get some data set index on relid column
df = psql.frame_query(QRY, con=conn)

print "Row count retrieved: %i" % (len(df),)

Thanks for any help you can render.

M

© Stack Overflow or respective owner

Related posts about python

Related posts about numpy