module 'pandas' has no attribute 'rolling_mean'
时间: 2023-09-19 11:08:26 浏览: 213
在新版的 pandas 库中,`rolling_mean()` 方法已经被弃用了,改为了 `rolling().mean()` 方法。你可以将代码中的 `rolling_mean()` 替换为 `rolling().mean()`,就可以解决该问题了。以下是一个示例:
```python
import pandas as pd
# 创建一个 Series
s = pd.Series([1, 2, 3, 4, 5])
# 对该 Series 进行滚动平均操作
rolling_mean = s.rolling(window=2).mean()
print(rolling_mean)
```
输出结果为:
```
0 NaN
1 1.5
2 2.5
3 3.5
4 4.5
dtype: float64
```
相关问题
module 'pandas' has no attribute 'rolling _mean'
This error message indicates that there is a typo in the code, where there is an extra space between "rolling" and "_mean" in the attribute name. The correct attribute name is "rolling_mean" in pandas module. The following code snippet demonstrates how to use the rolling_mean method:
```python
import pandas as pd
# create a sample data frame
df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [10, 20, 30, 40, 50]})
# calculate the rolling mean of column A with window size 2
rolling_mean = df['A'].rolling(window=2).mean()
print(rolling_mean)
```
Output:
```
0 NaN
1 1.5
2 2.5
3 3.5
4 4.5
Name: A, dtype: float64
```
AttributeError: module 'pandas' has no attribute 'rolling_mean'
这个错误是因为 `rolling_mean` 已经被弃用了,应该使用 `rolling().mean()`。
例如,如果你想对 Pandas DataFrame 中的一列计算滚动平均值,可以使用以下代码:
```
import pandas as pd
# 创建 DataFrame
df = pd.DataFrame({'values': [1, 2, 3, 4, 5]})
# 计算滚动平均值
rolling_mean = df['values'].rolling(window=2).mean()
print(rolling_mean)
```
输出:
```
0 NaN
1 1.5
2 2.5
3 3.5
4 4.5
Name: values, dtype: float64
```
在这个例子中,我们使用了 `rolling()` 方法来计算滚动窗口大小为 2 的平均值。
阅读全文