Skip to content

REGR: DataFrame.shift with periods>len(columns) GH#44978 #45005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@ Other
- Bug in :meth:`Series.replace` and :meth:`DataFrame.replace` with ``value=None`` and ExtensionDtypes (:issue:`44270`,:issue:`37899`)
- Bug in :meth:`FloatingArray.equals` failing to consider two arrays equal if they contain ``np.nan`` values (:issue:`44382`)
- Bug in :meth:`DataFrame.shift` with ``axis=1`` and ``ExtensionDtype`` columns incorrectly raising when an incompatible ``fill_value`` is passed (:issue:`44564`)
- Bug in :meth:`DataFrame.shift` with ``axis=1`` and ``periods`` larger than ``len(frame.columns)`` producing an invalid :class:`DataFrame` (:issue:`44978`)
- Bug in :meth:`DataFrame.diff` when passing a NumPy integer object instead of an ``int`` object (:issue:`44572`)
- Bug in :meth:`Series.replace` raising ``ValueError`` when using ``regex=True`` with a :class:`Series` containing ``np.nan`` values (:issue:`43344`)
- Bug in :meth:`DataFrame.to_records` where an incorrect ``n`` was used when missing names were replaced by ``level_n`` (:issue:`44818`)
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,13 @@ def shift(self: T, periods: int, axis: int, fill_value) -> T:
# GH#35488 we need to watch out for multi-block cases
# We only get here with fill_value not-lib.no_default
ncols = self.shape[0]
nper = abs(periods)
nper = min(nper, ncols)
if periods > 0:
indexer = np.array(
[-1] * periods + list(range(ncols - periods)), dtype=np.intp
[-1] * nper + list(range(ncols - periods)), dtype=np.intp
)
else:
nper = abs(periods)
indexer = np.array(
list(range(nper, ncols)) + [-1] * nper, dtype=np.intp
)
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/frame/methods/test_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,3 +664,15 @@ def test_shift_axis1_categorical_columns(self):
columns=ci,
)
tm.assert_frame_equal(result, expected)

@td.skip_array_manager_not_yet_implemented
def test_shift_axis1_many_periods(self):
# GH#44978 periods > len(columns)
df = DataFrame(np.random.rand(5, 3))
shifted = df.shift(6, axis=1, fill_value=None)

expected = df * np.nan
tm.assert_frame_equal(shifted, expected)

shifted2 = df.shift(-6, axis=1, fill_value=None)
tm.assert_frame_equal(shifted2, expected)