Skip to content

Commit d08f12c

Browse files
authored
REG: DataFrame.shift with axis=1 and CategoricalIndex columns (#38504)
1 parent d4b6233 commit d08f12c

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

doc/source/whatsnew/v1.3.0.rst

-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ Numeric
195195
^^^^^^^
196196
- Bug in :meth:`DataFrame.quantile`, :meth:`DataFrame.sort_values` causing incorrect subsequent indexing behavior (:issue:`38351`)
197197
- Bug in :meth:`DataFrame.select_dtypes` with ``include=np.number`` now retains numeric ``ExtensionDtype`` columns (:issue:`35340`)
198-
-
199198

200199
Conversion
201200
^^^^^^^^^^

pandas/core/frame.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -4586,20 +4586,23 @@ def shift(
45864586
if axis == 1 and periods != 0 and fill_value is lib.no_default and ncols > 0:
45874587
# We will infer fill_value to match the closest column
45884588

4589+
# Use a column that we know is valid for our column's dtype GH#38434
4590+
label = self.columns[0]
4591+
45894592
if periods > 0:
45904593
result = self.iloc[:, :-periods]
45914594
for col in range(min(ncols, abs(periods))):
45924595
# TODO(EA2D): doing this in a loop unnecessary with 2D EAs
45934596
# Define filler inside loop so we get a copy
45944597
filler = self.iloc[:, 0].shift(len(self))
4595-
result.insert(0, col, filler, allow_duplicates=True)
4598+
result.insert(0, label, filler, allow_duplicates=True)
45964599
else:
45974600
result = self.iloc[:, -periods:]
45984601
for col in range(min(ncols, abs(periods))):
45994602
# Define filler inside loop so we get a copy
46004603
filler = self.iloc[:, -1].shift(len(self))
46014604
result.insert(
4602-
len(result.columns), col, filler, allow_duplicates=True
4605+
len(result.columns), label, filler, allow_duplicates=True
46034606
)
46044607

46054608
result.columns = self.columns.copy()

pandas/tests/frame/methods/test_shift.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33

44
import pandas as pd
5-
from pandas import DataFrame, Index, Series, date_range, offsets
5+
from pandas import CategoricalIndex, DataFrame, Index, Series, date_range, offsets
66
import pandas._testing as tm
77

88

@@ -292,3 +292,25 @@ def test_shift_dt64values_int_fill_deprecated(self):
292292

293293
expected = DataFrame({"A": [pd.Timestamp(0), pd.Timestamp(0)], "B": df2["A"]})
294294
tm.assert_frame_equal(result, expected)
295+
296+
def test_shift_axis1_categorical_columns(self):
297+
# GH#38434
298+
ci = CategoricalIndex(["a", "b", "c"])
299+
df = DataFrame(
300+
{"a": [1, 3], "b": [2, 4], "c": [5, 6]}, index=ci[:-1], columns=ci
301+
)
302+
result = df.shift(axis=1)
303+
304+
expected = DataFrame(
305+
{"a": [np.nan, np.nan], "b": [1, 3], "c": [2, 4]}, index=ci[:-1], columns=ci
306+
)
307+
tm.assert_frame_equal(result, expected)
308+
309+
# periods != 1
310+
result = df.shift(2, axis=1)
311+
expected = DataFrame(
312+
{"a": [np.nan, np.nan], "b": [np.nan, np.nan], "c": [1, 3]},
313+
index=ci[:-1],
314+
columns=ci,
315+
)
316+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)