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

Conversation

spacesphere
Copy link
Contributor

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:

Copy link
Contributor

Choose a reason for hiding this comment

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

is there a reason you are splitting this into 2 functions? what is wrong with the original that this is needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mostly because we need validation logic not only for copying (see #19168). It should reduce repetitions in the code.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, not averse to this, maybe its the spelling and how these are split. if you can clearly define each purpose with a doc-string then all for it.

@@ -800,24 +800,29 @@ def __deepcopy__(self, memo=None):
memo = {}
return self.copy(deep=True)

def _validate_names(self, name=None, names=None, deep=False):
def _handle_names(self, name=None, names=None, deep=False):
Copy link
Contributor

Choose a reason for hiding this comment

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

I see the original issue, but how does this fix things, _validate_names is stilling doing partial validation and partial conversions.

Copy link
Contributor Author

@spacesphere spacesphere Jan 15, 2018

Choose a reason for hiding this comment

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

Well, I agree with that to some extent. But conversion can be removed if Pandas deprecates names argument for MultiIndex as it was proposed in #19168 (comment) .

Copy link
Contributor

Choose a reason for hiding this comment

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

well, no, the reason for acceptance is that MI needs to have the same interface as Index.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But if Index accepts names as an argument, they will be compatible.

return name

return self._validate_names(name=name, names=names)

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe if you these have doc-strings I would be able to tell what is going on here.

@jreback jreback added Indexing Related to indexing on series/frames, not to indexes themselves Internals Related to non-user accessible pandas implementation labels Jan 15, 2018
@codecov
Copy link

codecov bot commented Jan 15, 2018

Codecov Report

Merging #19246 into master will decrease coverage by 0.03%.
The diff coverage is 84.61%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #19246      +/-   ##
==========================================
- Coverage   91.56%   91.53%   -0.04%     
==========================================
  Files         148      147       -1     
  Lines       48856    48812      -44     
==========================================
- Hits        44733    44678      -55     
- Misses       4123     4134      +11
Flag Coverage Δ
#multiple 89.9% <84.61%> (-0.04%) ⬇️
#single 41.7% <38.46%> (+0.01%) ⬆️
Impacted Files Coverage Δ
pandas/core/indexes/multi.py 96.22% <100%> (ø) ⬆️
pandas/core/indexes/base.py 96.45% <83.33%> (-0.01%) ⬇️
pandas/core/accessor.py 93.75% <0%> (-4.96%) ⬇️
pandas/plotting/_converter.py 65.22% <0%> (-1.74%) ⬇️
pandas/core/indexes/accessors.py 89.36% <0%> (-0.64%) ⬇️
pandas/io/json/table_schema.py 98.19% <0%> (-0.1%) ⬇️
pandas/util/testing.py 84.41% <0%> (-0.04%) ⬇️
pandas/core/reshape/reshape.py 100% <0%> (ø) ⬆️
pandas/core/frame.py 97.62% <0%> (ø) ⬆️
pandas/core/base.py 96.77% <0%> (ø) ⬆️
... and 8 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c271d4d...3809d87. Read the comment docs.

@spacesphere
Copy link
Contributor Author

@jreback , please, review changes

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

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)

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

@gfyoung
Copy link
Member

gfyoung commented Jan 24, 2018

@PoppyBagel @jreback : I'm -1 for breaking out the logic like this if we have to duplicate the copying logic. It was perfectly encapsulated in that one function. I'm still in the camp that it just needed to be renamed.

@spacesphere spacesphere deleted the index-names-validation branch January 30, 2018 06:12
@spacesphere spacesphere restored the index-names-validation branch February 12, 2018 12:36
@spacesphere spacesphere deleted the index-names-validation branch February 12, 2018 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Internals Related to non-user accessible pandas implementation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CLN: Refactor Index._validate_names()
3 participants