Skip to content

CLN: Refactor Index._validate_names() #19171

@spacesphere

Description

@spacesphere
Contributor

Currently Index._validate_names() does not only validation, but some other unrelated to validation stuff:

def _validate_names(self, name=None, names=None, deep=False):
"""
Handles the quirks of having a singular 'name' parameter for general
Index and plural 'names' parameter for MultiIndex.
"""
from copy import deepcopy
if names is not None and name is not None:
raise TypeError("Can only provide one of `names` and `name`")
elif names is None and name is None:
return deepcopy(self.names) if deep else self.names
elif names is not None:
if not is_list_like(names):
raise TypeError("Must pass list-like as `names`.")
return names
else:
if not is_list_like(name):
return [name]
return name

Moreover, it's closely tied in with copying logic without any reason, so that _validate_names() cannot be used in other cases.
I think it would be reasonable to separate these responsibilities.

Activity

added
IndexingRelated to indexing on series/frames, not to indexes themselves
InternalsRelated to non-user accessible pandas implementation
on Jan 12, 2018
gfyoung

gfyoung commented on Jan 12, 2018

@gfyoung
Member

@PoppyBagel : Thanks for reporting this! Perhaps the name is a little misleading, as you're indeed correct that we don't just validate. Maybe a name like "_parse_names" might be more appropriate. Feel free to propose another name in a PR if you like.

added this to the 0.23.0 milestone on Jan 15, 2018
modified the milestones: 0.23.0, Next Major Release on Apr 14, 2018
added
IndexRelated to the Index class or subclasses
and removed
IndexingRelated to indexing on series/frames, not to indexes themselves
on Feb 10, 2020
jbrockmendel

jbrockmendel commented on Dec 21, 2021

@jbrockmendel
Member

Probably becomes simpler once #44916 is enforced.

modified the milestones: Contributions Welcome, 2.0 on Dec 21, 2021
removed this from the 2.0 milestone on Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    IndexRelated to the Index class or subclassesInternalsRelated to non-user accessible pandas implementationRefactorInternal refactoring of code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @jreback@jbrockmendel@gfyoung@mroeschke@spacesphere

      Issue actions

        CLN: Refactor Index._validate_names() · Issue #19171 · pandas-dev/pandas