格式化pandas.Timestamp for SQLite查询

时间:2014-05-09 04:56:23

标签: sql sqlite pandas timestamp

from pandas.io import sql
import sqlalchemy

eng = sqlalchemy.create_engine('sqlite:////path/to/file.db')

In [20]: sql.execute("select * from mytable", eng).fetchone()
Out[20]: (0, u'2013-03-13 13:05:00.000000', 1, 0, 0, 0)

现在我该怎么做才能让它发挥作用:

target = Timestamp('2014-04-30')
sql.execute("select * from mytable where date = ?", eng, params=target).\
            fetchone()

2 个答案:

答案 0 :(得分:2)

str_format = '%Y-%m-%d %H:%M:%s'
target = Timestamp('2014-04-30').strftime(str_format)

答案 1 :(得分:2)

答案中有一个拼写错误,最后一个%s(小写)应该是%S(大写)

>>> str_format = '%Y-%m-%d %H:%M:%s'
>>> target = pd.Timestamp('2014-04-30').strftime(str_format)
>>> target
'2014-04-30 00:00:1398808800'  

>>> str_format = '%Y-%m-%d %H:%M:%S'
>>> target = pd.Timestamp('2014-04-30').strftime(str_format)
>>> target
'2014-04-30 00:00:00'