pyspark.pandas.window.RollingGroupby.median#
- RollingGroupby.median()[source]#
Calculate the rolling median.
Added in version 4.3.0.
- Returns:
- Series or DataFrame
Returned object type is determined by the caller of the rolling calculation.
See also
pyspark.pandas.Series.rollingCalling object with Series data.
pyspark.pandas.DataFrame.rollingCalling object with DataFrames.
pyspark.pandas.Series.medianEquivalent method for Series.
pyspark.pandas.DataFrame.medianEquivalent method for DataFrame.
Notes
median in pandas-on-Spark uses distributed percentile approximation algorithm unlike pandas, the result might be different with pandas.
Examples
>>> s = ps.Series([2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5]) >>> s.groupby(s).rolling(3).median().sort_index() 2 0 NaN 1 NaN 3 2 NaN 3 NaN 4 3.0 4 5 NaN 6 NaN 7 4.0 8 4.0 5 9 NaN 10 NaN dtype: float64
For DataFrame, each rolling median is computed column-wise.
>>> df = ps.DataFrame({"A": s.to_numpy(), "B": s.to_numpy() ** 2}) >>> df.groupby(df.A).rolling(2).median().sort_index() B A 2 0 NaN 1 4.0 3 2 NaN 3 9.0 4 9.0 4 5 NaN 6 16.0 7 16.0 8 16.0 5 9 NaN 10 25.0