Skip to content

Commit d90edeb

Browse files
[backport 2.3.x] BUG/TST (string dtype): raise proper TypeError in interpolate (#60637) (#60652)
BUG/TST (string dtype): raise proper TypeError in interpolate (#60637) * TST(string dtype): Resolve xfail for interpolate * Adjust arrow tests * Fixup for NumPyExtensionArray * Use tm.shares_memory (cherry picked from commit 5e50d3f) Co-authored-by: Richard Shadrach <[email protected]>
1 parent e53967b commit d90edeb

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

Diff for: pandas/core/arrays/arrow/array.py

+3
Original file line numberDiff line numberDiff line change
@@ -2150,6 +2150,9 @@ def interpolate(
21502150
See NDFrame.interpolate.__doc__.
21512151
"""
21522152
# NB: we return type(self) even if copy=False
2153+
if not self.dtype._is_numeric:
2154+
raise TypeError(f"Cannot interpolate with {self.dtype} dtype")
2155+
21532156
mask = self.isna()
21542157
if self.dtype.kind == "f":
21552158
data = self._pa_array.to_numpy()

Diff for: pandas/core/arrays/numpy_.py

+3
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ def interpolate(
287287
See NDFrame.interpolate.__doc__.
288288
"""
289289
# NB: we return type(self) even if copy=False
290+
if not self.dtype._is_numeric:
291+
raise TypeError(f"Cannot interpolate with {self.dtype} dtype")
292+
290293
if not copy:
291294
out_data = self._ndarray
292295
else:

Diff for: pandas/tests/extension/test_arrow.py

+11
Original file line numberDiff line numberDiff line change
@@ -3350,6 +3350,17 @@ def test_string_to_datetime_parsing_cast():
33503350
tm.assert_series_equal(result, expected)
33513351

33523352

3353+
@pytest.mark.skipif(
3354+
pa_version_under13p0, reason="pairwise_diff_checked not implemented in pyarrow"
3355+
)
3356+
def test_interpolate_not_numeric(data):
3357+
if not data.dtype._is_numeric:
3358+
ser = pd.Series(data)
3359+
msg = re.escape(f"Cannot interpolate with {ser.dtype} dtype")
3360+
with pytest.raises(TypeError, match=msg):
3361+
pd.Series(data).interpolate()
3362+
3363+
33533364
def test_string_to_time_parsing_cast():
33543365
# GH 56463
33553366
string_times = ["11:41:43.076160"]

Diff for: pandas/tests/frame/methods/test_interpolate.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ def test_interpolate_inplace(self, frame_or_series, using_array_manager, request
6969
assert np.shares_memory(orig, obj.values)
7070
assert orig.squeeze()[1] == 1.5
7171

72-
# TODO(infer_string) raise proper TypeError in case of string dtype
73-
@pytest.mark.xfail(
74-
using_string_dtype(), reason="interpolate doesn't work for string"
75-
)
76-
def test_interp_basic(self, using_copy_on_write):
72+
def test_interp_basic(self, using_copy_on_write, using_infer_string):
7773
df = DataFrame(
7874
{
7975
"A": [1, 2, np.nan, 4],
@@ -90,6 +86,13 @@ def test_interp_basic(self, using_copy_on_write):
9086
"D": list("abcd"),
9187
}
9288
)
89+
if using_infer_string:
90+
dtype = "str" if using_infer_string else "object"
91+
msg = f"[Cc]annot interpolate with {dtype} dtype"
92+
with pytest.raises(TypeError, match=msg):
93+
df.interpolate()
94+
return
95+
9396
msg = "DataFrame.interpolate with object dtype"
9497
with tm.assert_produces_warning(FutureWarning, match=msg):
9598
result = df.interpolate()
@@ -111,8 +114,8 @@ def test_interp_basic(self, using_copy_on_write):
111114
tm.assert_frame_equal(df, expected)
112115

113116
# check we DID operate inplace
114-
assert np.shares_memory(df["C"]._values, cvalues)
115-
assert np.shares_memory(df["D"]._values, dvalues)
117+
assert tm.shares_memory(df["C"]._values, cvalues)
118+
assert tm.shares_memory(df["D"]._values, dvalues)
116119

117120
@pytest.mark.xfail(
118121
using_string_dtype(), reason="interpolate doesn't work for string"

0 commit comments

Comments
 (0)