Skip to content

DEPR/API: DataFrame.shift(axis=1, fill_value=inty) #49842

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 4 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ Other API changes
- 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`)
- :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`)
- 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`)
- 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`)
-

.. ---------------------------------------------------------------------------
Expand Down
7 changes: 0 additions & 7 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5641,14 +5641,7 @@ def shift(
# keep the same dtype (i.e. the _can_hold_element check)
# then we can go through the reindex_indexer path
# (and avoid casting logic in the Block method).
# The exception to this (until 2.0) is datetimelike
# dtypes with integers, which cast.
not can_hold_element(arrays[0], fill_value)
# TODO(2.0): remove special case for integer-with-datetimelike
# once deprecation is enforced
and not (
lib.is_integer(fill_value) and needs_i8_conversion(arrays[0].dtype)
)
):
# GH#35488 we need to watch out for multi-block cases
# We only get here with fill_value not-lib.no_default
Expand Down
11 changes: 4 additions & 7 deletions pandas/tests/frame/methods/test_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,6 @@ def test_datetime_frame_shift_with_freq_error(
with pytest.raises(ValueError, match=msg):
no_freq.shift(freq="infer")

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

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

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

@pytest.mark.parametrize(
Expand Down