Reputation: 365
With pandas lib, how to transform a time series with steps of one hour to a time series with steps of 5 minutes ?
To do this I tried to :
1) Enter the data :
import pandas as pd
config = { "00:00": 9, "01:00": 2, "02:00": 0, "03:00": 2, "04:00": 126, "05:00": 1135, "06:00": 5591, "07:00": 10968, "08:00": 7711,
"09:00": 3287, "10:00": 2652, "11:00": 2964, "12:00": 3959, "13:00": 3293, "14:00": 2625, "15:00": 3009, "16:00": 4563, "17:00": 5853,
"18:00": 5065, "19:00": 2537, "20:00": 1214, "21:00": 483, "22:00": 211, "23:00": 67, "23:59": 9 }
2) Construct lists :
list_of_keys = [key for (key,value) in config.items()]
list_of_values = [value for (key,value) in config.items()]
3) Construct the pandas.Serie
d_index = pd.DatetimeIndex(list_of_keys)
# Here I don't know if I should use DatetimeIndx or PeriodIndex ...
# p_index = pd.PeriodIndex(d_index, freq='H')
d_serie = pd.Series(list_of_values, index=d_index, dtype='int64')
After that, I do not know how to move forward ... I tried to use the resample function of the serie but without result.
Upvotes: 2
Views: 554
Reputation: 862651
You need:
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
#create series by dict
d_serie = pd.Series(config)
#convert index to TimedeltaIndex
d_serie.index = pd.to_timedelta(d_serie.index.astype(str) + ':00')
#print (d_serie)
#upsampling with forward filling NaNs
s = d_serie.resample('5Min').ffill()
#plot bar
ax = s.plot.bar()
#change format of values of axis x
ticklabels = pd.to_datetime(s.index.values).strftime('%H:%M')
ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))
#solution for many values in axis
#show only each 30th label, another are not visible
spacing = 30
visible = ax.xaxis.get_ticklabels()[::spacing]
for label in ax.xaxis.get_ticklabels():
if label not in visible:
label.set_visible(False)
plt.show()
Upvotes: 1