【发布时间】:2021-03-21 13:12:08
【问题描述】:
我观察到年份数据(我的 Matplotlib 图表中的横轴)是错误的。我所有的数据都是 2020 年的,但图表显示的是 1970-1980。
df:
Timestamp Value
36 2020-11-08 23:30:40.370 45.5
47 2020-11-13 04:52:29.410 44.5
67 2020-12-01 22:17:50.300 42.5
129 2020-11-24 00:57:11.950 43.0
176 2020-12-03 01:40:16.250 42.0
246 2020-11-12 07:32:54.000 43.5
281 2020-11-30 21:13:07.630 45.5
335 2020-11-30 20:43:11.050 43.5
...
图表如下所示:
根据@ImportanceOfBeingErnest's answer,我认为问题可能与熊猫日期的格式有关。所以我尝试了:
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
df.set_index('Timestamp')
df.index.to_pydatetime()
返回:
AttributeError Traceback (most recent call last)
<ipython-input-394-d7c25df8c8d8> in <module>
38 df.set_index('Timestamp')
39
---> 40 df.index.to_pydatetime()
41
AttributeError: 'Int64Index' object has no attribute 'to_pydatetime'
这个问题的原因是什么?
【问题讨论】:
标签: python pandas datetime matplotlib