Skip to content

#51826_Fixed bug when np.nan is used as index value with .reindex on … #54500

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

4 changes: 4 additions & 0 deletions pandas/_libs/intervaltree.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ 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
17 changes: 17 additions & 0 deletions pandas/tests/indexing/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,20 @@ 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.parametrize(
"base, expected_result",
[
(10, Series([np.nan, 0], index=[np.nan, 1.0], dtype=np.float64)),
(100, Series([np.nan, 0], index=[np.nan, 1.0], dtype=np.float64)),
(101, Series([np.nan, 0], index=[np.nan, 1.0], dtype=np.float64)),
(1010, Series([np.nan, 0], index=[np.nan, 1.0], dtype=np.float64)),
],
)
def test_interval_index_reindex_behavior(self, base, expected_result):
# GH 51826
left = np.arange(base, dtype=np.int32)
Copy link
Contributor

Choose a reason for hiding this comment

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

remove the dtypes from both of these

right = np.arange(1, base + 1, dtype=np.int32)
d = Series(range(base), index=IntervalIndex.from_arrays(left, right))
result = d.reindex(index=[np.nan, 1.0])
tm.assert_series_equal(result, expected_result)