我正在编写以下代码:
# 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)
他们有什么不同?
发布于 2020-08-20 16:06:55
data.rolling(360)['Ozone'] & data['Ozone'].rolling(360)可以互换使用,但是应该在使用聚合方法(如.mean )之后对它们进行比较,并且应该使用pandas.DataFrame.equal进行比较。.rolling方法需要一个window或用于计算的观察数。window中的值,下面的示例中的10,都是用NaN填充的。pandas.DataFrame.rollingpandas.Series.rollingdf.rolling(10)['A']) & df['A'].rolling(10)是一种pandas.core.window.rolling.Rolling类型,无法与之相比。.rolling。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: float64np.nan == np.nan是False。本质上,它们是相同的,但是当将这两个行与==进行比较时,与NaN的行计算为False。pandas.DataFrame.equals时,将位于同一位置的NaNs视为相等。# 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]:
Truehttps://stackoverflow.com/questions/63508736
复制相似问题