-
-
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
BUG: fix Series.interpolate(method='index') with unsorted index #29943
Conversation
pandas/tests/series/test_missing.py
Outdated
@@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
you can likely parameterize this test
pandas/tests/series/test_missing.py
Outdated
# 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 comment
The 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)
pandas/core/missing.py
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a comment here on what you are doing
pandas/core/missing.py
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
call this indexer
doc/source/whatsnew/v1.0.0.rst
Outdated
@@ -550,6 +550,7 @@ Numeric | |||
- Bug in :class:`UInt64Index` precision loss while constructing from a list with values in the ``np.uint64`` range (:issue:`29526`) | |||
- Bug in :class:`NumericIndex` construction that caused indexing to fail when integers in the ``np.uint64`` range were used (:issue:`28023`) | |||
- Bug in :class:`NumericIndex` construction that caused :class:`UInt64Index` to be casted to :class:`Float64Index` when integers in the ``np.uint64`` range were used to index a :class:`DataFrame` (:issue:`28279`) | |||
- Bug in :meth:`Series.interpolate` when using method ``index`` with unsorted index. (:issue:`21037`) |
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.
Hi @jreback, all done, please have a look. |
lgtm. @TomAugspurger if any comments |
Thanks! |
…as-dev#29943) * fix series interpolate bug with unsorted index GH21037
…as-dev#29943) * fix series interpolate bug with unsorted index GH21037
Series.interpolate(method='index')
use numpy.interp under the hood, which cannot work with unsorted X values. Here we sort the known data fornumpy.interp
.black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff