Skip to content

BUG: unexpected value returned when .reindexing on a pd.Series with a pd.IntervalIndex when using np.nan as an index value #51826

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

Closed
2 of 3 tasks
benlawson opened this issue Mar 7, 2023 · 1 comment · Fixed by #54549
Assignees
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@benlawson
Copy link

benlawson commented Mar 7, 2023

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

>>> import pandas as pd
>>> import numpy as np

>>> # expected behavior, currently working
>>> base = 100
>>> d = pd.Series(range(base), index=pd.IntervalIndex.from_arrays(range(base), range(1, base+1)))
>>> d.reindex(index=[np.nan, 1])
NaN    NaN
1.0    0.0
dtype: float64


>>> # unexpected behavior
>>> base = 101
>>> d = pd.Series(range(base), index=pd.IntervalIndex.from_arrays(range(base), range(1, base+1)))
>>> d.reindex(index=[np.nan, 1])
NaN    49
1.0     0
dtype: int64


>>> # varying size of index
>>> values = []
>>> for base in range(1, 200):
   ...:      d = pd.Series(range(base), index=pd.IntervalIndex.from_arrays(range(base), range(1, base+1)))
   ...:      value = d.reindex(index=[np.nan])
   ...: 
   ...:      values.append(value.iloc[0])
>>> print(values)
[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 49, 50, 50, 51, 51, 52, 52, 53, 53, 54, 54, 55, 55, 56, 56, 57, 57, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62, 63, 63, 64, 64, 65, 65, 66, 66, 67, 67, 68, 68, 69, 69, 70, 70, 71, 71, 72, 72, 73, 73, 74, 74, 75, 75, 76, 76, 77, 77, 78, 78, 79, 79, 80, 80, 81, 81, 82, 82, 83, 83, 84, 84, 85, 85, 86, 86, 87, 87, 88, 88, 89, 89, 90, 90, 91, 91, 92, 92, 93, 93, 94, 94, 95, 95, 96, 96, 97, 97, 98, 98]

Issue Description

I am using interval indices and using .reindex to index with missing labels (as suggested in the docs). However, it looks like if there are more than 100 rows in the index, when I .reindex with a missing key, i.e. np.nan, .reindex will return an unexpected non-null value. Since np.nan is not defined in the index, I would expect .reindex to return np.nan. But instead, it looks like value of a middle key is returned. When there are 100 rows or fewer, when I pass np.nan I get back what I expect, np.nan

Expected Behavior

>>> import pandas as pd
>>> import numpy as np

>>> # expected behavior, no change
>>> base = 100
>>> d = pd.Series(range(base), index=pd.IntervalIndex.from_arrays(range(base), range(1, base+1)))
>>> d.reindex(index=[np.nan, 1])
NaN    NaN
1.0    0.0
dtype: float64

>>> # unexpected behavior, changed to return `np.nan` when `np.nan` is passed in
>>> base = 101
>>> d = pd.Series(range(base), index=pd.IntervalIndex.from_arrays(range(base), range(1, base+1)))
>>> d.reindex(index=[np.nan, 1])
NaN    NaN
1.0    0.0
dtype: float64

>>> # varying size of index, changed to always return `np.nan` when `np.nan` is passed, regardless of the size of the index
>>> values = []
>>> for base in range(1, 200):
   ...:      d = pd.Series(range(base), index=pd.IntervalIndex.from_arrays(range(base), range(1, base+1)))
   ...:      value = d.reindex(index=[np.nan])
   ...: 
   ...:      values.append(value.iloc[0])
>>> print(values)
[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]

Installed Versions

INSTALLED VERSIONS

commit : 2e218d1
python : 3.10.9.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-197-generic
Version : #208-Ubuntu SMP Tue Nov 1 17:23:37 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.3
numpy : 1.24.2
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 65.6.3
pip : 23.0.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.11.0
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@benlawson benlawson added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 7, 2023
@benlawson benlawson changed the title BUG: unexpected value returned when .reindexing on a pd.IntervalIndex when using np.nan as an index value BUG: unexpected value returned when .reindexing on a pd.Series with a pd.IntervalIndex when using np.nan as an index value Mar 7, 2023
@DeaMariaLeon DeaMariaLeon added Indexing Related to indexing on series/frames, not to indexes themselves and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 25, 2023
@raj-thapa
Copy link
Contributor

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
3 participants