Skip to content

BUG: rolling.median/quantile does not work with (some) BaseIndexer subclasses #37153

Closed
@mroeschke

Description

@mroeschke
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Using the example from computation.rst.

In [1]: In [70]: use_expanding = [True, False, True, False, True]
   ...:

In [2]: In [72]: df = pd.DataFrame({'values': range(5)})
   ...:

In [3]: In [2]: from pandas.api.indexers import BaseIndexer
   ...:

In [4]: ...: class CustomIndexer(BaseIndexer):
   ...: ...:
   ...: ...:    def get_window_bounds(self, num_values, min_periods, center, closed):
   ...: ...:        start = np.empty(num_values, dtype=np.int64)
   ...: ...:        end = np.empty(num_values, dtype=np.int64)
   ...: ...:        for i in range(num_values):
   ...: ...:            if self.use_expanding[i]:
   ...: ...:                start[i] = 0
   ...: ...:                end[i] = i + 1
   ...: ...:            else:
   ...: ...:                start[i] = i
   ...: ...:                end[i] = i + self.window_size
   ...: ...:        return start, end
   ...:

In [5]: indexer = CustomIndexer(window_size=1, use_expanding=use_expanding)

# Each window
In [7]: for window in df.rolling(indexer):
   ...:     print(window)
   ...:
   values
0       0
   values
1       1
   values
0       0
1       1
2       2
   values
3       3
   values
0       0
1       1
2       2
3       3
4       4

# Should be [0, 1, 1, 3, 2]
In [8]: df.rolling(indexer).median()
Out[8]:
   values
0     0.0
1     1.0
2     1.5
3     NaN
4     3.0

# Should be [0, 1, 1, 3, 2]
In [10]: df.rolling(indexer).quantile(0.5)
Out[10]:
   values
0     0.0
1     1.0
2     1.5
3     NaN
4     3.0

The cython aggregations probably assume some sort of property of the windows that may not hold with the custom indexers

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugWindowrolling, ewma, expanding

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions