如何在Pandas中使用滚动?

2024-05-19 09:47:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写以下代码:

# Resample, interpolate and inspect ozone data here
data = data.resample('D').interpolate()
data.info()
# Create the rolling window
***rolling = data.rolling(360)['Ozone']

# Insert the rolling quantiles to the monthly returns
data['q10'] = rolling.quantile(.1)
data['q50'] = rolling.quantile(.5)
data['q90'] = rolling.quantile(.9)
# Plot the data
data.plot()
plt.show()

对于带星号的行(***),我想知道,我可以使用下面的行吗

data['Ozone'].rolling(360)

为什么下面的表达式是False

data.rolling(360)['Ozone']==data['Ozone'].rolling(360)

他们的区别是什么


Tags: andthe代码infodataherecreateresample
1条回答
网友
1楼 · 发布于 2024-05-19 09:47:05
  • data.rolling(360)['Ozone']&data['Ozone'].rolling(360)可以互换使用,但是在使用聚合方法(如.mean)之后,应该对它们进行比较,并且pandas.DataFrame.equal应该用于进行比较
  • .rolling方法需要window或用于计算的观察数。下例中window10中的值用NaN填充
  • ^{}
  • ^{}
  • df.rolling(10)['A'])&df['A'].rolling(10)pandas.core.window.rolling.Rolling类型,不会进行比较。
  • Pandas: Window-函数
import pandas as pd
import numpy as np

# test data and dataframe
np.random.seed(10)
df = pd.DataFrame(np.random.randint(20, size=(20, 1)), columns=['A'])

# this is pandas.DataFrame.rolling with a column selection
df.rolling(10)['A']
[out]:
Rolling [window=10,center=False,axis=0]

# this is pandas.Series.rolling
df['A'].rolling(10)
[out]:
Rolling [window=10,center=False,axis=0]

# see that the type is the same, pandas.core.window.rolling.Rolling
type(df.rolling(10)['A']) == type(df['A'].rolling(10))
[out]:
True

# the two implementations evaluate as False, when compared
df.rolling(10)['A'] == df['A'].rolling(10)
[out]:
False
  • 一旦使用聚合方法,就可以对对象进行比较。
    • 聚合.mean,我们可以看到用于window的值是NaN
  • df.rolling(10)['A'].mean()&df['A'].rolling(10).mean()都是pandas.core.series.Series类型,可以比较
df.rolling(10)['A'].mean()
[out]:
0      NaN
1      NaN
2      NaN
3      NaN
4      NaN
5      NaN
6      NaN
7      NaN
8      NaN
9     12.3
10    12.2
11    12.1
12    12.3
13    11.1
14    12.1
15    12.3
16    12.3
17    12.0
18    11.5
19    11.9
Name: A, dtype: float64

df['A'].rolling(10).mean()
[out]:
0      NaN
1      NaN
2      NaN
3      NaN
4      NaN
5      NaN
6      NaN
7      NaN
8      NaN
9     12.3
10    12.2
11    12.1
12    12.3
13    11.1
14    12.1
15    12.3
16    12.3
17    12.0
18    11.5
19    11.9
Name: A, dtype: float64
  • 它们的计算结果不同,因为np.nan == np.nanFalse。本质上,它们是相同的,但是当将二者与==进行比较时,NaN的行计算为False
  • 但是,使用^{}将相同位置的NAN视为相等
# row by row evaluation
df.rolling(10)['A'].mean() == df['A'].rolling(10).mean()
[out]:
0     False
1     False
2     False
3     False
4     False
5     False
6     False
7     False
8     False
9      True
10     True
11     True
12     True
13     True
14     True
15     True
16     True
17     True
18     True
19     True
Name: A, dtype: bool

# overall comparison
all(df.rolling(10)['A'].mean() == df['A'].rolling(10).mean())
[out]:
False

# using pandas.DataFrame.equals
df.rolling(10)['A'].mean().equals(df['A'].rolling(10).mean())
[out]:
True

相关问题 更多 >

    热门问题