[英]How to plot time only of pandas datetime64[ns] attribute
I have a dataframe of a long time range in format datetime64[ns]
and a int value我有一个格式为
datetime64[ns]
的长时间范围的数据框和一个 int 值
Data looks like this:数据如下所示:
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
Target is to plot all the data in just a 00:00 - 24:00 range on the x-axis, no dates anymore.目标是在 x 轴的 00:00 - 24:00 范围内绘制所有数据,不再有日期。
As i try to plot it, the timeline is 00:00 at any point.当我尝试绘制它时,时间线在任何时候都是 00:00。 How to fix this?
如何解决这个问题?
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()
tried to convert the timestamps before to dt.time and plot it then尝试将之前的时间戳转换为 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()
Plot does not allow to do that:情节不允许这样做:
TypeError: float() argument must be a string or a number, not 'datetime.time'
According to your requirement, I guess you don't need the dates and as well as the seconds field in your timestamp.根据您的要求,我猜您不需要时间戳中的日期和秒字段。 So you need a little bit of preprocessing at first.
因此,您首先需要进行一些预处理。 Remove the seconds field using the code below
使用下面的代码删除秒字段
dataset['MIN_DEP'] = dataset['MIN_DEP'].strftime("%H:%M")
Then you can remove the date from your timestamp in the following manner然后您可以通过以下方式从时间戳中删除日期
dataset['MIN_DEP'] = pd.Series([val.time() for val in dataset['MIN_DEP']])
Then you can plot your data in the usual manner.然后,您可以以通常的方式绘制数据。
This seems to work now.这现在似乎有效。 I did not recognise, the plot was still splitting up in dates.
我没有意识到,情节仍然在日期上分裂。 To work around I hat to replace all the dates with the same date and plottet it hiding the date using DateFormatter
为了解决我的问题,我想用相同的日期替换所有日期,并使用 DateFormatter 隐藏日期
import matplotlib.dates as mdates
pd_to_stat['MIN_DEP'] = pd_to_stat['MIN_DEP'].map(lambda t: t.replace(year=2020, month=1, day=1))
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.