Skip to content

reindex() inconsistency between Index and MultiIndex #21247

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
matthewgilbert opened this issue May 29, 2018 · 2 comments · Fixed by #42528
Closed

reindex() inconsistency between Index and MultiIndex #21247

matthewgilbert opened this issue May 29, 2018 · 2 comments · Fixed by #42528
Assignees
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@matthewgilbert
Copy link
Contributor

This seems related but distinct from #7895

import pandas as pd
from pandas import Timestamp as TS

df = pd.DataFrame([1, 2, 3], index=[0, 2, 4])
 # throws an error as expected
df.reindex([0, 1, 2, 3, 4, 5, 6], fill_value=0, limit=1)
ValueError: limit argument only valid if doing pad, backfill or nearest reindexing
idx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'A'),
                                 (TS('2015-01-04'), 'A'),
                                 (TS('2015-01-04'), 'B')])
df2 = pd.Series([0.02, 0.01, 0.012], index=idx)

widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'A'),
                                  (TS('2015-01-03'), 'B'),
                                  (TS('2015-01-04'), 'A'),
                                  (TS('2015-01-04'), 'B'),
                                  (TS('2015-01-04'), 'C'),
                                  (TS('2015-01-05'), 'B'),
                                  (TS('2015-01-05'), 'C'),
                                  (TS('2015-01-06'), 'B'),
                                  (TS('2015-01-06'), 'C')])
df2.reindex(widx, fill_value=0, limit=1)
2015-01-03  A    0.020
            B    0.000
2015-01-04  A    0.010
            B    0.012
            C    0.000
2015-01-05  B    0.000
            C    0.000
2015-01-06  B    0.000
            C    0.000
dtype: float64

Problem description

The behaviour illustrated above of reindex on a MultiIndex appears to be a bug to me. I would not expect reindex to support limit with a fill_value for a MultiIndex but not an Index, and in addition it appears like this limit is accepted but ignored for a MultiIndex reindex.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.0.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-22-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_CA.UTF-8
LOCALE: en_CA.UTF-8

pandas: 0.23.0
pytest: 3.0.5
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.13.1
scipy: 0.19.1
pyarrow: 0.8.0
xarray: None
IPython: 5.1.0
sphinx: 1.5.4
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.2
feather: None
matplotlib: 2.0.2
openpyxl: 2.4.1
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 3.7.2
bs4: 4.5.3
html5lib: 0.9999999
sqlalchemy: 1.1.5
pymysql: None
psycopg2: None
jinja2: 2.9.4
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: 0.2.1

@toobaz toobaz added Error Reporting Incorrect or improved errors from pandas MultiIndex labels May 29, 2018
@mroeschke
Copy link
Member

This looks to raise as expected on master now. Could use a test

In [4]: idx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'A'),
   ...:                                  (TS('2015-01-04'), 'A'),
   ...:                                  (TS('2015-01-04'), 'B')])
   ...: df2 = pd.Series([0.02, 0.01, 0.012], index=idx)
   ...:
   ...: widx = pd.MultiIndex.from_tuples([(TS('2015-01-03'), 'A'),
   ...:                                   (TS('2015-01-03'), 'B'),
   ...:                                   (TS('2015-01-04'), 'A'),
   ...:                                   (TS('2015-01-04'), 'B'),
   ...:                                   (TS('2015-01-04'), 'C'),
   ...:                                   (TS('2015-01-05'), 'B'),
   ...:                                   (TS('2015-01-05'), 'C'),
   ...:                                   (TS('2015-01-06'), 'B'),
   ...:                                   (TS('2015-01-06'), 'C')])
   ...: df2.reindex(widx, fill_value=0, limit=1)
ValueError: limit argument only valid if doing pad, backfill or nearest reindexing

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Error Reporting Incorrect or improved errors from pandas MultiIndex labels Jun 20, 2021
@calvh
Copy link
Contributor

calvh commented Jul 8, 2021

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants