-
-
Notifications
You must be signed in to change notification settings - Fork 19.2k
Description
pd.Indexacceptsname- greatpd.MultiIndexacceptsnames- after all, they are multiplenamespd.Indexacceptsnamesfor compatibility withpd.MultiIndex- well, OK, after all, that constructor can result in aMultiIndexpd.Indexshould accept ( BUG: Creating Index name usingnamesnames argument, doesn't set index name #19082 )nameseven when it results in a flat index - well, OK, still for compatibilitypd.MultiIndexacceptsnamefor compatibility - wait, wasn'tnamesalready provided for compatibility?! OK, forget it, go forname.pd.Indexacceptsnameeven for multiple levels' names, for compatibility - withMultiIndex, which accepts it for compatibility withpd.Index- aaaaalright- All
pd.Indexsubclasses (which will never result in multiple levels, and which currently in some cases discardnames, in other cases raise an error) should acceptnamesfor compatibility - no, wait.
Proposal 1
-
There is one way to have compatibility across any kind of
Index(sub)class constructor, and it isname. When the constructor results in aMultiIndex(which can happen withpd.Indexorpd.MultiIndex), thennameshould be list-like, and each level will "receive" the respective component -
in those cases - and only in those cases - in which a constructor actually results in a
MultiIndex,namescan be used as an alias forname. In all other cases, it is not accepted.
or alternatively:
-
(b) (my preference)
namesis deprecated (in all cases in which it is currently supported, and remains unsupported in other constructors/cases) -
(c) (tradeoff between mental health and backward compatibility)
namesis supported inpd.MultiIndex, still supported but deprecated inpd.Indexwhen resulting inMultiIndex(and remains unsupported in other constructors/cases)
Corollary:
nameswill not be supported by any constructor that is notpd.Indexorpd.MultiIndex.
Notice that a 1-level MultiIndex is still a MultiIndex. That is,
pd.Index([('a',), ('b',)], name=('only_one_name',))will still workpd.Index([('a',), ('b',)], names=('only_one_name',))will still work (at least as long as we don't deprecatenamesentirely)pd.Index([('a',), ('b',)], name='only_one_name')will still not work
Proposal 2
-
There is one way to have compatibility across any kind of
Index(sub)class constructor, and it isnames, which must always be list-like. -
In those cases in which the constructor results in a flat index,
nameis also accepted, and interpreted asnames=(name,); insteadnameis deprecated forpd.MultiIndex, and forpd.Indexwhen resulting in aMultiIndex(even if with 1 level)
Corollary:
- implementation-wise, we will want to decorate all
__new__of non-MultiIndexsubclasses to convertnamestoname