Skip to content

Interpolation nonnumeric error handling #21773

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

Closed
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
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ Documentation Changes
Bug Fixes
~~~~~~~~~

**Data-type specific**

- Bug in :meth:`Series.interpolate()` where appropiate error was not raised when interpolating with a non-numeric index column (:issue:`21662`)

Categorical
^^^^^^^^^^^

Expand Down
7 changes: 7 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6024,6 +6024,13 @@ def interpolate(self, method='linear', axis=0, limit=None, inplace=False,
raise NotImplementedError("Interpolation with NaNs in the index "
"has not been implemented. Try filling "
"those NaNs before interpolating.")

if not is_numeric_dtype(index):
raise ValueError("Index column must be numeric when using any "
Copy link
Contributor

Choose a reason for hiding this comment

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

you have linting issues here

"interpolation method other than linear. Try "
"setting a numeric index column before "
"interpolating.")

data = _maybe_transposed_self._data
new_data = data.interpolate(method=method, axis=ax, index=index,
values=_maybe_transposed_self, limit=limit,
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/generic/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,3 +1008,20 @@ def test_pipe_panel(self):

with pytest.raises(ValueError):
result = wp.pipe((f, 'y'), x=1, y=1)

Copy link
Contributor

Choose a reason for hiding this comment

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

this needs to go in pandas/tests/series/test_missing.py

also some tests are failing

def test_interpolate(self):
# GH 21662
# Using a numeric index column
df = DataFrame([0, 1, np.nan, 3], index=[1, 2, 3, 4])
series = Series(df[0])
assert series.interpolate(
method="quadratic").equals(series.interpolate(method="linear"))

# Using a non-numeric index column
df = DataFrame([0, 1, np.nan, 3], index=["A", "B", "C", "D"])
series = Series(df[0])
with pytest.raises(ValueError, match="Index column must be numeric "
"when using any interpolation method other than linear. Try "
"setting a numeric index column before interpolating."):

result = series.interpolate(method="quadratic")