From b84e0c81ee424444760a4587d30a7cb8662fa4d9 Mon Sep 17 00:00:00 2001 From: eightyseven Date: Wed, 6 Nov 2024 02:41:39 +0800 Subject: [PATCH] BUG: Frequency shift on empty DataFrame (#60172) * freq shift * Save local changes before merging --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/core/frame.py | 2 +- pandas/tests/frame/methods/test_shift.py | 10 ++++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 2e64c66812306..20efac7e2edb0 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -772,6 +772,7 @@ Other - Bug in :meth:`DataFrame.eval` and :meth:`DataFrame.query` which caused an exception when using NumPy attributes via ``@`` notation, e.g., ``df.eval("@np.floor(a)")``. (:issue:`58041`) - Bug in :meth:`DataFrame.eval` and :meth:`DataFrame.query` which did not allow to use ``tan`` function. (:issue:`55091`) - Bug in :meth:`DataFrame.query` which raised an exception or produced incorrect results when expressions contained backtick-quoted column names containing the hash character ``#``, backticks, or characters that fall outside the ASCII range (U+0001..U+007F). (:issue:`59285`) (:issue:`49633`) +- Bug in :meth:`DataFrame.shift` where passing a ``freq`` on a DataFrame with no columns did not shift the index correctly. (:issue:`60102`) - Bug in :meth:`DataFrame.sort_index` when passing ``axis="columns"`` and ``ignore_index=True`` and ``ascending=False`` not returning a :class:`RangeIndex` columns (:issue:`57293`) - Bug in :meth:`DataFrame.transform` that was returning the wrong order unless the index was monotonically increasing. (:issue:`57069`) - Bug in :meth:`DataFrame.where` where using a non-bool type array in the function would return a ``ValueError`` instead of a ``TypeError`` (:issue:`56330`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index c4defdb24370f..a3a459796f765 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5705,7 +5705,7 @@ def shift( "Passing a 'freq' together with a 'fill_value' is not allowed." ) - if self.empty: + if self.empty and freq is None: return self.copy() axis = self._get_axis_number(axis) diff --git a/pandas/tests/frame/methods/test_shift.py b/pandas/tests/frame/methods/test_shift.py index 4e490e9e344ba..a0f96ff111444 100644 --- a/pandas/tests/frame/methods/test_shift.py +++ b/pandas/tests/frame/methods/test_shift.py @@ -747,3 +747,13 @@ def test_shift_axis_one_empty(self): df = DataFrame() result = df.shift(1, axis=1) tm.assert_frame_equal(result, df) + + def test_shift_with_offsets_freq_empty(self): + # GH#60102 + dates = date_range("2020-01-01", periods=3, freq="D") + offset = offsets.Day() + shifted_dates = dates + offset + df = DataFrame(index=dates) + df_shifted = DataFrame(index=shifted_dates) + result = df.shift(freq=offset) + tm.assert_frame_equal(result, df_shifted)