[英]pandas to sql turns datetime64 index to time zone aware
我有一个pandas
Dataframe
,其索引类型为datetime64[ns]
。 当我使用to_sql
方法时,我希望将索引创建为timestamp
postgres列,但是它将创建一个timestamptz
列。
为什么会这样呢?
请注意,当datetime64[ns]
列不是索引而是普通列时,则不会发生。
熊猫0.23.4,psycopg2 2.7.4,sqlalchemy 1.2.7,PostgreSQL 9.6.6
例
dates = pd.date_range('2018-01-01', periods=5, freq='6h')
df_test = pd.DataFrame({'nums': range(5)}, index=dates)
nums
2018-01-01 00:00:00 0
2018-01-01 06:00:00 1
2018-01-01 12:00:00 2
插入到postgres
df_test.to_sql('foo_table',postgres_uri,schema='data_test',index_label='info_date')
从数据库中读取时,我得到不同类型的索引
df_db = pd.read_sql_table('foo_table',postgres_uri,schema='data_test',index_col='info_date')
nums
info_date
2018-01-01 00:00:00+00:00 0
2018-01-01 06:00:00+00:00 1
2018-01-01 12:00:00+00:00 2
如您所见, info_date
的类型为datetime64[ns, UTC]
我使用mysql,但是如果您设置index = True呢?
我总是使用此代码写入我的数据库:
df_test.to_sql(name='database', chunksize=20000, con=conn, index=True, if_exists='replace')
所以也许这会工作:
df_test.to_sql('foo_table',postgres_uri,schema='data_test',index=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.