Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLN: Refactor Index._validate_names() #19246

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,13 @@ def copy(self, name=None, deep=False, dtype=None, **kwargs):
new_index = self._shallow_copy()

names = kwargs.get('names')
names = self._validate_names(name=name, names=names, deep=deep)

if name is None and names is None:
from copy import deepcopy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import can be at the top. I think write like this

names = self._validate_names(name=name, names=names)
names = deepcopy(self.names) if deep else self.names

to avoid the double check here

names = deepcopy(self.names) if deep else self.names
else:
names = self._validate_names(name=name, names=names)

new_index = new_index.set_names(names)

if dtype:
Expand All @@ -800,24 +806,21 @@ def __deepcopy__(self, memo=None):
memo = {}
return self.copy(deep=True)

def _validate_names(self, name=None, names=None, deep=False):
def _validate_names(self, name=None, names=None):
"""
Handles the quirks of having a singular 'name' parameter for general
Index and plural 'names' parameter for MultiIndex.
Returns valid `names` considering both `names` and `name`
or raises TypeError in case of wrong arguments passed.
"""
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
raise TypeError("Can provide only one of names and name arguments")

if names is not None and not is_list_like(names):
raise TypeError("names must be list-like")

if name is not None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might be able to remove this as .set_names already handles the list-like / scalar case (or maybe remove it there)

return name if is_list_like(name) else [name]

return names

def __unicode__(self):
"""
Expand Down
9 changes: 7 additions & 2 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,16 @@ def copy(self, names=None, dtype=None, levels=None, labels=None,
``deep``, but if ``deep`` is passed it will attempt to deepcopy.
This could be potentially expensive on large MultiIndex objects.
"""
from copy import deepcopy

name = kwargs.get('name')
names = self._validate_names(name=name, names=names, deep=deep)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

if name is None and names is None:
names = deepcopy(self.names) if deep else self.names
else:
names = self._validate_names(name=name, names=names)

if deep:
from copy import deepcopy
if levels is None:
levels = deepcopy(self.levels)
if labels is None:
Expand Down