Skip to content

Commit d197767

Browse files
committed
BUG: DataFrame.shift shows different behavior for axis=1 when freq is specified
1 parent a160caf commit d197767

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/source/whatsnew/v1.5.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ Indexing
666666
- Bug in setting large integer values into :class:`Series` with ``float32`` or ``float16`` dtype incorrectly altering these values instead of coercing to ``float64`` dtype (:issue:`45844`)
667667
- Bug in :meth:`Series.asof` and :meth:`DataFrame.asof` incorrectly casting bool-dtype results to ``float64`` dtype (:issue:`16063`)
668668
- Bug in :meth:`NDFrame.xs`, :meth:`DataFrame.iterrows`, :meth:`DataFrame.loc` and :meth:`DataFrame.iloc` not always propagating metadata (:issue:`28283`)
669+
- Bug in :meth:`DataFrame.shift` when ``axis`` is ``columns`` and ``fill_value`` is absent, ``freq`` is ignored (:issue:`47039`)
669670
-
670671

671672
Missing

pandas/core/frame.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5487,7 +5487,13 @@ def shift(
54875487
axis = self._get_axis_number(axis)
54885488

54895489
ncols = len(self.columns)
5490-
if axis == 1 and periods != 0 and fill_value is lib.no_default and ncols > 0:
5490+
if (
5491+
axis == 1
5492+
and periods != 0
5493+
and freq is None
5494+
and fill_value is lib.no_default
5495+
and ncols > 0
5496+
):
54915497
# We will infer fill_value to match the closest column
54925498

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

pandas/tests/frame/methods/test_shift.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,16 @@ def test_shift_named_axis(self):
259259
result = df.shift(1, axis="columns")
260260
tm.assert_frame_equal(result, expected)
261261

262+
def test_shift_other_axis_with_freq(self, datetime_frame):
263+
obj = datetime_frame.T
264+
offset = offsets.BDay()
265+
266+
# GH#47039
267+
shifted = obj.shift(5, freq=offset, axis=1)
268+
assert len(shifted) == len(obj)
269+
unshifted = shifted.shift(-5, freq=offset, axis=1)
270+
tm.assert_equal(unshifted, obj)
271+
262272
def test_shift_bool(self):
263273
df = DataFrame({"high": [True, False], "low": [False, False]})
264274
rs = df.shift(1)

0 commit comments

Comments
 (0)