Skip to content

Commit

Permalink
CLN: move raising TypeError for interpolate with object dtype to …
Browse files Browse the repository at this point in the history
…Block (pandas-dev#58083)

* move raising for interpolate with object dtype from NDFrame to Block

* correct msg in test_interpolate_cannot_with_object_dtype

* correct the exception message and the comments
  • Loading branch information
natmokval authored Apr 1, 2024
1 parent 19aaf40 commit 33af3b8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 15 deletions.
5 changes: 0 additions & 5 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7718,11 +7718,6 @@ def interpolate(
raise ValueError("'method' should be a string, not None.")

obj, should_transpose = (self.T, True) if axis == 1 else (self, False)
# GH#53631
if np.any(obj.dtypes == object):
raise TypeError(
f"{type(self).__name__} cannot interpolate with object dtype."
)

if isinstance(obj.index, MultiIndex) and method != "linear":
raise ValueError(
Expand Down
8 changes: 3 additions & 5 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1388,12 +1388,10 @@ def interpolate(
# If there are no NAs, then interpolate is a no-op
return [self.copy(deep=False)]

# TODO(3.0): this case will not be reachable once GH#53638 is enforced
if self.dtype == _dtype_obj:
# only deal with floats
# bc we already checked that can_hold_na, we don't have int dtype here
# test_interp_basic checks that we make a copy here
return [self.copy(deep=False)]
# GH#53631
name = {1: "Series", 2: "DataFrame"}[self.ndim]
raise TypeError(f"{name} cannot interpolate with object dtype.")

copy, refs = self._get_refs_and_copy(inplace)

Expand Down
8 changes: 3 additions & 5 deletions pandas/tests/series/methods/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,11 +790,9 @@ def test_interpolate_unsorted_index(self, ascending, expected_values):

def test_interpolate_asfreq_raises(self):
ser = Series(["a", None, "b"], dtype=object)
msg2 = "Series cannot interpolate with object dtype"
msg = "Invalid fill method"
with pytest.raises(TypeError, match=msg2):
with pytest.raises(ValueError, match=msg):
ser.interpolate(method="asfreq")
msg = "Can not interpolate with method=asfreq"
with pytest.raises(ValueError, match=msg):
ser.interpolate(method="asfreq")

def test_interpolate_fill_value(self):
# GH#54920
Expand Down

0 comments on commit 33af3b8

Please sign in to comment.