Skip to content

DEPR: interpolate with object dtype #53638

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 1 commit into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ Deprecations
- Deprecated logical operation between two non boolean :class:`Series` with different indexes always coercing the result to bool dtype. In a future version, this will maintain the return type of the inputs. (:issue:`52500`, :issue:`52538`)
- Deprecated :func:`value_counts`, use ``pd.Series(obj).value_counts()`` instead (:issue:`47862`)
- Deprecated :meth:`Series.first` and :meth:`DataFrame.first` (please create a mask and filter using ``.loc`` instead) (:issue:`45908`)
- Deprecated :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` for object-dtype (:issue:`53631`)
- Deprecated allowing ``downcast`` keyword other than ``None``, ``False``, "infer", or a dict with these as values in :meth:`Series.fillna`, :meth:`DataFrame.fillna` (:issue:`40988`)
- Deprecated allowing arbitrary ``fill_value`` in :class:`SparseDtype`, in a future version the ``fill_value`` will need to be compatible with the ``dtype.subtype``, either a scalar that can be held by that subtype or ``NaN`` for integer or bool subtypes (:issue:`23124`)
- Deprecated behavior of :func:`assert_series_equal` and :func:`assert_frame_equal` considering NA-like values (e.g. ``NaN`` vs ``None`` as equivalent) (:issue:`52081`)
Expand Down
12 changes: 12 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7882,6 +7882,18 @@ def interpolate(
FutureWarning,
stacklevel=find_stack_level(),
)
elif np.any(obj.dtypes == object):
# GH#53631
if not (obj.ndim == 2 and np.all(obj.dtypes == object)):
# don't warn in cases that already raise
warnings.warn(
f"{type(self).__name__}.interpolate with object dtype is "
"deprecated and will raise in a future version. Call "
"obj.infer_objects(copy=False) before interpolating instead.",
FutureWarning,
stacklevel=find_stack_level(),
)

if method not in fillna_methods:
axis = self._info_axis_number

Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/copy_view/test_interp_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ def test_interpolate_cleaned_fill_method(using_copy_on_write):
df = DataFrame({"a": ["a", np.nan, "c"], "b": 1})
df_orig = df.copy()

result = df.interpolate(method="linear")
msg = "DataFrame.interpolate with object dtype"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df.interpolate(method="linear")

if using_copy_on_write:
assert np.shares_memory(get_array(result, "a"), get_array(df, "a"))
Expand Down
12 changes: 8 additions & 4 deletions pandas/tests/frame/methods/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def test_interp_basic(self, using_copy_on_write):
"D": list("abcd"),
}
)
result = df.interpolate()
msg = "DataFrame.interpolate with object dtype"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df.interpolate()
tm.assert_frame_equal(result, expected)

# check we didn't operate inplace GH#45791
Expand All @@ -82,7 +84,8 @@ def test_interp_basic(self, using_copy_on_write):
assert not np.shares_memory(cvalues, result["C"]._values)
assert not np.shares_memory(dvalues, result["D"]._values)

res = df.interpolate(inplace=True)
with tm.assert_produces_warning(FutureWarning, match=msg):
res = df.interpolate(inplace=True)
assert res is None
tm.assert_frame_equal(df, expected)

Expand All @@ -100,7 +103,9 @@ def test_interp_basic_with_non_range_index(self):
}
)

result = df.set_index("C").interpolate()
msg = "DataFrame.interpolate with object dtype"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df.set_index("C").interpolate()
expected = df.set_index("C")
expected.loc[3, "A"] = 3
expected.loc[5, "B"] = 9
Expand All @@ -120,7 +125,6 @@ def test_interp_bad_method(self):
"A": [1, 2, np.nan, 4],
"B": [1, 4, 9, np.nan],
"C": [1, 2, 3, 5],
"D": list("abcd"),
}
)
msg = (
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/series/methods/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,8 +846,10 @@ def test_interpolate_unsorted_index(self, ascending, expected_values):
expected = Series(data=expected_values, index=expected_values, dtype=float)
tm.assert_series_equal(result, expected)

def test_interpolate_afreq_raises(self):
def test_interpolate_asfreq_raises(self):
ser = Series(["a", None, "b"], dtype=object)
msg2 = "Series.interpolate with object dtype"
msg = "Invalid fill method"
with pytest.raises(ValueError, match=msg):
ser.interpolate(method="asfreq")
with tm.assert_produces_warning(FutureWarning, match=msg2):
ser.interpolate(method="asfreq")