简体   繁体   中英

Convert object to DateRange

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

Here is my current code to retrieve the data:

# get timestamp with time zone Pandas example
import pandas.io.sql as psql
import psycopg2

# define query
QRY = """
    select 
        i i, 
        i * random() f,
        case when random() > 0.5 
        then 
            true 
        else 
            false 
        end b, 
        (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),)

Result in Python:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 1000 entries, 0 to 999
Data columns:
i      1000  non-null values
f      1000  non-null values
b      1000  non-null values
tsz    1000  non-null values
dtypes: bool(1), float64(1), int64(1), object(1)

Interesting to note that the first column, "i", is an Integer col in PG. I'm not sure why Pandas thinks this is a "bool" type column. My real issue though is the "object" column which I think needs to be of some type of timestamp.

The dtypes listed are in alphabetical order. The tsz column probably contains datetime.datetime Python objects (what is usually returned by database drivers for timestamp columns), have you looked?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM