-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: fix Series.interpolate(method='index') with unsorted index #29943
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -277,7 +277,10 @@ def interpolate_1d( | |
inds = lib.maybe_convert_objects(inds) | ||
else: | ||
inds = xvalues | ||
result[invalid] = np.interp(inds[invalid], inds[valid], yvalues[valid]) | ||
seq = np.argsort(inds[valid]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a comment here on what you are doing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. call this |
||
result[invalid] = np.interp( | ||
inds[invalid], inds[valid][seq], yvalues[valid][seq] | ||
) | ||
result[preserve_nans] = np.nan | ||
return result | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1655,3 +1655,10 @@ def test_interpolate_timedelta_index(self, interp_methods_ind): | |
pytest.skip( | ||
"This interpolation method is not supported for Timedelta Index yet." | ||
) | ||
|
||
def test_interpolate_unsorted_index(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add all of the examples from the OP. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can likely parameterize this test |
||
# GH 21037 | ||
ts = pd.Series(data=[10, 9, np.nan, 2, 1], index=[10, 9, 3, 2, 1]) | ||
result = ts.interpolate(method="index").sort_index(ascending=True) | ||
expected = ts.sort_index(ascending=True).interpolate(method="index") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you hard-code the expected result (e.g. don't use interpolate) |
||
tm.assert_series_equal(result, expected) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
method=`index`
with an unsorted index, would previously return incorrect results.