I have a DataFrame (multiple daily timeseries) with DateTimeIndex
as index
and MultiIndex
as columns
. I would like to select a column and create a Box Plot where data are grouped by year. I thought it was easy but I am struggling to get some result.
>>> daily.shape
(11319, 118)
>>> daily.index
DatetimeIndex(['1986-01-01', '1986-01-02', '1986-01-03', '1986-01-04',
'1986-01-05', '1986-01-06', '1986-01-07', '1986-01-08',
'1986-01-09', '1986-01-10',
...
'2016-12-22', '2016-12-23', '2016-12-24', '2016-12-25',
'2016-12-26', '2016-12-27', '2016-12-28', '2016-12-29',
'2016-12-30', '2016-12-31'],
dtype='datetime64[ns]', name='timevalue', length=11319, freq=None)
>>> daily.columns
MultiIndex(levels=[['41B001', '41B004', '41B006', '41B008', '41B011', '41MEU1', '41N043', '41R001', '41R002', '41R012', '41WOL1', '41WOL2', '47E013', 'T1M001', 'T1M003'], ['BA-10.0', 'BA-2.5', 'BC', 'CO', 'CO2', 'NO', 'NO2', 'NOx', 'O3', 'PM-10.0', 'PM-2.5', 'RH', 'SO2', 'T', 'UVPM', 'VO-10.0', 'VO-2.5', 'WD', 'WS-s', 'WS-v', 'p']],
labels=[[0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14], [5, 6, 7, 3, 5, 6, 7, 8, 3, 5, 6, 7, 8, 3, 5, 6, 7, 12, 0, 1, 5, 6, 7, 8, 9, 10, 15, 16, 0, 1, 5, 6, 7, 9, 10, 15, 16, 0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 2, 3, 4, 5, 6, 7, 12, 14, 0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 0, 2, 3, 4, 5, 6, 7, 8, 9, 12, 14, 15, 4, 5, 6, 7, 12, 11, 13, 13, 17, 18, 19, 20, 11, 13, 13, 17, 18, 19, 20]],
names=['sitekey', 'measurandkey'])
The best I could achieve is:
fig, axe = plt.subplots()
daily.loc[:,[('41R001', 'SO2')]].groupby(daily.index.map(lambda x: x.year)).boxplot(ax=axe, subplots=False, rot=90)
But It will requires other postprocess for labelling axis.
When I try to reset_index()
to apply function and using pivot()
, I have indexing error because of the MultiIndex
.
d = daily.reset_index()
d['timevalue']
The Exception is: cannot handle a non-unique multi-index! That I do not understand since there is no occurrence of TimeValue in my MultiIndex. I also have tried .loc[]
but I think the problem is elsewhere.
So, what I would achieve is simple:
- I have daily timeseries among years and those timeseries are multi-indexed;
- I would like to select one of them (using
loc
and a composite key as in example above) and get a timeserie boxplot where data are grouped by year.
I thought it could be easy, but I cannot properly use pivot()
with this DataFrame because of the mutli-index error.
You were on the right track using
groupby
andpivot
. First, let's create some dummy data:Now, you can
That's it.
And more pictures following here...
If you don't mind using
seaborn
library you can make this plot pretty easily:The resulting image:
I tried using the
pandas.DataFrame.boxplot()
method but couldn't make it work for this case in a short span of time =).