ENH: reset_index on a MultiIndex with duplicate levels raises a ValueError by johnzangwill · Pull Request #44755 · pandas-dev/pandas (original) (raw)

Accidentially ending up with duplictated column names is not desireable.
IMO this is more of an api change instead of a bugfix.

Agreed on both counts. For #44267, it is the case that the user called the function with duplicate columns, the internals of groupby moved them into the index, and we want to restore them as columns. For this, it would suffice to have an internal version of reset_index where we can override allow_duplicates, i.e. this pattern:

def foo(x):
    return _foo(x, hidden_arg=False)

def _foo(x, hidden_arg=False):
    pass

where #44267 could then call _foo(x, hidden_arg=True) (where hidden_arg is allow_duplicates).

Also I think you can make a point that this should be changed inside of insert, not on a higher level.

I'm not sure what this means (in other words, what should be changed inside of insert?). In any case, for #44267, I don't think we should be reimplementing reset_index via calling insert directly (to be clear, I'm not suggesting you're saying that).