Skip to content

Commit 91431e9

Browse files
committed
Fix groupby.shift bug
1 parent 603120b commit 91431e9

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,7 @@ Groupby/resample/rolling
12221222
- Bug in :meth:`DataFrameGroupBy.apply` with ``as_index=False`` that was returning :class:`MultiIndex` instead of returning :class:`Index`. (:issue:`58291`)
12231223
- Bug in :meth:`DataFrameGroupBy.cumsum` and :meth:`DataFrameGroupBy.cumprod` where ``numeric_only`` parameter was passed indirectly through kwargs instead of passing directly. (:issue:`58811`)
12241224
- Bug in :meth:`DataFrameGroupBy.cumsum` where it did not return the correct dtype when the label contained ``None``. (:issue:`58811`)
1225+
- Bug in :meth:`DataFrameGroupBy.shift` where the resulting index would be sorted if the input is a :class:`DatetimeIndex` and multiple periods are specified (:issue:`62843`)
12251226
- 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`)
12261227
- 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`)
12271228
- Bug in :meth:`Rolling.apply` where the applied function could be called on fewer than ``min_period`` periods if ``method="table"``. (:issue:`58868`)

pandas/core/groupby/groupby.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5236,7 +5236,7 @@ def shift(
52365236
return (
52375237
shifted_dataframes[0]
52385238
if len(shifted_dataframes) == 1
5239-
else concat(shifted_dataframes, axis=1)
5239+
else concat(shifted_dataframes, axis=1, sort=False)
52405240
)
52415241

52425242
@final

pandas/core/reshape/concat.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ def concat(
419419
non_concat_axis = [obj.index for obj in objs]
420420

421421
if (
422-
any(not isinstance(index, DatetimeIndex) for index in non_concat_axis)
422+
intersect
423+
or any(not isinstance(index, DatetimeIndex) for index in non_concat_axis)
423424
or all(
424425
id(prev) == id(curr)
425426
for prev, curr in zip(non_concat_axis, non_concat_axis[1:])

pandas/tests/groupby/methods/test_groupby_shift_diff.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,21 @@ def test_group_shift_with_multiple_periods_and_both_fill_and_freq_deprecated():
248248
msg = "Passing a 'freq' together with a 'fill_value'"
249249
with pytest.raises(ValueError, match=msg):
250250
df.groupby("b")[["a"]].shift([1, 2], fill_value=1, freq="h")
251+
252+
253+
def test_groupby_shift_multiple_periods_unsorted_index():
254+
# https://github.com/pandas-dev/pandas/pull/62843
255+
idx = date_range("1/1/2000", periods=4, freq="h")
256+
df = DataFrame(
257+
{"a": [1, 2, 3], "b": [True, True, False]},
258+
index=[idx[2], idx[0], idx[1]],
259+
)
260+
result = df.groupby("b")[["a"]].shift([0, 1], freq="h")
261+
expected = DataFrame(
262+
{
263+
"a_0": [1.0, 2.0, 3.0, np.nan],
264+
"a_1": [3.0, np.nan, 2.0, 1.0],
265+
},
266+
index=[idx[2], idx[0], idx[1], idx[3]],
267+
)
268+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)