Skip to content

Commit cfc55b2

Browse files
snitishKevsterAmp
authored andcommitted
BUG: Maintain column order in table method rolling (pandas-dev#60465)
* BUG: Maintain column order in table method rolling * BUG: Add bug description to whatsnew/v3.0.0.rst
1 parent 08c51fd commit cfc55b2

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@ Groupby/resample/rolling
737737
- Bug in :meth:`DataFrameGroupBy.cumsum` and :meth:`DataFrameGroupBy.cumprod` where ``numeric_only`` parameter was passed indirectly through kwargs instead of passing directly. (:issue:`58811`)
738738
- Bug in :meth:`DataFrameGroupBy.cumsum` where it did not return the correct dtype when the label contained ``None``. (:issue:`58811`)
739739
- 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`)
740+
- 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`)
740741
- Bug in :meth:`Rolling.apply` where the applied function could be called on fewer than ``min_period`` periods if ``method="table"``. (:issue:`58868`)
741742
- Bug in :meth:`Series.resample` could raise when the the date range ended shortly before a non-existent time. (:issue:`58380`)
742743

pandas/core/window/rolling.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def _create_data(self, obj: NDFrameT, numeric_only: bool = False) -> NDFrameT:
269269
"""
270270
# filter out the on from the object
271271
if self.on is not None and not isinstance(self.on, Index) and obj.ndim == 2:
272-
obj = obj.reindex(columns=obj.columns.difference([self.on]))
272+
obj = obj.reindex(columns=obj.columns.difference([self.on], sort=False))
273273
if obj.ndim > 1 and numeric_only:
274274
obj = self._make_numeric_only(obj)
275275
return obj

pandas/tests/window/test_numba.py

+32
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,38 @@ def f(x):
459459
)
460460
tm.assert_frame_equal(result, expected)
461461

462+
def test_table_method_rolling_apply_col_order(self):
463+
# GH#59666
464+
def f(x):
465+
return np.nanmean(x[:, 0] - x[:, 1])
466+
467+
df = DataFrame(
468+
{
469+
"a": [1, 2, 3, 4, 5, 6],
470+
"b": [6, 7, 8, 5, 6, 7],
471+
}
472+
)
473+
result = df.rolling(3, method="table", min_periods=0)[["a", "b"]].apply(
474+
f, raw=True, engine="numba"
475+
)
476+
expected = DataFrame(
477+
{
478+
"a": [-5, -5, -5, -3.66667, -2.33333, -1],
479+
"b": [-5, -5, -5, -3.66667, -2.33333, -1],
480+
}
481+
)
482+
tm.assert_almost_equal(result, expected)
483+
result = df.rolling(3, method="table", min_periods=0)[["b", "a"]].apply(
484+
f, raw=True, engine="numba"
485+
)
486+
expected = DataFrame(
487+
{
488+
"b": [5, 5, 5, 3.66667, 2.33333, 1],
489+
"a": [5, 5, 5, 3.66667, 2.33333, 1],
490+
}
491+
)
492+
tm.assert_almost_equal(result, expected)
493+
462494
def test_table_method_rolling_weighted_mean(self, step):
463495
def weighted_mean(x):
464496
arr = np.ones((1, x.shape[1]))

0 commit comments

Comments
 (0)