Skip to content

Commit

Permalink
BUG: Maintain column order in table method rolling (#60465)
Browse files Browse the repository at this point in the history
* BUG: Maintain column order in table method rolling

* BUG: Add bug description to whatsnew/v3.0.0.rst
  • Loading branch information
snitish authored Dec 3, 2024
1 parent a6f721e commit e631442
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ Groupby/resample/rolling
- Bug in :meth:`DataFrameGroupBy.cumsum` and :meth:`DataFrameGroupBy.cumprod` where ``numeric_only`` parameter was passed indirectly through kwargs instead of passing directly. (:issue:`58811`)
- Bug in :meth:`DataFrameGroupBy.cumsum` where it did not return the correct dtype when the label contained ``None``. (:issue:`58811`)
- Bug in :meth:`DataFrameGroupby.transform` and :meth:`SeriesGroupby.transform` with a reducer and ``observed=False`` that coerces dtype to float when there are unobserved categories. (:issue:`55326`)
- Bug in :meth:`Rolling.apply` for ``method="table"`` where column order was not being respected due to the columns getting sorted by default. (:issue:`59666`)
- Bug in :meth:`Rolling.apply` where the applied function could be called on fewer than ``min_period`` periods if ``method="table"``. (:issue:`58868`)
- Bug in :meth:`Series.resample` could raise when the the date range ended shortly before a non-existent time. (:issue:`58380`)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def _create_data(self, obj: NDFrameT, numeric_only: bool = False) -> NDFrameT:
"""
# filter out the on from the object
if self.on is not None and not isinstance(self.on, Index) and obj.ndim == 2:
obj = obj.reindex(columns=obj.columns.difference([self.on]))
obj = obj.reindex(columns=obj.columns.difference([self.on], sort=False))
if obj.ndim > 1 and numeric_only:
obj = self._make_numeric_only(obj)
return obj
Expand Down
32 changes: 32 additions & 0 deletions pandas/tests/window/test_numba.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,38 @@ def f(x):
)
tm.assert_frame_equal(result, expected)

def test_table_method_rolling_apply_col_order(self):
# GH#59666
def f(x):
return np.nanmean(x[:, 0] - x[:, 1])

df = DataFrame(
{
"a": [1, 2, 3, 4, 5, 6],
"b": [6, 7, 8, 5, 6, 7],
}
)
result = df.rolling(3, method="table", min_periods=0)[["a", "b"]].apply(
f, raw=True, engine="numba"
)
expected = DataFrame(
{
"a": [-5, -5, -5, -3.66667, -2.33333, -1],
"b": [-5, -5, -5, -3.66667, -2.33333, -1],
}
)
tm.assert_almost_equal(result, expected)
result = df.rolling(3, method="table", min_periods=0)[["b", "a"]].apply(
f, raw=True, engine="numba"
)
expected = DataFrame(
{
"b": [5, 5, 5, 3.66667, 2.33333, 1],
"a": [5, 5, 5, 3.66667, 2.33333, 1],
}
)
tm.assert_almost_equal(result, expected)

def test_table_method_rolling_weighted_mean(self, step):
def weighted_mean(x):
arr = np.ones((1, x.shape[1]))
Expand Down

0 comments on commit e631442

Please sign in to comment.