Sunday 15 January 2012

python - Time-series boxplot in pandas -



python - Time-series boxplot in pandas -

how can create boxplot pandas time-series have box each day?

sample dataset of hourly info 1 box should consist of 24 values:

import pandas pd n = 480 ts = pd.series(randn(n), index=pd.date_range(start="2014-02-01", periods=n, freq="h")) ts.plot()

i aware create column day, have proper x-axis labeling , x-limit functionality (like in ts.plot()), beingness able work datetime index great.

there similar question r/ggplot2 here, if helps clarify want.

if alternative you, recommend using seaborn, wrapper matplotlib. looping on groups timeseries, that's much more work.

import pandas pd import seaborn import matplotlib.pyplot plt n = 480 ts = pd.series(np.random.randn(n), index=pd.date_range(start="2014-02-01", periods=n, freq="h")) fig, ax = plt.subplots(figsize=(12,5)) seaborn.boxplot(ts, ts.index.dayofyear, ax=ax)

which gives:

note i'm passing day of year grouper seaborn, if info spans multiple years wouldn't work. consider like:

ts.index.to_series().apply(lambda x: x.strftime('%y%m%d'))

edit, 3-hourly utilize grouper, works if there no minutes or lower defined. :

[(dt - datetime.timedelta(hours=int(dt.hour % 3))).strftime('%y%m%d%h') dt in ts.index]

python pandas time-series boxplot

No comments:

Post a Comment