【发布时间】:2021-02-19 03:31:47
【问题描述】:
我有一个格式为 datetime64[ns] 的长时间范围的数据框和一个 int 值
数据如下所示:
MIN_DEP DELAY
0 2018-01-01 05:09:00 0
1 2018-01-01 05:13:00 0
2 2018-01-01 05:39:00 0
3 2018-01-01 05:43:00 0
4 2018-01-01 06:12:00 34
... ... ...
77005 2020-09-30 23:42:00 0
77006 2020-09-30 23:43:00 0
77007 2020-09-30 23:43:00 43
77008 2020-10-01 00:18:00 0
77009 2020-10-01 00:59:00 0
[77010 rows x 2 columns]
MIN_DEP datetime64[ns]
DELAY int64
dtype: object
目标是在 x 轴上仅绘制 00:00 - 24:00 范围内的所有数据,不再有日期。
当我尝试绘制它时,时间线在任何时候都是 00:00。如何解决这个问题?
import matplotlib.dates as mdates
fig, ax = plt.subplots()
ax.plot(pd_to_stat['MIN_DEP'],pd_to_stat['DELAY'])
xfmt = mdates.DateFormatter('%H:%M')
ax.xaxis.set_major_formatter(xfmt)
plt.show()
尝试将之前的时间戳转换为 dt.time 然后绘制它
pd_to_stat['time'] = pd.to_datetime(pd_to_stat['MIN_DEP'], format='%H:%M').dt.time
fig, ax = plt.subplots()
ax.plot(pd_to_stat['time'],pd_to_stat['DELAY'])
plt.show()
情节不允许这样做:
TypeError: float() argument must be a string or a number, not 'datetime.time'
【问题讨论】:
标签: python pandas datetime plot