我想將DataFrame中的DatetimeIndex轉換爲浮點格式,可以在我的模型中進行分析。有人告訴我該怎麼做嗎?我需要使用date2num()函數嗎? 非常感謝!將Pandas DatetimeIndex轉換爲數字格式
回答
轉換爲Timedelta
和dt.total_seconds
提取總秒數:
data = \
{'date': {0: pd.Timestamp('2013-01-01 00:00:00'),
1: pd.Timestamp('2013-01-02 00:00:00'),
2: pd.Timestamp('2013-01-03 00:00:00'),
3: pd.Timestamp('2013-01-04 00:00:00'),
4: pd.Timestamp('2013-01-05 00:00:00'),
5: pd.Timestamp('2013-01-06 00:00:00'),
6: pd.Timestamp('2013-01-07 00:00:00'),
7: pd.Timestamp('2013-01-08 00:00:00'),
8: pd.Timestamp('2013-01-09 00:00:00'),
9: pd.Timestamp('2013-01-10 00:00:00')}}
df = pd.DataFrame.from_dict(data)
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
類型:
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
使用astype浮動也就是說,如果你有一個像
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')
一個數據幀然後
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)
請注意自2017年以來的秒數爲10e9,因此10e17不正確。請參閱https://stackoverflow.com/a/46502880/4909087並運行https://stackoverflow.com/questions/4548684/how-to-get-the-seconds-since-epoch-from-the-time-date- gmtime-in-py的輸出 –
但是,當您將其轉換回pd.to_datetime時,返回原始日期na – Dark
是的,但是我認爲OP想要使用曆元時間。我不知道astype給出了什麼,但它看起來像一個錯誤?這絕對不是時代。 –
- 1. 將Pandas DateTimeIndex轉換爲YYYYMMDD整數?
- 2. 如何將Pandas DatetimeIndex轉換爲相應的字符串
- 3. 將PeriodIndex轉換爲DateTimeIndex?
- 4. 熊貓DatetimeIndex字符串格式轉換爲歐洲
- 5. 轉換datetimeindex到QX-YY格式
- 6. 將時間格式轉換爲Pandas中有十進制數
- 7. 將貨幣格式轉換爲數字
- 8. 如何將C++數字格式轉換爲C#數字格式?
- 9. 將密文從數字格式轉換爲字母格式
- 10. 將pandas轉換爲pyspark表達式
- 11. 在XSLT中將指數格式轉換爲數字格式
- 12. Python Pandas將字符串轉換爲NaN
- 13. Python將Pandas Float轉換爲字符串
- 14. 將字典列表轉換爲Pandas Dataframe
- 15. Pandas Series.map()函數將非數字值轉換爲數字嗎?
- 16. 將字符串轉換爲格式爲
- 17. 將Pandas Dataframe轉換爲RCV
- 18. 將API轉換爲Pandas DataFrame
- 19. 將pandas DataFrame索引轉換爲時間戳格式
- 20. 將日期時間格式轉換爲Unix時間戳Pandas
- 21. 將Pandas DataFrame轉換爲LIBFM格式的txt文件
- 22. 將Excel的DateTime格式轉換爲DateTime Pandas
- 23. Python Pandas - Dataframe專欄 - 將FY格式'2015/2016'轉換爲'15/16'
- 24. 將文本格式轉換爲數字格式
- 25. 將數字格式轉換爲matlab中的時間格式
- 26. 將德國格式數字轉換爲國際格式
- 27. 將Pandas列轉換爲數據幀
- 28. 將字符串數字(字格式)轉換爲整數ruby
- 29. 將字母數字字符串轉換爲整數格式
- 30. 將Python字典轉換爲JSON格式
嘗試df.date.values.astype(float)一次 – Dark
@Bharathshetty'不能從類似datetimelike [datetime64 [ns]]到[float64]' –
它爲我工作。 – Dark