将Pandas DatetimeIndex转换为数字格式 [英] Converting Pandas DatetimeIndex to a numeric format
本文介绍了将Pandas DatetimeIndex转换为数字格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将DataFrame中的DatetimeIndex转换为float格式,可以在模型中对其进行分析.有人可以告诉我该怎么做吗?我需要使用date2num()函数吗? 非常感谢!
I want to convert the DatetimeIndex in my DataFrame to float format,which can be analysed in my model.Could someone tell me how to do it? Do I need to use date2num()function? Many thanks!
推荐答案
转换为Timedelta
并从dt.total_seconds
中提取总秒数:
Convert to Timedelta
and extract the total seconds from dt.total_seconds
:
df
date
0 2013-01-01
1 2013-01-02
2 2013-01-03
3 2013-01-04
4 2013-01-05
5 2013-01-06
6 2013-01-07
7 2013-01-08
8 2013-01-09
9 2013-01-10
pd.to_timedelta(df.date).dt.total_seconds()
0 1.356998e+09
1 1.357085e+09
2 1.357171e+09
3 1.357258e+09
4 1.357344e+09
5 1.357430e+09
6 1.357517e+09
7 1.357603e+09
8 1.357690e+09
9 1.357776e+09
Name: date, dtype: float64
或者,也许以int
类型表示的数据会更有用:
Or, maybe, the data would be more useful presented as an int
type:
pd.to_timedelta(df.date).dt.total_seconds().astype(int)
0 1356998400
1 1357084800
2 1357171200
3 1357257600
4 1357344000
5 1357430400
6 1357516800
7 1357603200
8 1357689600
9 1357776000
Name: date, dtype: int64
这篇关于将Pandas DatetimeIndex转换为数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文