【发布时间】:2016-10-31 04:22:51
【问题描述】:
我通过 psycopg2 模块从 Postgre SQL 查询中获取表。该表看起来像:
ct i v
1 3429-03-19 20:00:00 1 45
2 3433-02-10 21:00:00 1 65
3 3433-02-10 22:00:00 2 44
然后我将其转换为数据框。我需要根据 ct 列中的日期(datetime.date)将 i 和 v 下采样到每天。然后旋转数据框,使日期成为索引,列成为 i 和 v 的每日中位数。
table=cur.fetchall() #resulting table from psycopg2 (the SQL query)
col = ['Date', 'Daily i', 'Daily v']
pt_df = pd.DataFrame (rows, columns=col)
pt_df2 = pt_df.set_index(['Date'])
pt_df2
我尝试将日期设置为索引列,但出现 OutOfBoundsDatetime 错误:超出范围纳秒时间戳:3429-03-19 20:00:00。我怎样才能解决这个问题?我尝试将 ct 列转换为 pd.to_datetime。但这也导致了错误。
我尝试像这样旋转 df:
df = rows
pt_df2 = pd.pivot_table(df, index=["Date"], columns = col, aggfunc=[np.median(df, axis=1, 'i'), np.median(df, axis=1, 'v')])
pt_df2
但我也收到了一个错误。我真的对此感到震惊。
【问题讨论】:
-
你真的在处理公历 3429 年的数据吗????
-
是的,所有年份都在 3000 到 3600 之间
-
那么不要使用纳秒精度,因为对于千年后将发生的事情来说,纳秒精度是没有意义的
标签: python postgresql pandas dataframe psycopg2