Skip to content

df.reset_index() fails with a multi index with Nan's #14604

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
gryBox opened this issue Nov 7, 2016 · 1 comment
Closed

df.reset_index() fails with a multi index with Nan's #14604

gryBox opened this issue Nov 7, 2016 · 1 comment
Labels
Duplicate Report Duplicate issue or pull request Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex

Comments

@gryBox
Copy link

gryBox commented Nov 7, 2016

I believe this is the same issue as #6322 .

The code below returns the error: IndexError: cannot do a non-empty take from an empty axes.

import pandas as pd
import numpy as np
import random
import string



# Create 12 random strings 3 char long 
rndm_strgs = [''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(3)) for i in range(12)]            
rndm_strgs[0] = None
rndm_strgs[5] = None

# Make Dataframe
df = pd.DataFrame({'A' : list('pandasisgood'),
                   'B' : np.nan,
                   'C' : rndm_strgs,
                   'D' : np.random.rand(12)})

# Set an Index -> Columns have Nans
df_w_idx = df.set_index(['A','B','C'])

df_w_idx.reset_index()

To get the result I wanted:

for clmNm in df_w_idx.index.names:
    print(clmNm)

    df_w_idx[clmNm] = df_w_idx.index.get_level_values(clmNm)

    
df_w_idx = df_w_idx.reset_index(drop=True).copy()
df_w_idx

@jreback
Copy link
Contributor

jreback commented Nov 7, 2016

pls just put this example on the duplicated issue instead

@jreback jreback closed this as completed Nov 7, 2016
@jreback jreback added Indexing Related to indexing on series/frames, not to indexes themselves Duplicate Report Duplicate issue or pull request MultiIndex labels Nov 7, 2016
@jreback jreback added this to the No action milestone Nov 7, 2016
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

2 participants