Skip to content

Fixed unexpected np.nan value with reindex on pd.series with pd.Inter… #54549

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 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
5 changes: 5 additions & 0 deletions pandas/_libs/intervaltree.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ cdef class {{dtype_title}}Closed{{closed_title}}IntervalNode(IntervalNode):
"""Recursively query this node and its sub-nodes for intervals that
overlap with the query point.
"""

# GH 51826: ensures nan is handled properly during reindexing
if np.isnan(point):
return

cdef:
int64_t[:] indices
{{dtype}}_t[:] values
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/indexing/interval/test_interval.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
import pytest

from pandas.compat import IS64

import pandas as pd
from pandas import (
DataFrame,
Expand Down Expand Up @@ -172,3 +174,19 @@ def test_mi_intervalindex_slicing_with_scalar(self):
)
expected = Series([1, 6, 2, 8, 7], index=expected_index, name="value")
tm.assert_series_equal(result, expected)

@pytest.mark.xfail(not IS64, reason="GH 23440")
@pytest.mark.parametrize(
"base",
[101, 1010],
)
def test_reindex_behavior_with_interval_index(self, base):
# GH 51826

ser = Series(
range(base),
index=IntervalIndex.from_arrays(range(base), range(1, base + 1)),
)
expected_result = Series([np.nan, 0], index=[np.nan, 1.0], dtype=float)
result = ser.reindex(index=[np.nan, 1.0])
tm.assert_series_equal(result, expected_result)