pandas.core.window.Rolling.count¶
The rolling count of any non-NaN observations inside the window.
- return
Series or DataFrame Returned object type is determined by the caller of the rolling calculation.
Examples¶
import numpy as np
import pandas as pd
from numba import njit
@njit
def series_rolling_count():
series = pd.Series([4, 3, 2, np.nan, 6]) # Series of 4, 3, 2, np.nan, 6
out_series = series.rolling(3).count()
return out_series # Expect series of 1.0, 2.0, 3.0, 2.0, 2.0
print(series_rolling_count())
$ python ./series/rolling/series_rolling_count.py
0 1.0
1 2.0
2 3.0
3 2.0
4 2.0
dtype: float64
import numpy as np
import pandas as pd
from numba import njit
@njit
def df_rolling_count():
df = pd.DataFrame({'A': [4, 3, 2, np.nan, 6], 'B': [4, np.nan, 2, np.nan, 6]})
out_df = df.rolling(3).count()
# Expect DataFrame of
# {'A': [1.0, 2.0, 3.0, 2.0, 2.0], 'B': [1.0, 1.0, 2.0, 1.0, 2.0]}
return out_df
print(df_rolling_count())
$ python ./dataframe/rolling/dataframe_rolling_count.py
A B
0 1.0 1.0
1 2.0 1.0
2 3.0 2.0
3 2.0 1.0
4 2.0 2.0
See also
- Series.rolling
Calling object with a Series.
- DataFrame.rolling
Calling object with a DataFrame.
- Series.count
Similar method for Series.
- DataFrame.count
Similar method for DataFrame.