Skip to content

BUG/TST (string dtype): raise proper TypeError in interpolate #60637

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 4 commits into from
Jan 3, 2025
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
2 changes: 1 addition & 1 deletion pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2160,7 +2160,7 @@ def interpolate(
"""
# NB: we return type(self) even if copy=False
if not self.dtype._is_numeric:
raise ValueError("Values must be numeric.")
raise TypeError(f"Cannot interpolate with {self.dtype} dtype")

if (
not pa_version_under13p0
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
4 changes: 3 additions & 1 deletion pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3451,7 +3451,9 @@ def test_string_to_datetime_parsing_cast():
)
def test_interpolate_not_numeric(data):
if not data.dtype._is_numeric:
with pytest.raises(ValueError, match="Values must be 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()


Expand Down
13 changes: 5 additions & 8 deletions pandas/tests/frame/methods/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ def test_interpolate_inplace(self, frame_or_series, 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):
def test_interp_basic(self, using_infer_string):
df = DataFrame(
{
"A": [1, 2, np.nan, 4],
Expand All @@ -77,7 +73,8 @@ def test_interp_basic(self):
"D": list("abcd"),
}
)
msg = "DataFrame cannot interpolate with object dtype"
dtype = "str" if using_infer_string else "object"
msg = f"[Cc]annot interpolate with {dtype} dtype"
with pytest.raises(TypeError, match=msg):
df.interpolate()

Expand All @@ -87,8 +84,8 @@ def test_interp_basic(self):
df.interpolate(inplace=True)

# 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