数据帧包含dytpe为datetime64[s]
的索引.
df
ticker close
2008-01-28 aacg 0.7380
2008-01-29 aacg 0.6797
2008-01-30 aacg 0.6603
2008-01-31 aacg 0.7418
2008-02-01 aacg 0.7387
将df
写入表quote
:
df.to_sql('quote',con=engine,index=True)
在PostgreSQL中显示记录:
select * from quote;
index | ticker | close
---------------------+--------+--------
2008-01-28 00:00:00 | aacg | 0.738
2008-01-29 00:00:00 | aacg | 0.6797
2008-01-30 00:00:00 | aacg | 0.6603
2008-01-31 00:00:00 | aacg | 0.7418
2008-02-01 00:00:00 | aacg | 0.7387
显示表格 struct :
\d quote
Table "public.quote"
Column | Type | Collation | Nullable | Default
--------+-----------------------------+-----------+----------+---------
index | timestamp without time zone | | |
ticker | text | | |
close | double precision | | |
Indexes:
"ix_quote_index" btree (index)
怎样才能使记录写成如下格式和索引为date
的格式?
index | ticker | close
------------+--------+--------
2008-01-28 | aacg | 0.738
2008-01-29 | aacg | 0.6797
2008-01-30 | aacg | 0.6603
2008-01-31 | aacg | 0.7418
2008-02-01 | aacg | 0.7387
如果我用df.index.astype('str')
将df.index
转换为字符串,数据库中的index
类型将是text
,而to_sql
是意外的!