Description
pd.Index
acceptsname
- greatpd.MultiIndex
acceptsnames
- after all, they are multiplename
spd.Index
acceptsnames
for compatibility withpd.MultiIndex
- well, OK, after all, that constructor can result in aMultiIndex
pd.Index
should accept ( BUG: Creating Index name usingnames
names argument, doesn't set index name #19082 )names
even when it results in a flat index - well, OK, still for compatibilitypd.MultiIndex
acceptsname
for compatibility - wait, wasn'tnames
already provided for compatibility?! OK, forget it, go forname
.pd.Index
acceptsname
even for multiple levels' names, for compatibility - withMultiIndex
, which accepts it for compatibility withpd.Index
- aaaaalright- All
pd.Index
subclasses (which will never result in multiple levels, and which currently in some cases discardnames
, in other cases raise an error) should acceptnames
for 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.Index
orpd.MultiIndex
), thenname
should 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
,names
can be used as an alias forname
. In all other cases, it is not accepted.
or alternatively:
-
(b) (my preference)
names
is 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)
names
is supported inpd.MultiIndex
, still supported but deprecated inpd.Index
when resulting inMultiIndex
(and remains unsupported in other constructors/cases)
Corollary:
names
will not be supported by any constructor that is notpd.Index
orpd.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 deprecatenames
entirely)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,
name
is also accepted, and interpreted asnames=(name,)
; insteadname
is deprecated forpd.MultiIndex
, and forpd.Index
when resulting in aMultiIndex
(even if with 1 level)
Corollary:
- implementation-wise, we will want to decorate all
__new__
of non-MultiIndex
subclasses to convertnames
toname