gpt4 book ai didi

python - 具有完整性要求的按频率分类的 Pandas Grouper

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:13 25 4
gpt4 key购买 nike

我有每月的时间序列数据,这些数据既缺少一些条目,又由于其他原因分散了 NaN 值。我需要将数据汇总到季度和年度系列中,但我不想报告缺少数据的季度/年度数据。例如,在下面的数据中,我不想报告 2014 年第一季度的数据,因为我缺少当年的一月份。

import pandas as pd, numpy as np

df = pd.DataFrame([
('Monthly','2014-02-1', 529.1),
('Monthly','2014-03-1', 67.1),
('Monthly','2014-04-1', np.nan),
('Monthly','2014-05-1', 146.8),
('Monthly','2014-06-1', 469.7),
('Monthly','2014-07-1', 82.9),
('Monthly','2014-08-1', 636.9),
('Monthly','2014-09-1', 520.9),
('Monthly','2014-10-1', 217.4),
('Monthly','2014-11-1', 776.6),
('Monthly','2014-12-1', 18.4),
('Monthly','2015-01-1', 376.7),
('Monthly','2015-02-1', 266.5),
('Monthly','2015-03-1', np.nan),
('Monthly','2015-04-1', 144.1),
('Monthly','2015-05-1', 385.0),
('Monthly','2015-06-1', 527.1),
('Monthly','2015-07-1', 748.5),
('Monthly','2015-08-1', 518.2)],
columns=['Frequency','Date','Value'])

df['Date'] = pd.to_datetime(df['Date'])
df.set_index(['Frequency','Date'],inplace=True)
df

Value
Frequency Date
2014-02-01 529.1
2014-03-01 67.1
2014-04-01 NaN
2014-05-01 146.8
2014-06-01 469.7
2014-07-01 82.9
2014-08-01 636.9
2014-09-01 520.9
2014-10-01 217.4
2014-11-01 776.6
2014-12-01 18.4
2015-01-01 376.7
2015-02-01 266.5
2015-03-01 NaN
2015-04-01 144.1
2015-05-01 385.0
2015-06-01 527.1
2015-07-01 748.5
2015-08-01 518.2

我曾尝试使用 Grouper 函数,但 groupby 忽略了 NaN 值,并且据我所知,Grouper 实用程序不强制执行时间序列完整性:

df.groupby(pd.Grouper(level='Date', freq='Q')).sum()

Value
Date
2014-03-31 1571.2
2014-06-30 616.5
2014-09-30 1240.7
2014-12-31 1012.4
2015-03-31 643.2
2015-06-30 1056.2
2015-09-30 1266.7

我想看到的是:

             Value
Date
2014-03-31 NaN # Because of missing 2014-01-01
2014-06-30 NaN # Because of NaN in 2014-04-01
2014-09-30 1240.7
2014-12-31 1012.4
2015-03-31 NaN # Because of NaN in 2015-03-01
2015-06-30 1056.2
2015-09-30 NaN # Because of missing 2015-09-01

执行此操作的好方法是什么?

最佳答案

你可能想写自己的aggergate函数,1、如果有nan,返回一个nan; 2、如果周期太短,也返回nan; 3、否则,返回和:

In [43]:

gpy = df.groupby(pd.Grouper(level='Date', freq='Q'))

print gpy.agg(lambda x: np.nan if (np.isnan(x).any() or len(x)<3) else x.sum())

Value
Date
2014-03-31 NaN
2014-06-30 NaN
2014-09-30 1240.7
2014-12-31 1012.4
2015-03-31 NaN
2015-06-30 1056.2
2015-09-30 NaN

关于python - 具有完整性要求的按频率分类的 Pandas Grouper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33637312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com