Skip to content

Commit 67b19eb

Browse files
authored
Fixed unexpected np.nan value with reindex on pd.series with pd.Inter… (#54549)
* Fixed unexpected np.nan value with reindex on pd.series with pd.IntervalIndex * Moving expected result to test body * Fixed issues with Linux-32-bit test
1 parent 934eebb commit 67b19eb

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

pandas/_libs/intervaltree.pxi.in

+5
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,11 @@ cdef class {{dtype_title}}Closed{{closed_title}}IntervalNode(IntervalNode):
391391
"""Recursively query this node and its sub-nodes for intervals that
392392
overlap with the query point.
393393
"""
394+
395+
# GH 51826: ensures nan is handled properly during reindexing
396+
if np.isnan(point):
397+
return
398+
394399
cdef:
395400
int64_t[:] indices
396401
{{dtype}}_t[:] values

pandas/tests/indexing/interval/test_interval.py

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import numpy as np
22
import pytest
33

4+
from pandas.compat import IS64
5+
46
import pandas as pd
57
from pandas import (
68
DataFrame,
@@ -176,3 +178,19 @@ def test_mi_intervalindex_slicing_with_scalar(self):
176178
)
177179
expected = Series([1, 6, 2, 8, 7], index=expected_index, name="value")
178180
tm.assert_series_equal(result, expected)
181+
182+
@pytest.mark.xfail(not IS64, reason="GH 23440")
183+
@pytest.mark.parametrize(
184+
"base",
185+
[101, 1010],
186+
)
187+
def test_reindex_behavior_with_interval_index(self, base):
188+
# GH 51826
189+
190+
ser = Series(
191+
range(base),
192+
index=IntervalIndex.from_arrays(range(base), range(1, base + 1)),
193+
)
194+
expected_result = Series([np.nan, 0], index=[np.nan, 1.0], dtype=float)
195+
result = ser.reindex(index=[np.nan, 1.0])
196+
tm.assert_series_equal(result, expected_result)

0 commit comments

Comments
 (0)