我有一个MySql表,其时间戳分辨率为微秒:
+----------------------------+------+
| time | seq |
+----------------------------+------+
| 2015-06-19 02:17:57.389509 | 0 |
| 2015-06-19 02:17:57.934171 | 10 |
+----------------------------+------+
我想把它读成一个pandas Dataframe。使用
import pandas as pd
con = get_connection()
result = pd.read_sql("SELECT * FROM MyTable;", con=con)
print result
返回NaT(不是时间):
time seq
0 NaT 0
1 NaT 10
如何将其读入时间戳?
答案 0 :(得分:8)
通常,要转换时间戳,您可以使用pandas.to_datetime()
。
>>> import pandas as pd
>>> pd.to_datetime('2015-06-19 02:17:57.389509')
Timestamp('2015-06-19 02:17:57.389509')
从docs开始,从SQL读入时,您可以显式强制将列解析为日期:
pd.read_sql_table('data', engine, parse_dates=['Date'])
或更明确地指定格式字符串或要传递给pandas.to_datetime()
的参数的字典:
pd.read_sql_table('data', engine, parse_dates={'Date': '%Y-%m-%d'})
或
pd.read_sql_table('data', engine, parse_dates={'Date': {'format': '%Y-%m-%d %H:%M:%S'}})
添加快速概念证明。注意,我正在使用SQLITE
。假设您将时间戳存储为字符串:
from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData
import pandas as pd
engine = create_engine('sqlite:///:memory:', echo=True)
datapoints = [{'ts': '2015-06-19 02:17:57.389509', 'seq': 0},
{'ts':'2015-06-19 02:17:57.934171', 'seq': 10}]
metadata = MetaData()
mydata = Table('mydata', metadata,
Column('ts', String),
Column('seq', Integer),
)
metadata.create_all(engine)
ins = mydata.insert()
conn = engine.connect()
conn.execute(ins, datapoints)
foo = pd.read_sql_table('mydata', engine, parse_dates=['ts'])
print(foo)
输出:
ts seq
0 2015-06-19 02:17:57.389509 0
1 2015-06-19 02:17:57.934171 10
或者,如果您将它们存储为datetime对象,它的工作原理相同(代码差异是我将数据以日期时间格式存入数据库):
from datetime import datetime
from sqlalchemy import create_engine, Table, Column, Integer, DateTime, MetaData
import pandas as pd
engine = create_engine('sqlite:///:memory:', echo=True)
datapoints = [{'ts': datetime.strptime('2015-06-19 02:17:57.389509', '%Y-%m-%d %H:%M:%S.%f'), 'seq': 0},
{'ts':datetime.strptime('2015-06-19 02:17:57.934171', '%Y-%m-%d %H:%M:%S.%f'), 'seq': 10}]
metadata = MetaData()
mydata = Table('mydata', metadata,
Column('ts', DateTime),
Column('seq', Integer),
)
metadata.create_all(engine)
ins = mydata.insert()
conn = engine.connect()
conn.execute(ins, datapoints)
foo = pd.read_sql_table('mydata', engine, parse_dates=['ts'])
print(foo)
输出相同的内容:
ts seq
0 2015-06-19 02:17:57.389509 0
1 2015-06-19 02:17:57.934171 10
希望这有帮助。
编辑 要尝试解决@joris的问题,sqlite
存储所有datetime
对象作为字符串,但是内置适配器在获取时会自动将这些对象转换回datetime
个对象。使用以下内容扩展第二个示例:
from sqlalchemy.sql import select
s = select([mydata])
res = conn.execute(s)
row = res.fetchone()
print(type(row['ts']))
结果为<class 'datetime.datetime'>