Skip to content

Commit d1b01d2

Browse files
rhshadrachgmcrocetti
authored andcommitted
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
1 parent 2f64aab commit d1b01d2

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

pandas/core/arrays/arrow/array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2160,7 +2160,7 @@ def interpolate(
21602160
"""
21612161
# NB: we return type(self) even if copy=False
21622162
if not self.dtype._is_numeric:
2163-
raise ValueError("Values must be numeric.")
2163+
raise TypeError(f"Cannot interpolate with {self.dtype} dtype")
21642164

21652165
if (
21662166
not pa_version_under13p0

pandas/core/arrays/numpy_.py

+3
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ def interpolate(
292292
See NDFrame.interpolate.__doc__.
293293
"""
294294
# NB: we return type(self) even if copy=False
295+
if not self.dtype._is_numeric:
296+
raise TypeError(f"Cannot interpolate with {self.dtype} dtype")
297+
295298
if not copy:
296299
out_data = self._ndarray
297300
else:

pandas/tests/extension/test_arrow.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -3451,7 +3451,9 @@ def test_string_to_datetime_parsing_cast():
34513451
)
34523452
def test_interpolate_not_numeric(data):
34533453
if not data.dtype._is_numeric:
3454-
with pytest.raises(ValueError, match="Values must be numeric."):
3454+
ser = pd.Series(data)
3455+
msg = re.escape(f"Cannot interpolate with {ser.dtype} dtype")
3456+
with pytest.raises(TypeError, match=msg):
34553457
pd.Series(data).interpolate()
34563458

34573459

pandas/tests/frame/methods/test_interpolate.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ def test_interpolate_inplace(self, frame_or_series, request):
6464
assert np.shares_memory(orig, obj.values)
6565
assert orig.squeeze()[1] == 1.5
6666

67-
# TODO(infer_string) raise proper TypeError in case of string dtype
68-
@pytest.mark.xfail(
69-
using_string_dtype(), reason="interpolate doesn't work for string"
70-
)
71-
def test_interp_basic(self):
67+
def test_interp_basic(self, using_infer_string):
7268
df = DataFrame(
7369
{
7470
"A": [1, 2, np.nan, 4],
@@ -77,7 +73,8 @@ def test_interp_basic(self):
7773
"D": list("abcd"),
7874
}
7975
)
80-
msg = "DataFrame cannot interpolate with object dtype"
76+
dtype = "str" if using_infer_string else "object"
77+
msg = f"[Cc]annot interpolate with {dtype} dtype"
8178
with pytest.raises(TypeError, match=msg):
8279
df.interpolate()
8380

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

8986
# check we DID operate inplace
90-
assert np.shares_memory(df["C"]._values, cvalues)
91-
assert np.shares_memory(df["D"]._values, dvalues)
87+
assert tm.shares_memory(df["C"]._values, cvalues)
88+
assert tm.shares_memory(df["D"]._values, dvalues)
9289

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

0 commit comments

Comments
 (0)