Skip to content

Commit a6f3275

Browse files
wjsifeefladder
authored andcommitted
BUG: TypeError when shifting DataFrame created by concatenation of slices and fills with values (pandas-dev#42453)
1 parent fb22e69 commit a6f3275

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

doc/source/whatsnew/v1.3.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Fixed regressions
1717
- Performance regression in :meth:`DataFrame.isin` and :meth:`Series.isin` for nullable data types (:issue:`42714`)
1818
- Regression in updating values of :class:`pandas.Series` using boolean index, created by using :meth:`pandas.DataFrame.pop` (:issue:`42530`)
1919
- Regression in :meth:`DataFrame.from_records` with empty records (:issue:`42456`)
20+
- Fixed regression in :meth:`DataFrame.shift` where TypeError occurred when shifting DataFrame created by concatenation of slices and fills with values (:issue:`42719`)
2021
-
2122

2223
.. ---------------------------------------------------------------------------

pandas/core/internals/managers.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,14 @@ def shift(self: T, periods: int, axis: int, fill_value) -> T:
386386
# We only get here with fill_value not-lib.no_default
387387
ncols = self.shape[0]
388388
if periods > 0:
389-
indexer = [-1] * periods + list(range(ncols - periods))
389+
indexer = np.array(
390+
[-1] * periods + list(range(ncols - periods)), dtype=np.intp
391+
)
390392
else:
391393
nper = abs(periods)
392-
indexer = list(range(nper, ncols)) + [-1] * nper
394+
indexer = np.array(
395+
list(range(nper, ncols)) + [-1] * nper, dtype=np.intp
396+
)
393397
result = self.reindex_indexer(
394398
self.items,
395399
indexer,

pandas/tests/frame/methods/test_shift.py

+26
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,32 @@ def test_shift_axis1_multiple_blocks(self, using_array_manager):
183183

184184
tm.assert_frame_equal(result, expected)
185185

186+
@td.skip_array_manager_not_yet_implemented # TODO(ArrayManager) axis=1 support
187+
def test_shift_axis1_multiple_blocks_with_int_fill(self):
188+
# GH#42719
189+
df1 = DataFrame(np.random.randint(1000, size=(5, 3)))
190+
df2 = DataFrame(np.random.randint(1000, size=(5, 2)))
191+
df3 = pd.concat([df1.iloc[:4, 1:3], df2.iloc[:4, :]], axis=1)
192+
result = df3.shift(2, axis=1, fill_value=np.int_(0))
193+
assert len(df3._mgr.blocks) == 2
194+
195+
expected = df3.take([-1, -1, 0, 1], axis=1)
196+
expected.iloc[:, :2] = np.int_(0)
197+
expected.columns = df3.columns
198+
199+
tm.assert_frame_equal(result, expected)
200+
201+
# Case with periods < 0
202+
df3 = pd.concat([df1.iloc[:4, 1:3], df2.iloc[:4, :]], axis=1)
203+
result = df3.shift(-2, axis=1, fill_value=np.int_(0))
204+
assert len(df3._mgr.blocks) == 2
205+
206+
expected = df3.take([2, 3, -1, -1], axis=1)
207+
expected.iloc[:, -2:] = np.int_(0)
208+
expected.columns = df3.columns
209+
210+
tm.assert_frame_equal(result, expected)
211+
186212
@pytest.mark.filterwarnings("ignore:tshift is deprecated:FutureWarning")
187213
def test_tshift(self, datetime_frame):
188214
# TODO: remove this test when tshift deprecation is enforced

0 commit comments

Comments
 (0)