pandas.DataFrame.shift()fill_value不起作用 [英] pandas.DataFrame.shift() fill_value not working

查看:221
本文介绍了pandas.DataFrame.shift()fill_value不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来像是一个很奇怪的问题,但是当我将pandas.DataFrame.shift()函数与fill_value关键字参数一起使用时,我会不断得到一个TypeError: shift() got an unexpected keyword argument 'fill_value'.

This may sound like a rather weird question, but when I'm using the pandas.DataFrame.shift() function with the fill_value keyword argument, I keep getting a TypeError: shift() got an unexpected keyword argument 'fill_value'.

即使是文档中的一个简单示例也给了我该错误:

Even a simple example on the documentation is giving me that error:

df = pd.DataFrame({'c1': [1, 2, 3], 
                   'c2': [4, 5, 6],
                   'c3': [7, 8, 9]})

df.shift(periods=1, fill_value=0)

通常会期望

    c1    c2    c3
0   0     0     0
1   1     4     7
2   2     5     8
3   3     6     9

但是会引发错误.有人会知道可能是什么问题吗?我在其他地方搜索过,但似乎没有其他人遇到此问题...

but it throws the error. Would anybody happen to know what the problem may be? I've searched elsewhere but it seems that nobody else is experiencing this problem...

推荐答案

问题是使用熊猫波纹管0.24+

Problem is use pandas bellow 0.24+ where is not imlemented this parameter in DataFrame.shift.

填充值:对象,可选

用于新引入的缺失值的标量值.默认值取决于self的dtype.对于数字数据,使用np.nan.对于日期时间,时间增量或期间数据等,使用NaT.对于扩展名dtypes,使用self.dtype.na_value.

The scalar value to use for newly introduced missing values. the default depends on the dtype of self. For numeric data, np.nan is used. For datetime, timedelta, or period data, etc. NaT is used. For extension dtypes, self.dtype.na_value is used.

更改了版本 0.24.0 .

然后使用 DataFrame.fillna :

Then use DataFrame.fillna:

df = df.shift(periods=1).fillna(0)

DataFrame.fillna 通过 DataFrame.iloc 如果可能的话,数据中还有另一个缺失值,并且仅需要替换它:

Or DataFrame.fillna with specify first row by position by DataFrame.iloc if possible another missing values in data and necessary replace only it:

df = df.shift(periods=1)
df.iloc[0] = df.iloc[0].fillna(0)
print (df)
    c1   c2   c3
0  0.0  0.0  0.0
1  1.0  4.0  7.0
2  2.0  5.0  8.0

这篇关于pandas.DataFrame.shift()fill_value不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆