从 Pandas 时间序列生成星期几箱线图的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17194581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to generate day-of-week boxplots from a Pandas timeseries
提问by dkanoafry
i am trying to create a set of day-of-week boxplots for a timeseries (e.g. 5-minute temperature observations).
我正在尝试为时间序列创建一组星期几箱线图(例如 5 分钟温度观察)。
My code:
我的代码:
# ts is our timeseries
ts = df.SomeColumn
dow_map = {}
days = ['MON','TUE','WED','THU','FRI','SAT','SUN']
dow_idx = ts.index.dayofweek
i = 0
for d in days:
dow_map[d] = ts[dow_idx == i]
i = i + 1
df = pd.DataFrame(dow_map)
df.boxplot()
results in:
结果是:
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-898-6070c45e4c4b> in <module>()
41 i = i + 1
42
---> 43 df = pd.DataFrame(dow_map)
44 df.boxplot()
...
Exception: Reindexing only valid with uniquely valued Index objects
I did find succcess by creating DataFrames for each day-of-week and then concat-ing them into a final DataFrame, but this seems inefficient...
我确实通过为每周的每一天创建 DataFrames 然后将它们连接到最终的 DataFrame 中找到了成功,但这似乎效率低下......
回答by Joop
1st Create data frame and use weekdays method to get days of week:
1st 创建数据框并使用工作日方法获取星期几:
import pandas as pd
import numpy.random as random
n=1000
df = pd.DataFrame(random.randn(n), pd.date_range('2010-01-01', periods=n), columns=["data"])
df['Dates'] = df.index
df['week_days'] =df.index.weekday
df
now pivot that table so that the week_days are as columns (could also change the needdays to string formats of days but leaving that for you.
现在旋转该表,以便 week_days 作为列(也可以将需要天数更改为天数的字符串格式,但将其留给您。
x =df.pivot(index='Dates', columns='week_days', values='data')
x.boxplot()


回答by 404pio
import locale, calendar
# for example pl_PL
locale.setlocale(locale.LC_ALL, 'pl_PL.UTF-8')
x = x.rename_axis(lambda x: calendar.day_abbr[x].capitalize())

