Skip to content

Commit beab917

Browse files
authored
DEPR/API: DataFrame.shift(axis=1, fill_value=inty) (#49842)
1 parent c7460e5 commit beab917

File tree

3 files changed

+5
-14
lines changed

3 files changed

+5
-14
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ Other API changes
348348
- Changed behavior of :class:`Index` constructor with sequence containing at least one ``NaT`` and everything else either ``None`` or ``NaN`` to infer ``datetime64[ns]`` dtype instead of ``object``, matching :class:`Series` behavior (:issue:`49340`)
349349
- :func:`read_stata` with parameter ``index_col`` set to ``None`` (the default) will now set the index on the returned :class:`DataFrame` to a :class:`RangeIndex` instead of a :class:`Int64Index` (:issue:`49745`)
350350
- Changed behavior of :class:`Index` constructor with an object-dtype ``numpy.ndarray`` containing all-``bool`` values or all-complex values, this will now retain object dtype, consistent with the :class:`Series` behavior (:issue:`49594`)
351+
- Changed behavior of :meth:`DataFrame.shift` with ``axis=1``, an integer ``fill_value``, and homogeneous datetime-like dtype, this now fills new columns with integer dtypes instead of casting to datetimelike (:issue:`49842`)
351352
- :meth:`DataFrame.values`, :meth:`DataFrame.to_numpy`, :meth:`DataFrame.xs`, :meth:`DataFrame.reindex`, :meth:`DataFrame.fillna`, and :meth:`DataFrame.replace` no longer silently consolidate the underlying arrays; do ``df = df.copy()`` to ensure consolidation (:issue:`49356`)
352353
-
353354

pandas/core/frame.py

-7
Original file line numberDiff line numberDiff line change
@@ -5720,14 +5720,7 @@ def shift(
57205720
# keep the same dtype (i.e. the _can_hold_element check)
57215721
# then we can go through the reindex_indexer path
57225722
# (and avoid casting logic in the Block method).
5723-
# The exception to this (until 2.0) is datetimelike
5724-
# dtypes with integers, which cast.
57255723
not can_hold_element(arrays[0], fill_value)
5726-
# TODO(2.0): remove special case for integer-with-datetimelike
5727-
# once deprecation is enforced
5728-
and not (
5729-
lib.is_integer(fill_value) and needs_i8_conversion(arrays[0].dtype)
5730-
)
57315724
):
57325725
# GH#35488 we need to watch out for multi-block cases
57335726
# We only get here with fill_value not-lib.no_default

pandas/tests/frame/methods/test_shift.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,6 @@ def test_datetime_frame_shift_with_freq_error(
500500
with pytest.raises(ValueError, match=msg):
501501
no_freq.shift(freq="infer")
502502

503-
@td.skip_array_manager_not_yet_implemented # TODO(ArrayManager) axis=1 support
504503
def test_shift_dt64values_int_fill_deprecated(self):
505504
# GH#31971
506505
ser = Series([pd.Timestamp("2020-01-01"), pd.Timestamp("2020-01-02")])
@@ -516,17 +515,15 @@ def test_shift_dt64values_int_fill_deprecated(self):
516515
df2 = DataFrame({"A": ser, "B": ser})
517516
df2._consolidate_inplace()
518517

519-
with pytest.raises(TypeError, match="value should be a"):
520-
df2.shift(1, axis=1, fill_value=0)
518+
result = df2.shift(1, axis=1, fill_value=0)
519+
expected = DataFrame({"A": [0, 0], "B": df2["A"]})
520+
tm.assert_frame_equal(result, expected)
521521

522-
# same thing but not consolidated
523-
# This isn't great that we get different behavior, but
524-
# that will go away when the deprecation is enforced
522+
# same thing but not consolidated; pre-2.0 we got different behavior
525523
df3 = DataFrame({"A": ser})
526524
df3["B"] = ser
527525
assert len(df3._mgr.arrays) == 2
528526
result = df3.shift(1, axis=1, fill_value=0)
529-
expected = DataFrame({"A": [0, 0], "B": df2["A"]})
530527
tm.assert_frame_equal(result, expected)
531528

532529
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)