API: better error-handling for df.set_index #22484
Labels
Error Reporting
Incorrect or improved errors from pandas
Indexing
Related to indexing on series/frames, not to indexes themselves
Milestone
splitting up #22236.
Let's have
df = pd.DataFrame(np.random.randn(5, 5), columns=list('ABCDE'))
The error handling of
df.set_index
can be improved in at least three cases:df.set_index(['A', 'A'], drop=False)
works, whiledf.set_index(['A', 'A'], drop=True)
yieldsKeyError: 'A'
KeyError
instead ofTypeError
:df.set_index(map(str, df.A))
KeyError: "None of [Index([...], dtype='object')] are in the [columns]"
df.set_index(['foo', 'bar', 'baz'])
only shows one missing keyKeyError: 'foo'
(in a huge stacktrace)Better would be:
drop=True
TypeError: only allowed types are: ...
KeyError: "['foo', 'bar', 'baz']"
The text was updated successfully, but these errors were encountered: