如何仅绘制熊猫datetime64 [ns]属性的时间

时间:2020-11-06 11:38:22

标签: python pandas datetime plot

我有一个较长时间范围的数据帧,格式为datetime64[ns]和一个整数值

数据如下:

                  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()

Time is 00:00 at any point

试图将之前的时间戳转换为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'

1 个答案:

答案 0 :(得分:0)

这似乎现在可行。我没有意识到,剧情还在按日期划分。要解决此问题,我建议将所有日期替换为相同的日期,然后使用DateFormatter将其隐藏起来,以进行打印。

ExecStopPost=/bin/bash -c "if [ $(systemctl show your_service --property=ExecMainStatus) == 'ExecMainStatus=143' ]; then exit; else /bin/bash -c '/usr/local/bin/your_custom_program.sh'; fi"

Time is now visible