Skip to content

Commit d4e535e

Browse files
natmokvalpmhatre1
authored andcommitted
CLN: enforce deprecation of interpolate with object dtype (pandas-dev#57820)
* remove interpolate with object dtype * enforce deprecation interpolate with object dtype, correct tests * fix ruff error * add a note to v3.0.0 * combine two conditions * change blocks of if statements to avoid duplicate checks * replace err msg containing 'Try setting at least one column to a numeric dtype' * simplify if condition
1 parent 75fa607 commit d4e535e

File tree

5 files changed

+28
-69
lines changed

5 files changed

+28
-69
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Removal of prior version deprecations/changes
201201
- Changed the default value of ``observed`` in :meth:`DataFrame.groupby` and :meth:`Series.groupby` to ``True`` (:issue:`51811`)
202202
- Enforced deprecation disallowing parsing datetimes with mixed time zones unless user passes ``utc=True`` to :func:`to_datetime` (:issue:`57275`)
203203
- Enforced deprecation of :meth:`.DataFrameGroupBy.get_group` and :meth:`.SeriesGroupBy.get_group` allowing the ``name`` argument to be a non-tuple when grouping by a list of length 1 (:issue:`54155`)
204+
- Enforced deprecation of :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` for object-dtype (:issue:`57820`)
204205
- Enforced deprecation of ``axis=None`` acting the same as ``axis=0`` in the DataFrame reductions ``sum``, ``prod``, ``std``, ``var``, and ``sem``, passing ``axis=None`` will now reduce over both axes; this is particularly the case when doing e.g. ``numpy.sum(df)`` (:issue:`21597`)
205206
- Enforced deprecation of passing a dictionary to :meth:`SeriesGroupBy.agg` (:issue:`52268`)
206207
- Enforced deprecation of string ``AS`` denoting frequency in :class:`YearBegin` and strings ``AS-DEC``, ``AS-JAN``, etc. denoting annual frequencies with various fiscal year starts (:issue:`57793`)

pandas/core/generic.py

+4-17
Original file line numberDiff line numberDiff line change
@@ -7816,17 +7816,11 @@ def interpolate(
78167816
obj, should_transpose = self, False
78177817
else:
78187818
obj, should_transpose = (self.T, True) if axis == 1 else (self, False)
7819+
# GH#53631
78197820
if np.any(obj.dtypes == object):
7820-
# GH#53631
7821-
if not (obj.ndim == 2 and np.all(obj.dtypes == object)):
7822-
# don't warn in cases that already raise
7823-
warnings.warn(
7824-
f"{type(self).__name__}.interpolate with object dtype is "
7825-
"deprecated and will raise in a future version. Call "
7826-
"obj.infer_objects(copy=False) before interpolating instead.",
7827-
FutureWarning,
7828-
stacklevel=find_stack_level(),
7829-
)
7821+
raise TypeError(
7822+
f"{type(self).__name__} cannot interpolate with object dtype."
7823+
)
78307824

78317825
if method in fillna_methods and "fill_value" in kwargs:
78327826
raise ValueError(
@@ -7842,13 +7836,6 @@ def interpolate(
78427836

78437837
limit_direction = missing.infer_limit_direction(limit_direction, method)
78447838

7845-
if obj.ndim == 2 and np.all(obj.dtypes == object):
7846-
raise TypeError(
7847-
"Cannot interpolate with all object-dtype columns "
7848-
"in the DataFrame. Try setting at least one "
7849-
"column to a numeric dtype."
7850-
)
7851-
78527839
if method.lower() in fillna_methods:
78537840
# TODO(3.0): remove this case
78547841
# TODO: warn/raise on limit_direction or kwargs which are ignored?

pandas/tests/copy_view/test_interp_fillna.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,12 @@ def test_interp_fill_functions_inplace(func, dtype):
111111
assert view._mgr._has_no_reference(0)
112112

113113

114-
def test_interpolate_cleaned_fill_method():
115-
# Check that "method is set to None" case works correctly
114+
def test_interpolate_cannot_with_object_dtype():
116115
df = DataFrame({"a": ["a", np.nan, "c"], "b": 1})
117-
df_orig = df.copy()
118-
119-
msg = "DataFrame.interpolate with object dtype"
120-
with tm.assert_produces_warning(FutureWarning, match=msg):
121-
result = df.interpolate(method="linear")
122116

123-
assert np.shares_memory(get_array(result, "a"), get_array(df, "a"))
124-
result.iloc[0, 0] = Timestamp("2021-12-31")
125-
126-
assert not np.shares_memory(get_array(result, "a"), get_array(df, "a"))
127-
tm.assert_frame_equal(df, df_orig)
117+
msg = "DataFrame cannot interpolate with object dtype"
118+
with pytest.raises(TypeError, match=msg):
119+
df.interpolate()
128120

129121

130122
def test_interpolate_object_convert_no_op():

pandas/tests/frame/methods/test_interpolate.py

+16-37
Original file line numberDiff line numberDiff line change
@@ -76,29 +76,14 @@ def test_interp_basic(self):
7676
"D": list("abcd"),
7777
}
7878
)
79-
expected = DataFrame(
80-
{
81-
"A": [1.0, 2.0, 3.0, 4.0],
82-
"B": [1.0, 4.0, 9.0, 9.0],
83-
"C": [1, 2, 3, 5],
84-
"D": list("abcd"),
85-
}
86-
)
87-
msg = "DataFrame.interpolate with object dtype"
88-
with tm.assert_produces_warning(FutureWarning, match=msg):
89-
result = df.interpolate()
90-
tm.assert_frame_equal(result, expected)
79+
msg = "DataFrame cannot interpolate with object dtype"
80+
with pytest.raises(TypeError, match=msg):
81+
df.interpolate()
9182

92-
# check we didn't operate inplace GH#45791
9383
cvalues = df["C"]._values
9484
dvalues = df["D"].values
95-
assert np.shares_memory(cvalues, result["C"]._values)
96-
assert np.shares_memory(dvalues, result["D"]._values)
97-
98-
with tm.assert_produces_warning(FutureWarning, match=msg):
99-
res = df.interpolate(inplace=True)
100-
assert res is None
101-
tm.assert_frame_equal(df, expected)
85+
with pytest.raises(TypeError, match=msg):
86+
df.interpolate(inplace=True)
10287

10388
# check we DID operate inplace
10489
assert np.shares_memory(df["C"]._values, cvalues)
@@ -117,14 +102,16 @@ def test_interp_basic_with_non_range_index(self, using_infer_string):
117102
}
118103
)
119104

120-
msg = "DataFrame.interpolate with object dtype"
121-
warning = FutureWarning if not using_infer_string else None
122-
with tm.assert_produces_warning(warning, match=msg):
105+
msg = "DataFrame cannot interpolate with object dtype"
106+
if not using_infer_string:
107+
with pytest.raises(TypeError, match=msg):
108+
df.set_index("C").interpolate()
109+
else:
123110
result = df.set_index("C").interpolate()
124-
expected = df.set_index("C")
125-
expected.loc[3, "A"] = 3
126-
expected.loc[5, "B"] = 9
127-
tm.assert_frame_equal(result, expected)
111+
expected = df.set_index("C")
112+
expected.loc[3, "A"] = 3
113+
expected.loc[5, "B"] = 9
114+
tm.assert_frame_equal(result, expected)
128115

129116
def test_interp_empty(self):
130117
# https://github.com/pandas-dev/pandas/issues/35598
@@ -315,22 +302,14 @@ def test_interp_raise_on_only_mixed(self, axis):
315302
"E": [1, 2, 3, 4],
316303
}
317304
)
318-
msg = (
319-
"Cannot interpolate with all object-dtype columns "
320-
"in the DataFrame. Try setting at least one "
321-
"column to a numeric dtype."
322-
)
305+
msg = "DataFrame cannot interpolate with object dtype"
323306
with pytest.raises(TypeError, match=msg):
324307
df.astype("object").interpolate(axis=axis)
325308

326309
def test_interp_raise_on_all_object_dtype(self):
327310
# GH 22985
328311
df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}, dtype="object")
329-
msg = (
330-
"Cannot interpolate with all object-dtype columns "
331-
"in the DataFrame. Try setting at least one "
332-
"column to a numeric dtype."
333-
)
312+
msg = "DataFrame cannot interpolate with object dtype"
334313
with pytest.raises(TypeError, match=msg):
335314
df.interpolate()
336315

pandas/tests/series/methods/test_interpolate.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -846,10 +846,10 @@ def test_interpolate_unsorted_index(self, ascending, expected_values):
846846

847847
def test_interpolate_asfreq_raises(self):
848848
ser = Series(["a", None, "b"], dtype=object)
849-
msg2 = "Series.interpolate with object dtype"
849+
msg2 = "Series cannot interpolate with object dtype"
850850
msg = "Invalid fill method"
851-
with pytest.raises(ValueError, match=msg):
852-
with tm.assert_produces_warning(FutureWarning, match=msg2):
851+
with pytest.raises(TypeError, match=msg2):
852+
with pytest.raises(ValueError, match=msg):
853853
ser.interpolate(method="asfreq")
854854

855855
def test_interpolate_fill_value(self):

0 commit comments

Comments
 (0)