Skip to content

Commit f1d1367

Browse files
Backport PR #42317: Revert "REF: move shift logic from BlockManager to DataFrame" (#42321)
Co-authored-by: jbrockmendel <[email protected]>
1 parent 9107f58 commit f1d1367

File tree

3 files changed

+44
-35
lines changed

3 files changed

+44
-35
lines changed

pandas/core/frame.py

+17-34
Original file line numberDiff line numberDiff line change
@@ -5285,45 +5285,28 @@ def shift(
52855285
axis = self._get_axis_number(axis)
52865286

52875287
ncols = len(self.columns)
5288+
if axis == 1 and periods != 0 and fill_value is lib.no_default and ncols > 0:
5289+
# We will infer fill_value to match the closest column
52885290

5289-
if (
5290-
axis == 1
5291-
and periods != 0
5292-
and ncols > 0
5293-
and (fill_value is lib.no_default or len(self._mgr.arrays) > 1)
5294-
):
5295-
# Exclude single-array-with-fill_value case so we issue a FutureWarning
5296-
# if an integer is passed with datetimelike dtype GH#31971
5297-
from pandas import concat
5291+
# Use a column that we know is valid for our column's dtype GH#38434
5292+
label = self.columns[0]
52985293

5299-
# tail: the data that is still in our shifted DataFrame
53005294
if periods > 0:
5301-
tail = self.iloc[:, :-periods]
5302-
else:
5303-
tail = self.iloc[:, -periods:]
5304-
# pin a simple Index to avoid costly casting
5305-
tail.columns = range(len(tail.columns))
5306-
5307-
if fill_value is not lib.no_default:
5308-
# GH#35488
5309-
# TODO(EA2D): with 2D EAs we could construct other directly
5310-
ser = Series(fill_value, index=self.index)
5295+
result = self.iloc[:, :-periods]
5296+
for col in range(min(ncols, abs(periods))):
5297+
# TODO(EA2D): doing this in a loop unnecessary with 2D EAs
5298+
# Define filler inside loop so we get a copy
5299+
filler = self.iloc[:, 0].shift(len(self))
5300+
result.insert(0, label, filler, allow_duplicates=True)
53115301
else:
5312-
# We infer fill_value to match the closest column
5313-
if periods > 0:
5314-
ser = self.iloc[:, 0].shift(len(self))
5315-
else:
5316-
ser = self.iloc[:, -1].shift(len(self))
5317-
5318-
width = min(abs(periods), ncols)
5319-
other = concat([ser] * width, axis=1)
5320-
5321-
if periods > 0:
5322-
result = concat([other, tail], axis=1)
5323-
else:
5324-
result = concat([tail, other], axis=1)
5302+
result = self.iloc[:, -periods:]
5303+
for col in range(min(ncols, abs(periods))):
5304+
# Define filler inside loop so we get a copy
5305+
filler = self.iloc[:, -1].shift(len(self))
5306+
result.insert(
5307+
len(result.columns), label, filler, allow_duplicates=True
5308+
)
53255309

5326-
result = cast(DataFrame, result)
53275310
result.columns = self.columns.copy()
53285311
return result
53295312

pandas/core/internals/managers.py

+19
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,25 @@ def shift(self: T, periods: int, axis: int, fill_value) -> T:
381381
if fill_value is lib.no_default:
382382
fill_value = None
383383

384+
if axis == 0 and self.ndim == 2 and self.nblocks > 1:
385+
# GH#35488 we need to watch out for multi-block cases
386+
# We only get here with fill_value not-lib.no_default
387+
ncols = self.shape[0]
388+
if periods > 0:
389+
indexer = [-1] * periods + list(range(ncols - periods))
390+
else:
391+
nper = abs(periods)
392+
indexer = list(range(nper, ncols)) + [-1] * nper
393+
result = self.reindex_indexer(
394+
self.items,
395+
indexer,
396+
axis=0,
397+
fill_value=fill_value,
398+
allow_dups=True,
399+
consolidate=False,
400+
)
401+
return result
402+
384403
return self.apply("shift", periods=periods, axis=axis, fill_value=fill_value)
385404

386405
def fillna(self: T, value, limit, inplace: bool, downcast) -> T:

pandas/tests/apply/test_frame_transform.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,15 @@ def test_transform_ufunc(axis, float_frame, frame_or_series):
3939

4040

4141
@pytest.mark.parametrize("op", frame_transform_kernels)
42-
def test_transform_groupby_kernel(axis, float_frame, op, request):
42+
def test_transform_groupby_kernel(axis, float_frame, op, using_array_manager, request):
4343
# GH 35964
44+
if using_array_manager and op == "pct_change" and axis in (1, "columns"):
45+
# TODO(ArrayManager) shift with axis=1
46+
request.node.add_marker(
47+
pytest.mark.xfail(
48+
reason="shift axis=1 not yet implemented for ArrayManager"
49+
)
50+
)
4451

4552
args = [0.0] if op == "fillna" else []
4653
if axis == 0 or axis == "index":

0 commit comments

Comments
 (0)