Skip to content

BUG: interpolation with duplicate index values fails (#42585) #43039

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
wants to merge 2 commits into from
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
2 changes: 1 addition & 1 deletion pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def _interpolate_1d(
if method in NP_METHODS:
# np.interp requires sorted X values, #21037

indexer = np.argsort(indices[valid])
indexer = np.argsort(indices[valid], kind="stable")
result[invalid] = np.interp(
indices[invalid], indices[valid][indexer], yvalues[valid][indexer]
)
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/series/methods/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,23 @@ def test_interpolate_unsorted_index(self, ascending, expected_values):
expected = Series(data=expected_values, index=expected_values, dtype=float)
tm.assert_series_equal(result, expected)

def test_interpolate_duplicate_index_decreasing(self):
# GH 42585
ts = Series(data=[-200, -50, np.nan, -50, 1000], index=[79, 79, 0, -61, -2783])
result = ts.interpolate(method="index")
assert result.loc[0] == -50
# TODO: also check validity of sort assumption

def test_interpolate_duplicate_index_increasing(self):
# GH 42585
ts = Series(
data=[0, 0, 0, 0, 0, 0, 0, -50, np.nan, -50, 0, 0, 0, 0, 0, 0, 0, 0],
index=[1, 1, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 7, 8, 8, 9, 10, 11],
)
result = ts.interpolate(method="index")
assert result.loc[0] == -50
# TODO: check validity of sort assumption?

def test_interpolate_pos_args_deprecation(self):
# https://github.com/pandas-dev/pandas/issues/41485
ser = Series([1, 2, 3])
Expand Down