-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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: loc
-based indexing with float keys fails inconsistently if index is integer close to numeric limits
#53290
Comments
Another approach would be for the label-based indexing to map semantically onto the equivalent that would occur if the index were just a normal column and one were doing mask-based filtering (this would then use the normal cross-kind casting rules for binops). |
Floats being lossy is well known and inherent to how computers work and things like this will happen when you use floats for indexing on integer indexes. So this isn't a bug, but a result of indexing with big floats on integer indexes (which you shouldn't do) |
Is the pandas position therefore that indexing with floats on non-float indices is undefined behaviour? |
If the float and int evaluate equal, it's ok. The problem here is that |
Agreed, but I contend that it should consistently not match (note that |
That does look wrong to me. Can you open an issue on this? |
I mean, that is this issue, no? |
Oh, very sorry about that, I misunderstood the code snippet that you iterated first over ints then floats. I see now it's first scalars, then lists. So the issue here is that lists of large floats work when indexing when they should fail? (while their scalar equivalent fails correctly?) |
Yes (for some definition of "works" that isn't the one I expected). Just to clarify what is happening: Index:
I think there are few possible valid answers (depending on the casting rules for
I would say (1) is arguably the best option, but does introduce value-dependent Hence, option (4): deprecate cross-kind casting in |
Any further thoughts here? |
The underlying problems seems to be: >>> big_int = 2**63 - 1
>>> big_float = float(big_int)
>>> np_float = np.array([big_int])[0]
>>>
>>> big_int == np_float
True
>>> big_float == np_float
True
>>> big_int == big_float
False This is weird, the last statement isn't consistent with the two previous statements. |
This is why cross-kind casting is so insidious (especially with operator overloading), and why I think that my option (4) [Disallow cross-kind casting in label-based indexing] above is the best long-term solution. What is going on? These examples are all calling different routines, specifically:
|
Another way to see this problem is like this: >>> s[[9223372036854775807]]
1
>>> s[[9223372036854775807.0]]
3 I don't think this is fixable and the lack of precision is inherent when using large floats. |
OK, I've opened #53613 instead to propose fixing this by deprecating the behaviour completely. |
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
Running this produces:
When asking for a single scalar float, since the conversion is lossy, no
.loc
produces an answer (in the guts, there's an overflow-error when casting from the float back to an integer, since the nearest integer tofloat(2**63-1)
is2**63
, similarly for-10
and-100
, whereasfloat(2**63 - 1000)
is representable as an integer, but is not2**63 - 1000
).This is the behaviour I would expect, since the index does not contain the requested keys.
In contrast when indexing with a singleton list of the same values, indexing is always successful but only produces the correct answer for
2**63 - 100
and2**63 - 1000
. For2**63 - 1
and2**63 - 10
the entry for2**63 - 100
is returned instead.Aside: note that this is not symmetric: if the index is floats and we ask for an integer key with
loc
the behaviour is as if the key is cast to float first and then looked for.Expected Behavior
Looking for a key that doesn't exist should consistently produce a keyerror. At the very least, these two broadly equivalent ways of indexing should have the same semantics.
FWIW, I would argue that the best thing is to deprecate lossy casting (e.g. cross-kind) in label-based indexing, and remove it in pandas 3. The semantics of the indexing are already hard enough to understand without having to have intimate knowledge of the behaviour of numpy's type promotion rules and their interaction with numeric representations. [Yes, I know that Python hashes small ints and small integral floats the same, I also think that was a bad decision]
Installed Versions
INSTALLED VERSIONS
commit : 37ea63d
python : 3.11.3.final.0
python-bits : 64
OS : Linux
OS-release : 5.19.0-41-generic
Version : #42~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Apr 18 17:40:00 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8
pandas : 2.0.1
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.7.2
pip : 23.1.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.13.2
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: