Skip to content

BUG: Fix Series.reindex losing values when reindexing to MultiIndex #61969

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Roline-Stapny
Copy link

#Before

# Create a Series with a named Index
series = pd.Series([26.73, 24.255], index=pd.Index([81, 82], name='a'))

# Create a MultiIndex with level names 'a', 'b', 'c'
target = pd.MultiIndex.from_product(
    [[81, 82], [np.nan], ["2018-06-01", "2018-07-01"]], 
    names=["a", "b", "c"]
)

# This would incorrectly set all values to NaN
series.reindex(target)
# a   b    c         
# 81  NaN  2018-06-01   NaN
#          2018-07-01   NaN
# 82  NaN  2018-06-01   NaN
#          2018-07-01   NaN

# But this works correctly
series.reindex(target, level="a")
# a   b    c         
# 81  NaN  2018-06-01    26.73
#          2018-07-01    26.73
# 82  NaN  2018-06-01    24.255
#          2018-07-01    24.255

#After

# Same setup as before
series = pd.Series([26.73, 24.255], index=pd.Index([81, 82], name='a'))
target = pd.MultiIndex.from_product(
    [[81, 82], [np.nan], ["2018-06-01", "2018-07-01"]], 
    names=["a", "b", "c"]
)

# Now both produce the same correct result
series.reindex(target)  # Automatically detects level='a'
# a   b    c         
# 81  NaN  2018-06-01    26.73
#          2018-07-01    26.73
# 82  NaN  2018-06-01    24.255
#          2018-07-01    24.255

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant