Skip to content

Accept index in addition to list for set_index  #10797

@michaelbilow

Description

@michaelbilow

Running this code generates an error, since columns are now considered an Index and not a list. I'm not sure if the error that gets raised (set_index ends up asking for an index that is the entire length of the data frame) is actually by design. In this case, it was a bit unintuitive to figure out how to catch, since Index objects and python lists often work so similarly.

import string import pandas as pd data1 = 'x'*5 + 'y'*5 data2 = string.lowercase[:5]*2 data3 = range(10) data_dict = {'Cat': list(data1), 'SubCat': list(data2), 'Vals':data3} df = pd.DataFrame(data_dict) ordered_df = df[['Cat', 'SubCat', 'Vals']] correct_df = ordered_df.reset_index([x for x in ordered_df.columns[:2]]) error_df = ordered_df.set_index(ordered_df.columns[:2]) 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions