且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

pandas 中的时间序列箱线图

更新时间:2022-06-14 09:29:35

如果它适合你,我建议使用 Seaborn,它是 Matplotlib 的包装器.您可以通过循环遍历时间序列中的组来自己完成,但这需要更多的工作.

If its an option for you, i would recommend using Seaborn, which is a wrapper for Matplotlib. You could do it yourself by looping over the groups from your timeseries, but that's much more work.

import pandas as pd
import numpy as np
import seaborn
import matplotlib.pyplot as 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.index.dayofyear, ts, ax=ax)

这给出了:

请注意,我将 day of year 作为 grouper 传递给 seaborn,如果您的数据跨越多年,这将不起作用.然后你可以考虑这样的事情:

Note that i'm passing the day of year as the grouper to seaborn, if your data spans multiple years this wouldn't work. You could then consider something like:

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

编辑,对于 3 小时,您可以将其用作石斑鱼,但它仅在没有定义分钟或更低的情况下才有效.:

Edit, for 3-hourly you could use this as a grouper, but it only works if there are no minutes or lower defined. :

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