Skip to content

[backport 2.3.x] BUG/TST (string dtype): raise proper TypeError in interpolate (#60637) #60652

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
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
3 changes: 3 additions & 0 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2150,6 +2150,9 @@ def interpolate(
See NDFrame.interpolate.__doc__.
"""
# NB: we return type(self) even if copy=False
if not self.dtype._is_numeric:
raise TypeError(f"Cannot interpolate with {self.dtype} dtype")

mask = self.isna()
if self.dtype.kind == "f":
data = self._pa_array.to_numpy()
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ def interpolate(
See NDFrame.interpolate.__doc__.
"""
# NB: we return type(self) even if copy=False
if not self.dtype._is_numeric:
raise TypeError(f"Cannot interpolate with {self.dtype} dtype")

if not copy:
out_data = self._ndarray
else:
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3350,6 +3350,17 @@ def test_string_to_datetime_parsing_cast():
tm.assert_series_equal(result, expected)


@pytest.mark.skipif(
pa_version_under13p0, reason="pairwise_diff_checked not implemented in pyarrow"
)
def test_interpolate_not_numeric(data):
if not data.dtype._is_numeric:
ser = pd.Series(data)
msg = re.escape(f"Cannot interpolate with {ser.dtype} dtype")
with pytest.raises(TypeError, match=msg):
pd.Series(data).interpolate()


def test_string_to_time_parsing_cast():
# GH 56463
string_times = ["11:41:43.076160"]
Expand Down
17 changes: 10 additions & 7 deletions pandas/tests/frame/methods/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@ def test_interpolate_inplace(self, frame_or_series, using_array_manager, request
assert np.shares_memory(orig, obj.values)
assert orig.squeeze()[1] == 1.5

# TODO(infer_string) raise proper TypeError in case of string dtype
@pytest.mark.xfail(
using_string_dtype(), reason="interpolate doesn't work for string"
)
def test_interp_basic(self, using_copy_on_write):
def test_interp_basic(self, using_copy_on_write, using_infer_string):
df = DataFrame(
{
"A": [1, 2, np.nan, 4],
Expand All @@ -90,6 +86,13 @@ def test_interp_basic(self, using_copy_on_write):
"D": list("abcd"),
}
)
if using_infer_string:
dtype = "str" if using_infer_string else "object"
msg = f"[Cc]annot interpolate with {dtype} dtype"
with pytest.raises(TypeError, match=msg):
df.interpolate()
return

msg = "DataFrame.interpolate with object dtype"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df.interpolate()
Expand All @@ -111,8 +114,8 @@ def test_interp_basic(self, using_copy_on_write):
tm.assert_frame_equal(df, expected)

# check we DID operate inplace
assert np.shares_memory(df["C"]._values, cvalues)
assert np.shares_memory(df["D"]._values, dvalues)
assert tm.shares_memory(df["C"]._values, cvalues)
assert tm.shares_memory(df["D"]._values, dvalues)

@pytest.mark.xfail(
using_string_dtype(), reason="interpolate doesn't work for string"
Expand Down
Loading