Fabian Bosler Fabian Bosler - 3 months ago 10
Python Question

Pandas Time Series: How to plot only times of day (no dates) against other values?

As I am preparing to do some regressions on a rather big dataset I would like to visualize the data at first.

The data we are talking about is data about the New York subway (hourly entries, rain, weather and such) for May, 2011.

When creating the dataframe I converted hours and time to pandas datetime format.

Now I realize that what I want to do does not make much sense from a logical point of view for the example at hand. However, I would still like to plot the exact time of day against the hourly entries. Which, as I said, is not very meaningful since ENTRIESn_hourly is aggregated. But let's for the sake of the argument assume the ENTRIESn_hourly would be explicitly related to the exact timestamp.

Now how would I go about taking only the times and ignoring the dates and then plot that out?

Please find the jupyter notebook here: https://github.com/FBosler/Udacity/blob/master/Example.ipynb

Thx alot!

Answer

IIUC you can do it this way:

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>

enter image description here

.dt accessor gives you access to the following attributes:

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