Skip to content

Avoid accessing private loc methods #27383

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

Merged
merged 13 commits into from
Jul 24, 2019
Merged

Conversation

jbrockmendel
Copy link
Member

Works toward fixing #27344

Adds types where unambiguous

@jbrockmendel
Copy link
Member Author

@WillAyd can you help me make heads or tails out of the mypy complaint?

@WillAyd
Copy link
Member

WillAyd commented Jul 14, 2019

Yea I imagine it's because of part of Line 1745:

[key] + [slice(None)]

So the left operand is inferred to List[str] while the right hand side is a slice and Mypy is complaining that you are appending a slice to a list of what maybe should be just str.

Could either:

  1. Create a new variable for this typed to List[Union[str, slice]]
  2. # type: ignore this line

I think option 1 is better since this code is shadowing key variable anyway with a different type

@jbrockmendel
Copy link
Member Author

Yea I imagine it's because of part of Line 1745:

The part that I don't get is why is it only complaining now? The types being added should be a strict improvement typing-wise, right?

@WillAyd
Copy link
Member

WillAyd commented Jul 14, 2019 via email

@pep8speaks
Copy link

pep8speaks commented Jul 14, 2019

Hello @jbrockmendel! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2019-07-23 23:08:04 UTC

@@ -222,7 +223,7 @@ def _validate_key(self, key, axis: int):
"""
raise AbstractMethodError(self)

def _has_valid_tuple(self, key):
def _has_valid_tuple(self, key: tuple):
Copy link
Member

Choose a reason for hiding this comment

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

Can you use the generic Tuple from the typing module? Providing the type(s) contained within would be even better (maybe Tuple[Hashable]?)

"""Translate any partial string timestamp matches in key, returning the
new key (GH 10331)"""
if isinstance(labels, MultiIndex):
if isinstance(key, str) and labels.levels[0].is_all_dates:
# Convert key '2016-01-01' to
# ('2016-01-01'[, slice(None, None, None)]+)
key = tuple([key] + [slice(None)] * (len(labels.levels) - 1))
key = tuple(
[key] + [slice(None)] * (len(labels.levels) - 1)
Copy link
Member

Choose a reason for hiding this comment

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

The more I think about this a better idiom would be something like:

new_key = None # type: List[str, slice]
if isinstance(labels, MultiIndex):
   ...
   new_key = tuple([key] + [slice(None)] * len(labels.levels) - 1)
    ...

Should appease mypy and prevent the shadowing of the key variable

@@ -3484,7 +3484,9 @@ def _setitem_array(self, key, value):
for k1, k2 in zip(key, value.columns):
self[k1] = value[k2]
else:
indexer = self.loc._convert_to_indexer(key, axis=1)
indexer = self.loc._get_listlike_indexer(
Copy link
Contributor

Choose a reason for hiding this comment

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

self.columns.get_indexer(key)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like this returns something different, will have to keep digging

@jreback jreback added Clean Indexing Related to indexing on series/frames, not to indexes themselves labels Jul 15, 2019
@codecov
Copy link

codecov bot commented Jul 15, 2019

Codecov Report

Merging #27383 into master will decrease coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #27383      +/-   ##
==========================================
- Coverage   92.91%   92.89%   -0.02%     
==========================================
  Files         184      184              
  Lines       50385    50387       +2     
==========================================
- Hits        46813    46809       -4     
- Misses       3572     3578       +6
Flag Coverage Δ
#multiple 91.57% <90.47%> (-0.01%) ⬇️
#single 42.33% <90.47%> (-0.09%) ⬇️
Impacted Files Coverage Δ
pandas/io/pytables.py 90.34% <100%> (ø) ⬆️
pandas/core/frame.py 97.08% <100%> (-0.12%) ⬇️
pandas/core/indexing.py 94.72% <100%> (+0.1%) ⬆️
pandas/io/gbq.py 88.88% <0%> (-11.12%) ⬇️
pandas/util/_decorators.py 92.13% <0%> (-3.22%) ⬇️
pandas/io/packers.py 87.65% <0%> (ø) ⬆️
pandas/io/sas/sasreader.py 96% <0%> (ø) ⬆️
pandas/io/excel/_base.py 92% <0%> (ø) ⬆️
pandas/io/feather_format.py 25% <0%> (ø) ⬆️
... and 3 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 c104a0c...f9c7fc9. Read the comment docs.

1 similar comment
@codecov
Copy link

codecov bot commented Jul 15, 2019

Codecov Report

Merging #27383 into master will decrease coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #27383      +/-   ##
==========================================
- Coverage   92.91%   92.89%   -0.02%     
==========================================
  Files         184      184              
  Lines       50385    50387       +2     
==========================================
- Hits        46813    46809       -4     
- Misses       3572     3578       +6
Flag Coverage Δ
#multiple 91.57% <90.47%> (-0.01%) ⬇️
#single 42.33% <90.47%> (-0.09%) ⬇️
Impacted Files Coverage Δ
pandas/io/pytables.py 90.34% <100%> (ø) ⬆️
pandas/core/frame.py 97.08% <100%> (-0.12%) ⬇️
pandas/core/indexing.py 94.72% <100%> (+0.1%) ⬆️
pandas/io/gbq.py 88.88% <0%> (-11.12%) ⬇️
pandas/util/_decorators.py 92.13% <0%> (-3.22%) ⬇️
pandas/io/packers.py 87.65% <0%> (ø) ⬆️
pandas/io/sas/sasreader.py 96% <0%> (ø) ⬆️
pandas/io/excel/_base.py 92% <0%> (ø) ⬆️
pandas/io/feather_format.py 25% <0%> (ø) ⬆️
... and 3 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 c104a0c...f9c7fc9. Read the comment docs.

@jbrockmendel
Copy link
Member Author

gentle ping; there are (requested) follow-ups

@jreback jreback added this to the 1.0 milestone Jul 24, 2019
@jreback jreback merged commit d6b443c into pandas-dev:master Jul 24, 2019
@jreback
Copy link
Contributor

jreback commented Jul 24, 2019

thanks

@jbrockmendel jbrockmendel deleted the loc_ branch July 24, 2019 00:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Clean Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants