Unify index and multindex (and possibly others) API · Issue #3268 · pandas-dev/pandas (original) (raw)
Have you ever written code that looks like this:
if isinstance(d.index, MultiIndex):
results = []
for l in d.index.levels:
for x in baz(l):
results.append(foo)
elif isinstance(d.index, Index):
for x in d.index:
foo
I've had to special case the handling of index vs. multindex several times in the past.
Conceptually, I should be able to treat index as a private case of MultIndex
with nlevels =1, and supporting that in the API would make things nicer.
Edit by @cpcloud:
Tasks :
API Unification
Method unification is relatively simple:
Index.from_tuplesandIndex.from_arraysare justMultiIndex.from_tuplesandMultiIndex.from_arraysmoved to classmethods ofIndex.droplevel, ` just raises on Index (right? what would it mean?): API: implement droplevels() for flat index #21115has_duplicatesis straightforwardtruncateshould be equivalent to slicingreorder_levelsraises if not level=0 or name of indexequal_levels- straightforwardlevshape- (len(ind),)sortorder- Noneget_loc_level- I think meaningless with tuple, raises whatever if not 0 or index nameis_lexsorted- doesn't need to changeis_lexosrted_tuple- doesn't need to changeis_monotonic_*lexsort_depth- doesn't need to be changed at allsearchsortedrepeatlevelsandlabelsproperty for Index - question on whether it should be sorted.- change to
renamebehavior:Indexwill accept either string or single-element list; MI continues to handle only list