[英]Pandas Resampling: TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'
Please, help me. 请帮我。 I want to resample based on 1D. 我想基于1D进行重新采样。 I have following format of data. 我有以下格式的数据。 I want to use resampling in pandas. 我想在熊猫中使用重新采样。
I want to resample based on Date and product and also fill the missing values. 我想根据日期和产品重新采样,并填写缺失的值。
But I keep getting this mistake: I tried like 5 options and mistake only changes after "instance of": I saw there Multiindex, Index. 但我一直犯这个错误:我尝试了5个选项并且只在“实例”之后才改变错误:我看到了Multiindex,Index。
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' TypeError:仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但得到'RangeIndex'的实例
product value date
A 1.52 2016-01-01
A NULL 2016-09-20
A 1.33 2018-08-02
B 1.30 2016-01-01
B NULL 2017-01-02
B 1.54 2017-03-10
B 2.08 2017-06-28
B 2.33 2018-08-02
I put these data into 我把这些数据放进去了
df.reset_index().set_index('date','sku')
df= df.groupby('product').resample('1D')['value'].ffill().bfill().ffill()
I tried also: 我也尝试过:
df = df.set_index(['date','sku'])
df = df.set_index('date','sku')
df = df.reset_index().set_index(['date','sku'])
Please, can you explain me what I am doing wrong? 拜托,你能解释一下我做错了什么吗? Thanks! 谢谢!
Today morning it was working on these data and the command from Jezrael: 今天早上它正在处理这些数据和来自Jezrael的命令:
df = df.set_index('date').groupby('product').resample('1D')['value'].ffill()
product value date
0 A 1.52 2016-01-01
1 A NaN 2016-09-20
2 A 1.87 2018-08-02
3 B 2.33 2016-01-01
4 B NaN 2016-09-20
5 B 4.55 2018-08-02
But suddenly it doesnt anymore. 但突然间它不再存在了。 Now I have Index in the error statement. 现在我在错误声明中有索引。
You need DatetimeIndex
if working with DataFrameGroupBy.resample
, also bfill
is omited because if some only NaN
s groups is possible these data are replaced from another groups: 您需要DatetimeIndex
如果有工作DataFrameGroupBy.resample
,也bfill
是因为被遗漏的,如果一些只NaN
S基团是可能的,这些数据是从另一组取代:
#if necessary convert to datetimes
#df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date').groupby('product').resample('1D')['value'].ffill()
print (df)
product date
A 2016-01-01 1.52
2016-01-02 1.52
2016-01-03 1.52
2016-01-04 1.52
2016-01-05 1.52
2016-01-06 1.52
2016-01-07 1.52
2016-01-08 1.52
2016-01-09 1.52
2016-01-10 1.52
2016-01-11 1.52
2016-01-12 1.52
Changed sample for better explanation: 更改样本以获得更好的解释:
print (df)
product value date
0 A 1.52 2016-01-01
1 A NaN 2016-01-03
2 B NaN 2017-01-02
3 B NaN 2017-01-03
4 C 1.54 2017-03-10
5 C 2.08 2017-03-12
6 C 2.33 2017-03-14
df1 = df.set_index('date').groupby('product').resample('1D')['value'].ffill()
print (df1)
product date
A 2016-01-01 1.52
2016-01-02 1.52
2016-01-03 NaN < NaN is not changed because in original data
B 2017-01-02 NaN <- only NaN group B
2017-01-03 NaN
C 2017-03-10 1.54
2017-03-11 1.54
2017-03-12 2.08
2017-03-13 2.08
2017-03-14 2.33
Name: value, dtype: float64
df11 = df.set_index('date').groupby('product').resample('1D')['value'].ffill().bfill()
print (df11)
product date
A 2016-01-01 1.52
2016-01-02 1.52
2016-01-03 1.54 <- back filling value from group C
B 2017-01-02 1.54 <- back filling value from group C
2017-01-03 1.54 <- back filling value from group C
C 2017-03-10 1.54
2017-03-11 1.54
2017-03-12 2.08
2017-03-13 2.08
2017-03-14 2.33
Name: value, dtype: float64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.