Question

I have a pandas Series that I would like to plot by month, using ggplot and facetting - that is, one mini-plot for each month.

The Series has datetime as the index, so I can use Series.index.time for the x-axis, and I can get the month using Series.index.month. But if I do this, the datetimes for each facet are absolute, and so they don't line up. Basically, what I need is an index that describes the time relative to the month, or "time since start of month" (day of month + time of day). Is there any simpleway to do something like this in pandas?

Example data (if anyone has a better way of representing this usefully, let me know):

print(vars(T))

{'_data': BlockManager
Items: Index(['Tair'], dtype='object')
Axis 1: <class 'pandas.tseries.index.DatetimeIndex'>
[2002-01-01 23:00:00, ..., 2002-01-02 01:00:00]
Length: 5, Freq: 30T, Timezone: None
FloatBlock: [Tair], 1 x 5, dtype: float32, 'is_copy': None, '_item_cache': {}}

print(T)
                           Tair
2002-01-01 23:00:00  282.739990
2002-01-01 23:30:00  282.350006
2002-01-02 00:00:00  281.795013
2002-01-02 00:30:00  281.239990
2002-01-02 01:00:00  281.024994

[5 rows x 1 columns]

Basically, what I want is the same index, but with the year and month dropped.

Was it helpful?

Solution

Set the index as the time attribute of itself

T.index = T.index.time

Update

To include the day, you can use

T.index = [i.strftime('%d %H:%M') for i in T.index]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top