Skip to main content
 首页 » 编程设计

python之Pandas 时间序列 : How to plot only times of day (no dates) against other values

2025年05月04日24oomusou

当我准备对一个相当大的数据集进行一些回归时,我想首先将数据可视化。

我们这里说的数据是2011年5月纽约地铁的数据(每小时车次、雨量、天气等)。

在创建数据框时,我将小时和时间转换为 pandas 日期时间格式。
现在我意识到,从手头示例的逻辑角度来看,我想做的事情没有多大意义。但是,我仍然想根据每小时条目绘制一天中的确切时间。正如我所说,这不是很有意义,因为 ENTRIESn_hourly 是聚合的。但是,为了论证,我们假设 ENTRIESn_hourly 与确切的时间戳明确相关。

现在我将如何只获取时间并忽略日期然后将其绘制出来?

请在此处找到 jupyter notebook:https://github.com/FBosler/Udacity/blob/master/Example.ipynb

非常感谢!

请您参考如下方法:

IIUC 你可以这样做:

In [9]: weather_turnstile.plot.line(x=weather_turnstile.Date_Time.dt.time, y='ENTRIESn_hourly', marker='o', alpha=0.3) 
Out[9]: <matplotlib.axes._subplots.AxesSubplot at 0xc2a63c8> 

.dt accessor使您可以访问以下属性:

In [10]: weather_turnstile.Date_Time.dt. 
weather_turnstile.Date_Time.dt.ceil             weather_turnstile.Date_Time.dt.is_quarter_end   weather_turnstile.Date_Time.dt.strftime 
weather_turnstile.Date_Time.dt.date             weather_turnstile.Date_Time.dt.is_quarter_start weather_turnstile.Date_Time.dt.time 
weather_turnstile.Date_Time.dt.day              weather_turnstile.Date_Time.dt.is_year_end      weather_turnstile.Date_Time.dt.to_period 
weather_turnstile.Date_Time.dt.dayofweek        weather_turnstile.Date_Time.dt.is_year_start    weather_turnstile.Date_Time.dt.to_pydatetime 
weather_turnstile.Date_Time.dt.dayofyear        weather_turnstile.Date_Time.dt.microsecond      weather_turnstile.Date_Time.dt.tz 
weather_turnstile.Date_Time.dt.days_in_month    weather_turnstile.Date_Time.dt.minute           weather_turnstile.Date_Time.dt.tz_convert 
weather_turnstile.Date_Time.dt.daysinmonth      weather_turnstile.Date_Time.dt.month            weather_turnstile.Date_Time.dt.tz_localize 
weather_turnstile.Date_Time.dt.floor            weather_turnstile.Date_Time.dt.nanosecond       weather_turnstile.Date_Time.dt.week 
weather_turnstile.Date_Time.dt.freq             weather_turnstile.Date_Time.dt.normalize        weather_turnstile.Date_Time.dt.weekday 
weather_turnstile.Date_Time.dt.hour             weather_turnstile.Date_Time.dt.quarter          weather_turnstile.Date_Time.dt.weekday_name 
weather_turnstile.Date_Time.dt.is_month_end     weather_turnstile.Date_Time.dt.round            weather_turnstile.Date_Time.dt.weekofyear 
weather_turnstile.Date_Time.dt.is_month_start   weather_turnstile.Date_Time.dt.second           weather_turnstile.Date_Time.dt.year