我有以下格式时间的示例 CSV 数据
2014-04-29 00:00:01.933000,2014-04-29 00:00:01.933000
我可以执行pd.to_datetime(数据[' ts'])以及pd.DatetimeIndex(数据[' ts'])
但如何将输出格式化为%Y-%m-%d%H:%M:%S 或基本上要删除纳秒。
这个简单的日期格式似乎错过了所有文档,我在熊猫中读过。我知道另一种方法是转换为浮动
np.floor(pd.DatetimeIndex(pd.to_datetime(sdfeed1['ts'])).astype(int64)/1e9)
我不喜欢这种丑陋的做法。有没有简单的方法将日期时间数组转换为简单格式。就像excel一样,不使用任何lambda函数。
pd.convert(...日期列表......,格式=' ....'),应该就这么简单!我问得太多了吗?为此推动np和lambda函数是过度的。
答案 0 :(得分:1)
使用dateutil.parser
然后格式化日期时间对象
>>> from dateutil.parser import parse
>>> raw_text = '2014-04-29 00:00:01.933000 2014-04-29 00:00:01.933000 2014-04-29 00:00:02.308000 2014-04-29 00:00:02.308000 2014-04-29 00:00:03.308000 2014-04-29 00:00:03.308000 2014-04-29 00:00:06.792000 2014-04-29 00:00:06.792000 2014-04-29 00:00:07.480000 2014-04-29 00:00:07.480000 2014-04-29 00:00:08.355000 2014-04-29 00:00:08.355000 2014-04-29 00:00:15.027000 2014-04-29 00:00:15.027000 2014-04-29 00:00:15.917000 2014-04-29 00:00:15.917000 2014-04-29 00:00:22.558000 2014-04-29 00:00:22.558000'
>>> dates = raw_text.split()
>>> date_parts = 2
>>> for idx in xrange(0,len(dates),date_parts):
... text = ' '.join(dates[idx:idx+date_parts])
... dt = parse(text)
... dt.strftime("%Y-%m-%d %H:%M:%S")
...
'2014-04-29 00:00:01'
'2014-04-29 00:00:01'
'2014-04-29 00:00:02'
'2014-04-29 00:00:02'
'2014-04-29 00:00:03'
'2014-04-29 00:00:03'
'2014-04-29 00:00:06'
'2014-04-29 00:00:06'
'2014-04-29 00:00:07'
'2014-04-29 00:00:07'
'2014-04-29 00:00:08'
'2014-04-29 00:00:08'
'2014-04-29 00:00:15'
'2014-04-29 00:00:15'
'2014-04-29 00:00:15'
'2014-04-29 00:00:15'
'2014-04-29 00:00:22'
'2014-04-29 00:00:22'
注意:dateutil.parser
解析日期字符串而不指定模式!!
>>> text = '2014/04/29 00:00:01.933000'
>>> dt = parse(text)
>>> dt.strftime("%Y-%m-%d %H:%M:%S")
'2014-04-29 00:00:01'
>>>
>>> text = '29/4/14 00:00:01.933000'
>>> dt = parse(text)
>>> dt.strftime("%Y-%m-%d %H:%M:%S")
'2014-04-29 00:00:01'
答案 1 :(得分:0)
您可以尝试这样:
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
将结果输出为:
'2014-09-05 14:41:26'
答案 2 :(得分:0)
hibernate.hbm2ddl.auto
这会将datetime64 [ns]中的纳秒设置为0。最终结果仍然是datetime64 [ns],但是当调用astype()时,纳秒将被删除。
答案 3 :(得分:0)
这个怎么样。
pd.Timestamp("now").round('s')