Skip to content

DataFrame.reindex() with MultiIndex doesn't fill missing values #21147

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
holymonson opened this issue May 21, 2018 · 3 comments
Closed

DataFrame.reindex() with MultiIndex doesn't fill missing values #21147

holymonson opened this issue May 21, 2018 · 3 comments
Labels
Duplicate Report Duplicate issue or pull request Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex

Comments

@holymonson
Copy link
Contributor

Code Sample, a copy-pastable example if possible

pd.DataFrame([-1., -2.], [['a', 'a'], [1, 2]]).reindex([1, 2, 3], level=1)
#        0
# a 1 -1.0
#   2 -2.0

Problem description

DataFrame.reindex() with level specified doesn't fill missing values.

Expected Output

#      0
# a 1 -1.0
#   2 -2.0
#   3  NaN

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Darwin
OS-release: 17.5.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: zh_CN.UTF-8
LOCALE: zh_CN.UTF-8

pandas: 0.23.0
pytest: None
pip: 10.0.1
setuptools: 39.1.0
Cython: None
numpy: 1.14.3
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: None
sphinx: 1.6.2
patsy: None
dateutil: 2.7.2
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: 1.1.0
xlwt: None
xlsxwriter: 1.0.5
lxml: None
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@gfyoung gfyoung added Indexing Related to indexing on series/frames, not to indexes themselves API Design MultiIndex Compat pandas objects compatability with Numpy or Python functions and removed API Design labels May 21, 2018
@gfyoung
Copy link
Member

gfyoung commented May 21, 2018

Indeed, such a behavior would work just fine with a single Index, but strangely not with MultiIndex. Patch and PR are more than welcome!

@holymonson
Copy link
Contributor Author

idx, idxer = pd.Index(range(3)).reindex([0, 1, 4])
idx, idxer
# (Int64Index([0, 1, 4], dtype='int64'), array([ 0,  1, -1]))
idx.size
# 3

idx, idxer = pd.MultiIndex.from_arrays([['a']*3, range(3)]).reindex([0, 1, 4], level=1)
idx, idxer
# (MultiIndex(levels=[['a'], [0, 1, 4]],
#            labels=[[0, 0], [0, 1]]), array([0, 1]))
idx.size
# 2

In reindexing, Int64Index will be replace with the original target, while MultiIndex with the masked intersection only. Is it designed to be a special implement for MultiIndex.reindex() other than Index.reindex()? Seems no docs for that.

else: # sort the leaves
mask = new_lev_labels != -1
mask_all = mask.all()
if not mask_all:
new_labels = [lab[mask] for lab in new_labels]

@jorisvandenbossche jorisvandenbossche removed the Compat pandas objects compatability with Numpy or Python functions label Jun 21, 2018
@jorisvandenbossche
Copy link
Member

@holymonson Thanks for the report. Going to close this in favor of:

But feel free to chime in on on those issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

3 participants