def moving_average(Ser, window_size=1): ''' Applies a moving average (box) filter on an input time series Parameters ---------- Ser : pandas.Series (index must be a DateTimeIndex or julian date) window_size : float, optional The size of the moving_average window [days] that will be applied on the input Series Default: 1 Returns ------- Ser : pandas.Series moving-average filtered time series ''' # if index is datetimeindex then convert it to julian date if type(Ser.index) == pd.DatetimeIndex: jd_index = julday(Ser.index.month, Ser.index.day, Ser.index.year, Ser.index.hour, Ser.index.minute, Ser.index.second) else: jd_index = Ser.index.values filtered = boxcar_filter(np.squeeze(Ser.values.astype(np.double)), jd_index.astype(np.double), window=window_size) result = pd.Series(filtered, index=Ser.index) return result
def test_boxcar_filter_arrsize_1(): """ Test boxcar filter with array of size 1 """ test_jd = np.arange(1, dtype=np.double) test_data = np.array([1], dtype=np.double) filtered = filters.boxcar_filter(test_data, test_jd, window=5) np.testing.assert_allclose(filtered, [1.])
def test_boxcar_filter_arrsize_1(): """ Test boxcar filter with array of size 1 """ test_jd = np.arange(1, dtype=np.double) test_data = np.array( [1], dtype=np.double) filtered = filters.boxcar_filter(test_data, test_jd, window=5) np.testing.assert_allclose(filtered, [1.])
def test_boxcar_filter(): """ Test boxcar filter """ test_jd = np.arange(10, dtype=np.double) test_data = np.array( [1, 2, 3, 4, -999999.0, 6, 7, 8, 9, np.nan], dtype=np.double) filtered = filters.boxcar_filter(test_data, test_jd, window=5) np.testing.assert_allclose(filtered, [2., 2.5, 2.5, 3.75, np.nan, 6.25, 7.5, 7.5, 8., np.nan])
def moving_average(Ser, window_size=1, fillna=False, min_obs=1): ''' Applies a moving average (box) filter on an input time series Parameters ---------- Ser : pandas.Series (index must be a DateTimeIndex or julian date) window_size : float, optional The size of the moving_average window [days] that will be applied on the input Series Default: 1 fillna: bool, optional Fill nan values at the center window value min_obs: int The minimum amount of observations necessary for a valid moving average Returns ------- Ser : pandas.Series moving-average filtered time series ''' # if index is datetimeindex then convert it to julian date if type(Ser.index) == pd.DatetimeIndex: jd_index = julday(np.asarray(Ser.index.month), np.asarray(Ser.index.day), np.asarray(Ser.index.year), np.asarray(Ser.index.hour), np.asarray(Ser.index.minute), np.asarray(Ser.index.second)) else: jd_index = Ser.index.values filtered = boxcar_filter( np.atleast_1d(np.squeeze(Ser.values.astype(np.double))), jd_index.astype(np.double), window=window_size, fillna=fillna, min_obs=min_obs) result = pd.Series(filtered, index=Ser.index) return result
def moving_average(Ser, window_size=1, fillna=False, min_obs=1): ''' Applies a moving average (box) filter on an input time series Parameters ---------- Ser : pandas.Series (index must be a DateTimeIndex or julian date) window_size : float, optional The size of the moving_average window [days] that will be applied on the input Series Default: 1 fillna: bool, optional Fill nan values at the center window value min_obs: int The minimum amount of observations necessary for a valid moving average Returns ------- Ser : pandas.Series moving-average filtered time series ''' # if index is datetimeindex then convert it to julian date if type(Ser.index) == pd.DatetimeIndex: jd_index = julday(np.asarray(Ser.index.month), np.asarray(Ser.index.day), np.asarray(Ser.index.year), np.asarray(Ser.index.hour), np.asarray(Ser.index.minute), np.asarray(Ser.index.second)) else: jd_index = Ser.index.values filtered = boxcar_filter(np.atleast_1d( np.squeeze(Ser.values.astype(np.double))), jd_index.astype(np.double), window=window_size, fillna=fillna, min_obs=min_obs) result = pd.Series(filtered, index=Ser.index) return result
def moving_average(Ser, window_size=1): ''' Applies a moving average (box) filter on an input time series Parameters ---------- Ser : pandas.Series (index must be a DateTimeIndex or julian date) window_size : float, optional The size of the moving_average window [days] that will be applied on the input Series Default: 1 Returns ------- Ser : pandas.Series moving-average filtered time series ''' # if index is datetimeindex then convert it to julian date if type(Ser.index) == pd.DatetimeIndex: jd_index = julday(np.asarray(Ser.index.month), np.asarray(Ser.index.day), np.asarray(Ser.index.year), np.asarray(Ser.index.hour), np.asarray(Ser.index.minute), np.asarray(Ser.index.second)) else: jd_index = Ser.index.values filtered = boxcar_filter( np.atleast_1d(np.squeeze(Ser.values.astype(np.double))), jd_index.astype(np.double), window=window_size) result = pd.Series(filtered, index=Ser.index) return result