【问题标题】:How to use rolling in pandas?如何在熊猫中使用滚动?
【发布时间】:2020-08-21 01:13:22
【问题描述】:

我正在编写以下代码:

# 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)

它们有什么区别?

【问题讨论】:

    标签: python pandas


    【解决方案1】:
    • data.rolling(360)['Ozone'] & data['Ozone'].rolling(360) 可以互换使用,但需要使用聚合方法进行比较,如.mean,使用pandas.DataFrame.equal进行比较。
    • .rolling 方法需要 window,或用于计算的观察数。下例中window10中的值用NaN填充。
    • pandas.DataFrame.rolling
    • pandas.Series.rolling
    • 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
    • 但是,使用 pandas.DataFrame.equals 会将同一位置的 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 2018-07-30
      • 2021-10-17
      • 2017-12-28
      • 2018-06-26
      • 2017-03-30
      • 2019-04-11
      相关资源
      最近更新 更多