Skip to content

Passing level to reindex on a multi-index doesn't reindex the desired level #25460

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
iaindillingham opened this issue Feb 27, 2019 · 6 comments
Labels
Bug Duplicate Report Duplicate issue or pull request Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex

Comments

@iaindillingham
Copy link

Code Sample

import numpy as np
import pandas as pd

category_idx = pd.Index(['A', 'B'])
date_idx = pd.date_range('2018-01', '2018-02', freq='MS')
idx = pd.MultiIndex.from_product([category_idx, date_idx], names=['category', 'date'])

series = pd.Series(np.random.randn(len(category_idx) * len(date_idx)), index=idx)
series
# category  date      
# A         2018-01-01   -0.060216
#           2018-02-01    0.757948
# B         2018-01-01    0.411784
#           2018-02-01   -0.062216
# dtype: float64

new_date_idx = date_idx.union([date_idx[-1] + date_idx.freq])
new_date_idx
# DatetimeIndex(['2018-01-01', '2018-02-01', '2018-03-01'], dtype='datetime64[ns]', freq='MS')

series.reindex(index=new_date_idx, level='date')
# Where are the NaNs?
# category  date      
# A         2018-01-01   -0.060216
#           2018-02-01    0.757948
# B         2018-01-01    0.411784
#           2018-02-01   -0.062216
# dtype: float64

series.loc['A'].reindex(index=new_date_idx)
# 2018-01-01   -0.060216
# 2018-02-01    0.757948
# 2018-03-01         NaN
# Freq: MS, dtype: float64

Problem description

Passing level to reindex on a multi-index doesn't reindex the desired level.

Expected Output

In the above example, I'd expect to see two new rows, each containing NaN. The expected output is analogous to calling reindex on a single-level index.

I originally posted this issue as a question to StackOverflow, where a user kindly posted a workaround: https://stackoverflow.com/questions/54904974/extending-a-datetime-index-within-a-multi-index.

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.6.final.0 python-bits: 64 OS: Linux OS-release: 4.15.0-45-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_GB.UTF-8 LOCALE: en_GB.UTF-8

pandas: 0.24.1
pytest: None
pip: 19.0.3
setuptools: 40.4.3
Cython: None
numpy: 1.15.3
scipy: 1.2.0
pyarrow: 0.12.1
xarray: None
IPython: 7.3.0
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.6
blosc: None
bottleneck: 1.2.1
tables: None
numexpr: 2.6.8
feather: None
matplotlib: 3.0.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: 4.2.5
bs4: 4.7.1
html5lib: None
sqlalchemy: 1.2.12
pymysql: 0.9.2
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: 0.7.0
gcsfs: None

@gfyoung gfyoung added Datetime Datetime data dtype Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex labels Mar 1, 2019
@gfyoung
Copy link
Member

gfyoung commented Mar 1, 2019

cc @toobaz

@dmvianna
Copy link

dmvianna commented May 16, 2019

Present in 0.23.4

It works for an Index (new row created)

pd.DataFrame(dict(a=[1,2],b=[2,3]), index=[1,2]).reindex([1,2,3])

image

But not for a MultiIndex (new row is not created)

a = pd.DataFrame(dict(a=[1,2],b=[2,3]), index=[[1,2],[3,4]])
a.index.names = ['a','b']
a.reindex([1,2,3], level='a')

image

This affects joins too. Right joins of a MultiIndex to an Index will have the same effect as an inner join. Could be related.

@toobaz toobaz added Index Related to the Index class or subclasses and removed Indexing Related to indexing on series/frames, not to indexes themselves labels Jun 28, 2019
@mroeschke mroeschke added the Bug label Apr 1, 2020
@mroeschke mroeschke removed the Datetime Datetime data dtype label May 5, 2020
@simonjayhawkins simonjayhawkins removed the Index Related to the Index class or subclasses label Jun 5, 2020
@simonjayhawkins simonjayhawkins added the Indexing Related to indexing on series/frames, not to indexes themselves label Jul 24, 2020
@ybagdasa
Copy link

ybagdasa commented Jun 1, 2022

@simonjayhawkins Has this issue been resolved? I'm just encountering it in pandas 1.4.2.

@jreback
Copy link
Contributor

jreback commented Jun 1, 2022

it's an open issue - you or anyone in the community can submit a patch

@ybagdasa
Copy link

ybagdasa commented Jun 1, 2022

I'm spent a few hours looking into the issue, but I am not familiar enough with index joins to know what the problem is. I think it might be happening in the function Index._join_level found in pandas/core/indexes/base.py.

Within the function new_level, left_lev_indexer, right_lev_indexer = old_level.join( right, how=how, return_indexers=True )
is called. The variable how is a right join. Not sure whether the outputs are as expected here. new_level seems to be correct.

Finally near the end of the function
join_index = MultiIndex( levels=new_levels, codes=new_codes, names=left.names, verify_integrity=False, )
is called. The new_codes appear to be the same as the old multiindex codes. I wonder if this is the issue.

Hope that helps someone willing to fix it.

@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone Jun 2, 2022
@simonjayhawkins
Copy link
Member

Thanks @ybagdasa for the investigation. Given this issue is a duplicate of #21147, which was itself a duplicate and closed. so closing this as duplicate of #12319 for the same reasons #21147 (comment) to help keep all the discussion in one place. (as there is also overlap with #7895)

feel free to add your comments to those issues.

@simonjayhawkins simonjayhawkins added the Duplicate Report Duplicate issue or pull request label Jun 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Projects
None yet
Development

No branches or pull requests

8 participants