简体   繁体   中英

Pandas rolling mean returns 'nan'

I have an array of positions (the indexes) at which local maxima are found in a dataset. I have applied the following simple approach for extrema detection however as my data is sensitive to smaller fluctuations I wish to filter the detected extrema to include only the outliers which are one standard deviation away from the mean, as measured in a 21-minute sliding window to the activity volume.

To do this I will be required to calculate the mean and standard deviation for each 21-minute window on a rolling basis , for which the pandas.rolling_mean method will be required. However in passing the detected_extrema np.array to the rolling_mean and stddev methods I obtain the result: [[ nan nan nan nan nan nan …]] of not available results. Why is this?

x = np.array(df_1, dtype=np.float)

# for local maxima
positions = argrelextrema(x, np.greater)

detected_extrema = x.take(positions)
print detected_extrema

print pd.rolling_mean(detected_extrema, 21, min_periods=None, freq=None, center=False, how=None)
print pd.rolling_std(detected_extrema, 21, min_periods=None, freq=None, center=False, how=None)

Out:

[[   89.    60.    78.    55.    61.    49.    38.    40.    30.    20.
     36.    39.    22.    19.   772.   204.   153.   139.   184.   130.
    154.   187.   174.   279.   273.   164.    42.    36.  1004.   216.
    761.   322.   205.   564.   373.   171.]]
[[ nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan
   nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan
   nan  nan  nan  nan  nan  nan  nan  nan]]
[[ nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan
   nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan
   nan  nan  nan  nan  nan  nan  nan  nan]]

You could try reshaping your array:

a = np.array([[89, 60, 78, 55, 61, 49, 38, 40, 30, 20, 36,39,22,19, 772, 204, 153, 139, 184, 130, 154, 187, 174, 279, 273, 164,42,36,1004, 216, 761, 322, 205, 564, 373, 171]])

a.shape
(1, 36)

pd.rolling_mean(a, 21, min_periods=None, freq=None, center=False, how=None)

[[ nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan
   nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan  nan
   nan  nan  nan  nan  nan  nan  nan  nan]]

b = a.flatten()
b.shape
(36,)

pd.rolling_mean(b, 21, min_periods=None, freq=None, center=False, how=None)

[          nan           nan           nan           nan           nan
           nan           nan           nan           nan           nan
           nan           nan           nan           nan           nan
           nan           nan           nan           nan           nan
  112.95238095  117.61904762  123.04761905  132.61904762  143.          147.9047619
  147.57142857  147.47619048  193.38095238  202.23809524  237.52380952
  251.14285714  259.04761905  284.85714286  301.71428571  273.0952381 ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM