我正在寫一個程序從一個名爲quandl的網站下載數據。我遇到的問題是,當下載數據時,下載的數據幀的索引列的日期格式爲:2000-01-02 00:00:00。當我使用dates = df.index.values.tolist()
時,日期以946684800000000000(這是從前的日期)的整數形式返回。有誰知道如何處理這種日期格式,並把它放到一個格式,我可以使用日期時間模塊???python數據框中的日期時間索引
1
A
回答
3
如果df.index
是DatetimeIndex
,然後使用df.index.to_pydatetime()
到日期轉換爲的datetime.datetime
秒的對象陣列。 例如,
In [14]: index = pd.date_range('2000-1-1', periods=3, freq='D')
In [15]: index
Out[15]: DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03'], dtype='datetime64[ns]', freq='D', tz=None)
In [16]: index.values.tolist()
Out[16]: [946684800000000000L, 946771200000000000L, 946857600000000000L]
In [17]: index.to_pydatetime()
Out[20]:
array([datetime.datetime(2000, 1, 1, 0, 0),
datetime.datetime(2000, 1, 2, 0, 0),
datetime.datetime(2000, 1, 3, 0, 0)], dtype=object)
請注意,這取決於你想與這些日期做什麼,它可能是更有利的是用DatetimeIndex
比datetime.datetime
秒的對象數組工作。
提示:這是問題的類型 - 在IPython可以有很大的幫助 - 一個陌生的對象的自省。 IPython具有TAB完成屬性。在IPython的提示輸入
In [17]: index.
,然後按TAB拉起該IPython的已檢測到的所有屬性和index
對象的方法的列表。 (因爲有些對象具有自定義__getattr__
方法,所以這可能不是一個完整列表,但它通常很有用。)仔細閱讀列表或對「datetime」執行文本搜索將導致您登錄index.to_datetime
和index.to_pydatetime
。稍微的實驗會告訴你,index.to_pydatetime
做你需要的。此外,index.to_pydatetime
後輸入問號導致IPython的告訴你有用的信息,包括文檔字符串:
In [19]: index.to_pydatetime?
Type: instancemethod
String form: <bound method DatetimeIndex.to_pydatetime of DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03'], dtype='datetime64[ns]', freq='D', tz=None)>
File: /home/unutbu/.virtualenvs/dev/lib/python2.7/site-packages/pandas-0.16.2+175.g5a9a9da-py2.7-linux-x86_64.egg/pandas/tseries/index.py
Definition: index.to_pydatetime()
Docstring:
Return DatetimeIndex as object ndarray of datetime.datetime objects
Returns
-------
datetimes : ndarray
+0
這是完美的,非常感謝你 – Lererferler
相關問題
- 1. Python Dataframe.resample()從日期時間索引中刪除時間
- 2. MySQL索引日期時間
- 3. 在pandas-python中選擇基於日期時間索引的數據
- 4. 在熊貓數據框中設置多級索引更改日期時間
- 5. Python中的排序日期時間和返回索引錯誤
- 6. 使用日期時間索引的部分連接數據幀
- 7. 如何在Python中的數據框中將日期時間舍入爲十分鐘的日期時間
- 8. 從pandas數據框中的日期時間刪除時間戳
- 9. 使用日期時間索引的Python中的時間序列預測
- 10. 從python/django的日期時間列中檢索日期
- 11. 在日期時間索引上使用pandas切片數據幀
- 12. Python - 日期時間函數
- 13. python中的聚合數據框索引
- 14. oracle日期時間字段索引
- 15. R中日期之間的時間,分解成子數據框
- 16. 如何索引python中的日期列
- 17. Python中,由日期時間
- 18. Python日期時間索引和字典映射
- 19. 用日期時間索引或列查詢Python Pandas DataFrame
- 20. 索引的日期時間大熊貓據幀失敗
- 21. 熊貓日期時間多指標改變爲日期索引和時間的字段(重新索引)
- 22. 選擇行日期時間=日期,仍然使用索引
- 23. 數據中的日期時間格式
- 24. 基於小時/分鐘/秒的分割日期時間索引數據
- 25. 如何篩選多索引數據框上的日期
- 26. 把日期時間轉換爲Python中參考日期時間的整數
- 27. Python熊貓時間序列插值日期時間數據
- 28. 如何將數據框中的分隔日期與日期時間相結合
- 29. Python的日期時間
- 30. 由列表索引的python數據框
的D型是'datetime64 [NS]'也許,你能解釋一下你的'使用datetime with'是什麼意思你可以轉換爲Python日期時間'df.index.to_pydatetime()' – EdChum
感謝您的幫助,但回答低於 – Lererferler