Skip to content

Commit d7cb419

Browse files
Backport PR pandas-dev#47051 on branch 1.4.x (BUG: DataFrame.shift shows different behavior for axis=1 when freq is specified) (pandas-dev#47158)
Backport PR pandas-dev#47051: BUG: `DataFrame.shift` shows different behavior for `axis=1` when `freq` is specified Co-authored-by: Wenjun Si <[email protected]>
1 parent 48a9a95 commit d7cb419

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/source/whatsnew/v1.4.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Fixed regressions
2222
- Fixed regression in :func:`read_csv` with ``index_col=False`` identifying first row as index names when ``header=None`` (:issue:`46955`)
2323
- Fixed regression in :meth:`.DataFrameGroupBy.agg` when used with list-likes or dict-likes and ``axis=1`` that would give incorrect results; now raises ``NotImplementedError`` (:issue:`46995`)
2424
- Fixed regression in :meth:`DataFrame.resample` and :meth:`DataFrame.rolling` when used with list-likes or dict-likes and ``axis=1`` that would raise an unintuitive error message; now raises ``NotImplementedError`` (:issue:`46904`)
25+
- Fixed regression in :meth:`DataFrame.shift` when ``axis`` is ``columns`` and ``fill_value`` is absent, ``freq`` is ignored (:issue:`47039`)
2526

2627
.. ---------------------------------------------------------------------------
2728

pandas/core/frame.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -5333,7 +5333,13 @@ def shift(
53335333
axis = self._get_axis_number(axis)
53345334

53355335
ncols = len(self.columns)
5336-
if axis == 1 and periods != 0 and fill_value is lib.no_default and ncols > 0:
5336+
if (
5337+
axis == 1
5338+
and periods != 0
5339+
and freq is None
5340+
and fill_value is lib.no_default
5341+
and ncols > 0
5342+
):
53375343
# We will infer fill_value to match the closest column
53385344

53395345
# Use a column that we know is valid for our column's dtype GH#38434

pandas/tests/frame/methods/test_shift.py

+10
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,16 @@ def test_shift_named_axis(self):
255255
result = df.shift(1, axis="columns")
256256
tm.assert_frame_equal(result, expected)
257257

258+
def test_shift_other_axis_with_freq(self, datetime_frame):
259+
obj = datetime_frame.T
260+
offset = offsets.BDay()
261+
262+
# GH#47039
263+
shifted = obj.shift(5, freq=offset, axis=1)
264+
assert len(shifted) == len(obj)
265+
unshifted = shifted.shift(-5, freq=offset, axis=1)
266+
tm.assert_equal(unshifted, obj)
267+
258268
def test_shift_bool(self):
259269
df = DataFrame({"high": [True, False], "low": [False, False]})
260270
rs = df.shift(1)

0 commit comments

Comments
 (0)