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

BUG: Check types in Index.__contains__ (#22085) #22602

Merged
merged 2 commits into from
Sep 19, 2018
Merged

BUG: Check types in Index.__contains__ (#22085) #22602

merged 2 commits into from
Sep 19, 2018

Conversation

yeojin-dev
Copy link
Contributor

I added is_float, is_integer_dtype in Index.contains.
If key is float and dtype of Index object is integer, return False.

And this is the same as #22360. I deleted the branch while rebasing it... 😅

@pep8speaks
Copy link

pep8speaks commented Sep 5, 2018

Hello @yeojin-dev! Thanks for updating the PR.

Comment last updated on September 19, 2018 at 12:28 Hours UTC

@TomAugspurger
Copy link
Contributor

If you fetch and merge master the CI failures should be fixed.

@jreback jreback added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Sep 5, 2018
@@ -77,6 +77,9 @@ cdef class IndexEngine:
def __contains__(self, object val):
self._ensure_mapping_populated()
hash(val)
if (util.is_float_object(val) and isinstance(self, Int64Engine) and
Copy link
Contributor

Choose a reason for hiding this comment

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

can you just do this in the index_class_helper.pyi (just override the method there). Is this applicable for the UInt64Engine as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -2474,6 +2474,19 @@ def test_comparison_tzawareness_compat(self, op):
# TODO: implement _assert_tzawareness_compat for the reverse
# comparison with the Series on the left-hand side

def test_contains_with_float_val(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

move to pandas/tests/indexes/test_indexing.py (near the other contains tests). Use an assert on the constructed Index itself (e.g. assert its an Int64Index or FloatIndex), or you can just construct them directly. Test on UInt64 as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

name these a bit more intuitively, e.g. integer_index, float_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.

Done.

@codecov
Copy link

codecov bot commented Sep 12, 2018

Codecov Report

Merging #22602 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #22602      +/-   ##
==========================================
+ Coverage   92.16%   92.16%   +<.01%     
==========================================
  Files         169      169              
  Lines       50769    50778       +9     
==========================================
+ Hits        46791    46800       +9     
  Misses       3978     3978
Flag Coverage Δ
#multiple 90.58% <100%> (ø) ⬆️
#single 42.33% <90.9%> (ø) ⬆️
Impacted Files Coverage Δ
pandas/core/indexes/numeric.py 97.41% <100%> (+0.1%) ⬆️

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 1a12c41...39fab6a. Read the comment docs.

@yeojin-dev
Copy link
Contributor Author

@jreback Please check. 😆

@@ -80,4 +80,13 @@ cdef class {{name}}Engine(IndexEngine):

{{endif}}

{{if name == 'Int64' or name == 'UInt64'}}
def __contains__(self, object val):
Copy link
Contributor

Choose a reason for hiding this comment

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

this is nice, can you add an else for all other dtypes.

Then I think can you edit pandas/core/indexes/base.py (and any sub-classes) that have a __contains__
and remove the hash check, and I think move the try/except to here.

Copy link
Contributor

Choose a reason for hiding this comment

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

actually on 2nd thought, this might be better in pandas.core.indexes.numeric.py can you try there? (ignore my other comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jreback I've no idea what I should edit in numeric.py. There's no __contains__ except Float64Index. Did you mean what I should is to move my code to numeric.py from pxi.in?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes that is what I meant.

Pipfile Outdated
@@ -0,0 +1,11 @@
[[source]]
url = "https://pypi.org/simple"
Copy link
Contributor

Choose a reason for hiding this comment

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

what is this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, it's my mistake.

@@ -174,6 +175,12 @@ class Int64Index(NumericIndex):
_engine_type = libindex.Int64Engine
_default_dtype = np.int64

def __contains__(self, key):
hash(key)
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a doc-string

Copy link
Contributor

Choose a reason for hiding this comment

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

can you create a IntegerIndex(NumericIndex) and put this method there, then inherit Int64Index and UInt64Index from that?

@jreback jreback added this to the 0.24.0 milestone Sep 19, 2018
@jreback jreback merged commit 1c113db into pandas-dev:master Sep 19, 2018
@jreback
Copy link
Contributor

jreback commented Sep 19, 2018

thanks @yeojin-dev sorry for the back and forth, sometimes hard to figure out where to put things

@yeojin-dev yeojin-dev deleted the fix-22085 branch September 20, 2018 04:10
@yeojin-dev
Copy link
Contributor Author

@jreback Thanks. 😆

aeltanawy pushed a commit to aeltanawy/pandas that referenced this pull request Sep 20, 2018
Sup3rGeo pushed a commit to Sup3rGeo/pandas that referenced this pull request Oct 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Checking that a pandas.Series.index contains a value
4 participants