从 Pandas 时间序列生成星期几箱线图的最佳方法

Best way to generate day-of-week boxplots from a Pandas timeseries(从 Pandas 时间序列生成星期几箱线图的最佳方法)
本文介绍了从 Pandas 时间序列生成星期几箱线图的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试为时间序列创建一组星期几的箱线图(例如 5 分钟的温度观察).

i am trying to create a set of day-of-week boxplots for a timeseries (e.g. 5-minute temperature observations).

我的代码:

# 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()

结果:

---------------------------------------------------------------------------
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

我确实通过为每周的每一天创建 DataFrame,然后将它们连接到最终的 DataFrame 中找到了成功,但这似乎效率低下...

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...

推荐答案

1st 创建数据框并使用 weekdays 方法获取星期几:

1st Create data frame and use weekdays method to get days of week:

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

现在旋转该表,以便将 week_days 作为列(也可以将 needdays 更改为天的字符串格式,但留给您.

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.

x =df.pivot(index='Dates', columns='week_days', values='data')
x.boxplot()

这篇关于从 Pandas 时间序列生成星期几箱线图的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Seasonal Decomposition of Time Series by Loess with Python(Loess 用 Python 对时间序列进行季节性分解)
Resample a time series with the index of another time series(使用另一个时间序列的索引重新采样一个时间序列)
How can I simply calculate the rolling/moving variance of a time series in python?(如何在 python 中简单地计算时间序列的滚动/移动方差?)
How to use Dynamic Time warping with kNN in python(如何在python中使用动态时间扭曲和kNN)
Keras LSTM: a time-series multi-step multi-features forecasting - poor results(Keras LSTM:时间序列多步多特征预测 - 结果不佳)
Python pandas time series interpolation and regularization(Python pandas 时间序列插值和正则化)