-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Pandas doesn't always cast strings to int consistently when using .isin() #24918
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
Comments
Thanks for the report, though I believe the expectation is wrong. Why should A, E, F or G return anything? |
@WillAyd A, E and F do though. Why should G be different? Either all of them should return something or none of them should. I think it is counterintuitive for a .isin(mylist) to return some rows, and .isin(mylist + [newel]) to return no rows. Adding more elements to the list that pandas is matching on should never result in fewer elements being returned. Do you know if there is a C subroutine the performs the comparisons or if it's in Python? |
That's my point. None of these should return anything The implementation of this for a Series is linked below if you want to take a look Line 3943 in 83eb242
|
Thanks @WillAyd. I followed that function into the pandas/pandas/core/algorithms.py Line 418 in 83eb242
That function sees that the series At first I thought that example D was returning matches for So really what it comes down to is that Iterating through each element and casting it is probably not what we want. I can also see why It was a fun rabbit hole to go down, thanks again Will! |
@BasilBeirouti thanks a lot for investigating! Looking at the line you've called out I get the impression this is an unintended consequence of that exception handling cc @jreback in case he knows of something I don't and/or objects to making this comparison stricter |
this takes a pretty tricky path of code and sometimes we call out to numpy which has some weird inference rules it’s possible might be a bug - it’s also somewhat performance sensitive |
This is a consistency issue, and
|
As described #24918 (comment), the expected behavior is not to cast strings to ints, so the behavior we see in the OP is correct. Closing. |
Code Sample, a copy-pastable example if possible
Problem description
In the examples above, examples A-F behave as expected; only example G behaves unexpectedly.
When using
df[df.mycol.isin(alist)]
, and if mycol is an integer column, Pandas appears to be able to convert strings in alist to integers, and intelligently check if they occur in mycol. Only in the last case (example G) outlined above does it fail to do so. This seems inconsistent to me because it is able to ignore 'fart' in previous examples (examples C and D), It is also able to correctly cast '2' to an int and return matching rows (examples A, B, D). It is also able to do both of these things simultaneously (example D). It is also able to correctly cast two matching elements (Example E). Only when all elements need to be casted, and one of them is uncastable, does the unexpected behavior occur (example G). In Example F, all need to be casted, but they are all castable, so it works fine.Basically, as long as they are all castable to the correct type, or there is at least one element of the correct type, it works.
To put it another way, if
df[df.mycol.isin(alist)]
returns something, thendf[df.mycol.isin(alist + [newel])]
should return at least that same thing. This seems reasonable to me and I think how it was intended to work.For my own edification, I would like to understand where the code that does the casting / comparisons is implemented. Is it in C or Python?
Expected Output
last line should ignore the string 'fart', correctly cast the string '2' to int 2 and string '4' to int 4, as it does in previous examples, and return the matching rows in df.
Output of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.7.2.final.0
python-bits: 64
OS: Darwin
OS-release: 18.2.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
pandas: 0.23.4
pytest: None
pip: 18.1
setuptools: 40.6.3
Cython: None
numpy: 1.16.0
scipy: 1.2.0
pyarrow: None
xarray: None
IPython: 7.2.0
sphinx: None
patsy: None
dateutil: 2.7.5
pytz: 2018.9
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: 1.2.16
pymysql: None
psycopg2: 2.7.7 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: