Skip to content

CLN: move raising TypeError for interpolate with object dtype to Block #58083

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
Show file tree
Hide file tree
Changes from 3 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
5 changes: 0 additions & 5 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7749,11 +7749,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
5 changes: 3 additions & 2 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1388,12 +1388,13 @@ 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)]
raise TypeError(
f"{type(self).__name__} cannot interpolate with object dtype."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we dont need to have Block in the exception message. the comments in the line above can be removed. can you add "# GH#53631"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can do something like name = {1: "Series", 2: "DataFrame"}[self.ndim] to keep the exception message unchanged

Copy link
Contributor Author

@natmokval natmokval Mar 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you, I made these changes, ci - green.

)

copy, refs = self._get_refs_and_copy(inplace)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/copy_view/test_interp_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_interp_fill_functions_inplace(func, dtype):
def test_interpolate_cannot_with_object_dtype():
df = DataFrame({"a": ["a", np.nan, "c"], "b": 1})

msg = "DataFrame cannot interpolate with object dtype"
msg = ".* cannot interpolate with object dtype"
with pytest.raises(TypeError, match=msg):
df.interpolate()

Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/frame/methods/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_interp_basic(self):
"D": list("abcd"),
}
)
msg = "DataFrame cannot interpolate with object dtype"
msg = ".* cannot interpolate with object dtype"
with pytest.raises(TypeError, match=msg):
df.interpolate()

Expand All @@ -102,7 +102,7 @@ def test_interp_basic_with_non_range_index(self, using_infer_string):
}
)

msg = "DataFrame cannot interpolate with object dtype"
msg = ".* cannot interpolate with object dtype"
if not using_infer_string:
with pytest.raises(TypeError, match=msg):
df.set_index("C").interpolate()
Expand Down Expand Up @@ -296,14 +296,14 @@ def test_interp_raise_on_only_mixed(self, axis):
"E": [1, 2, 3, 4],
}
)
msg = "DataFrame cannot interpolate with object dtype"
msg = ".* cannot interpolate with object dtype"
with pytest.raises(TypeError, match=msg):
df.astype("object").interpolate(axis=axis)

def test_interp_raise_on_all_object_dtype(self):
# GH 22985
df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}, dtype="object")
msg = "DataFrame cannot interpolate with object dtype"
msg = ".* cannot interpolate with object dtype"
with pytest.raises(TypeError, match=msg):
df.interpolate()

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