Skip to content

Commit cea06cd

Browse files
jbrockmendelim-vinicius
authored and
im-vinicius
committed
DEPR: interpolate with object dtype (pandas-dev#53638)
1 parent ea64c4a commit cea06cd

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ Deprecations
280280
- 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`)
281281
- Deprecated :func:`value_counts`, use ``pd.Series(obj).value_counts()`` instead (:issue:`47862`)
282282
- Deprecated :meth:`Series.first` and :meth:`DataFrame.first` (please create a mask and filter using ``.loc`` instead) (:issue:`45908`)
283+
- Deprecated :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` for object-dtype (:issue:`53631`)
283284
- 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`)
284285
- 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`)
285286
- 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`)

pandas/core/generic.py

+12
Original file line numberDiff line numberDiff line change
@@ -7882,6 +7882,18 @@ def interpolate(
78827882
FutureWarning,
78837883
stacklevel=find_stack_level(),
78847884
)
7885+
elif np.any(obj.dtypes == object):
7886+
# GH#53631
7887+
if not (obj.ndim == 2 and np.all(obj.dtypes == object)):
7888+
# don't warn in cases that already raise
7889+
warnings.warn(
7890+
f"{type(self).__name__}.interpolate with object dtype is "
7891+
"deprecated and will raise in a future version. Call "
7892+
"obj.infer_objects(copy=False) before interpolating instead.",
7893+
FutureWarning,
7894+
stacklevel=find_stack_level(),
7895+
)
7896+
78857897
if method not in fillna_methods:
78867898
axis = self._info_axis_number
78877899

pandas/tests/copy_view/test_interp_fillna.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ def test_interpolate_cleaned_fill_method(using_copy_on_write):
113113
df = DataFrame({"a": ["a", np.nan, "c"], "b": 1})
114114
df_orig = df.copy()
115115

116-
result = df.interpolate(method="linear")
116+
msg = "DataFrame.interpolate with object dtype"
117+
with tm.assert_produces_warning(FutureWarning, match=msg):
118+
result = df.interpolate(method="linear")
117119

118120
if using_copy_on_write:
119121
assert np.shares_memory(get_array(result, "a"), get_array(df, "a"))

pandas/tests/frame/methods/test_interpolate.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ def test_interp_basic(self, using_copy_on_write):
6969
"D": list("abcd"),
7070
}
7171
)
72-
result = df.interpolate()
72+
msg = "DataFrame.interpolate with object dtype"
73+
with tm.assert_produces_warning(FutureWarning, match=msg):
74+
result = df.interpolate()
7375
tm.assert_frame_equal(result, expected)
7476

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

85-
res = df.interpolate(inplace=True)
87+
with tm.assert_produces_warning(FutureWarning, match=msg):
88+
res = df.interpolate(inplace=True)
8689
assert res is None
8790
tm.assert_frame_equal(df, expected)
8891

@@ -100,7 +103,9 @@ def test_interp_basic_with_non_range_index(self):
100103
}
101104
)
102105

103-
result = df.set_index("C").interpolate()
106+
msg = "DataFrame.interpolate with object dtype"
107+
with tm.assert_produces_warning(FutureWarning, match=msg):
108+
result = df.set_index("C").interpolate()
104109
expected = df.set_index("C")
105110
expected.loc[3, "A"] = 3
106111
expected.loc[5, "B"] = 9
@@ -120,7 +125,6 @@ def test_interp_bad_method(self):
120125
"A": [1, 2, np.nan, 4],
121126
"B": [1, 4, 9, np.nan],
122127
"C": [1, 2, 3, 5],
123-
"D": list("abcd"),
124128
}
125129
)
126130
msg = (

pandas/tests/series/methods/test_interpolate.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -846,8 +846,10 @@ def test_interpolate_unsorted_index(self, ascending, expected_values):
846846
expected = Series(data=expected_values, index=expected_values, dtype=float)
847847
tm.assert_series_equal(result, expected)
848848

849-
def test_interpolate_afreq_raises(self):
849+
def test_interpolate_asfreq_raises(self):
850850
ser = Series(["a", None, "b"], dtype=object)
851+
msg2 = "Series.interpolate with object dtype"
851852
msg = "Invalid fill method"
852853
with pytest.raises(ValueError, match=msg):
853-
ser.interpolate(method="asfreq")
854+
with tm.assert_produces_warning(FutureWarning, match=msg2):
855+
ser.interpolate(method="asfreq")

0 commit comments

Comments
 (0)