将 Pandas DatetimeIndex 转换为数字格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46501626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting Pandas DatetimeIndex to a numeric format
提问by Nick Gao
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!
我想将我的 DataFrame 中的 DatetimeIndex 转换为浮点格式,可以在我的模型中进行分析。有人可以告诉我该怎么做吗?我需要使用 date2num() 函数吗?非常感谢!
回答by cs95
Convert to Timedelta
and extract the total seconds from dt.total_seconds
:
转换为Timedelta
并从中提取总秒数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
Or, maybe, the data would be more useful presented as an int
type:
或者,也许,将数据作为一种int
类型表示会更有用:
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
回答by Bharath
Use astype float i.e if you have a dataframe like
使用 astype float 即如果你有一个像
df = pd.DataFrame({'date': ['1998-03-01 00:00:01', '2001-04-01 00:00:01','1998-06-01 00:00:01','2001-08-01 00:00:01','2001-05-03 00:00:01','1994-03-01 00:00:01'] })
df['date'] = pd.to_datetime(df['date'])
df['x'] = list('abcdef')
df = df.set_index('date')
Then
然后
df.index.values.astype(float)
array([ 8.88710401e+17, 9.86083201e+17, 8.96659201e+17,
9.96624001e+17, 9.88848001e+17, 7.62480001e+17])
pd.to_datetime(df.index.values.astype(float))
DatetimeIndex(['1998-03-01 00:00:01', '2001-04-01 00:00:01',
'1998-06-01 00:00:01', '2001-08-01 00:00:01',
'2001-05-03 00:00:01', '1994-03-01 00:00:01'],
dtype='datetime64[ns]', freq=None)
回答by Colin Catlin
I believe this offers another solution, here assuming a dataframe with a DatetimeIndex.
我相信这提供了另一种解决方案,这里假设数据帧带有 DatetimeIndex。
pd.to_numeric(df.index, downcast='float')
# although normally I would prefer an integer, and to coerce errors to NaN
pd.to_numeric(df.index, errors = 'coerce',downcast='integer')
回答by Tomek Tajne
I found another solution:
我找到了另一个解决方案:
df['date'] = df['date'].astype('datetime64').astype(int).astype(float)