Skip to content

Commit

Permalink
BUG/TST (string dtype): raise proper TypeError in interpolate (#60637)
Browse files Browse the repository at this point in the history
* TST(string dtype): Resolve xfail for interpolate

* Adjust arrow tests

* Fixup for NumPyExtensionArray

* Use tm.shares_memory
  • Loading branch information
rhshadrach authored Jan 3, 2025
1 parent 228627a commit 5e50d3f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
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 @@ -292,6 +292,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

0 comments on commit 5e50d3f

Please sign in to comment.