本文介绍了将pandas.tslib.Timestamp转换为datetime python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个df
时间序列.我提取了索引,并希望将它们分别转换为datetime
.您如何去做呢?我尝试使用pandas.to_datetime(x)
,但在使用type()
I have a df
time series. I extracted the indexes and want to convert them each to datetime
. How do you go about doing that? I tried to use pandas.to_datetime(x)
but it doesn't convert it when I check after using type()
推荐答案
只需尝试 to_datetime()
>>> import pandas as pd
>>> t = pd.tslib.Timestamp('2016-03-03 00:00:00')
>>> type(t)
pandas.tslib.Timestamp
>>> t.to_datetime()
datetime.datetime(2016, 3, 3, 0, 0)
>>> t.to_pydatetime()
datetime.datetime(2016, 3, 3, 0, 0)
更改为datetime.date
类型
>>> t.date()
datetime.date(2016, 3, 3)
更新
谢谢@@ a =="https://stackoverflow.com/users/5402386/mjp"> mjp ,to_datetime()
将来会被弃用,请改用to_pydatetime()
!
update
Thanks, @mjp, to_datetime()
will be deprecated in the future, use to_pydatetime()
instead!
In [4]: t.to_datetime()
/Users/qiuwei/Library/Python/2.7/lib/python/site-packages/IPython/core/interactiveshell.py:2881: FutureWarning: to_datetime is deprecated. Use self.to_pydatetime()
exec(code_obj, self.user_global_ns, self.user_ns)
Out[4]: datetime.datetime(2016, 3, 3, 0, 0)
这篇关于将pandas.tslib.Timestamp转换为datetime python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!