Skip to content

Commit b84e0c8

Browse files
authored
BUG: Frequency shift on empty DataFrame (#60172)
* freq shift * Save local changes before merging
1 parent 34387bd commit b84e0c8

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,7 @@ Other
772772
- 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`)
773773
- Bug in :meth:`DataFrame.eval` and :meth:`DataFrame.query` which did not allow to use ``tan`` function. (:issue:`55091`)
774774
- 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`)
775+
- Bug in :meth:`DataFrame.shift` where passing a ``freq`` on a DataFrame with no columns did not shift the index correctly. (:issue:`60102`)
775776
- 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`)
776777
- Bug in :meth:`DataFrame.transform` that was returning the wrong order unless the index was monotonically increasing. (:issue:`57069`)
777778
- 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`)

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5705,7 +5705,7 @@ def shift(
57055705
"Passing a 'freq' together with a 'fill_value' is not allowed."
57065706
)
57075707

5708-
if self.empty:
5708+
if self.empty and freq is None:
57095709
return self.copy()
57105710

57115711
axis = self._get_axis_number(axis)

pandas/tests/frame/methods/test_shift.py

+10
Original file line numberDiff line numberDiff line change
@@ -747,3 +747,13 @@ def test_shift_axis_one_empty(self):
747747
df = DataFrame()
748748
result = df.shift(1, axis=1)
749749
tm.assert_frame_equal(result, df)
750+
751+
def test_shift_with_offsets_freq_empty(self):
752+
# GH#60102
753+
dates = date_range("2020-01-01", periods=3, freq="D")
754+
offset = offsets.Day()
755+
shifted_dates = dates + offset
756+
df = DataFrame(index=dates)
757+
df_shifted = DataFrame(index=shifted_dates)
758+
result = df.shift(freq=offset)
759+
tm.assert_frame_equal(result, df_shifted)

0 commit comments

Comments
 (0)