-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DEPR: deprecate Index.__getitem__ with float key #34193
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
Changes from 3 commits
d48e646
b551587
bf9420a
2ed0d4a
70e5ac7
045888b
becde65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
from functools import partial | ||
import inspect | ||
from typing import Any, Collection, Iterable, List, Union | ||
import warnings | ||
|
||
import numpy as np | ||
|
||
|
@@ -144,20 +145,29 @@ def is_bool_indexer(key: Any) -> bool: | |
return False | ||
|
||
|
||
def cast_scalar_indexer(val): | ||
def cast_scalar_indexer(val, warn_float=False): | ||
""" | ||
To avoid numpy DeprecationWarnings, cast float to integer where valid. | ||
|
||
Parameters | ||
---------- | ||
val : scalar | ||
warn_float : bool, default False | ||
If True, raise deprecation warning for a float indexer. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "raise" -> "issue" |
||
|
||
Returns | ||
------- | ||
outval : scalar | ||
""" | ||
# assumes lib.is_scalar(val) | ||
if lib.is_float(val) and val.is_integer(): | ||
if warn_float: | ||
warnings.warn( | ||
"Indexing with a float is deprecated, and will raise an IndexError " | ||
"in pandas 2.0. You can manually convert to an integer key instead.", | ||
FutureWarning, | ||
stacklevel=3, | ||
) | ||
return int(val) | ||
return val | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
from pandas.util._decorators import cache_readonly | ||
|
||
from pandas.core.dtypes.common import ( | ||
is_float, | ||
is_hashable, | ||
is_integer, | ||
is_iterator, | ||
|
@@ -1188,6 +1189,8 @@ def _post_plot_logic(self, ax, data): | |
from matplotlib.ticker import FixedLocator | ||
|
||
def get_label(i): | ||
if is_float(i) and i.is_integer(): | ||
i = int(i) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not really sure this is correct, but it's what is currently actually is being done (the conversion to int is just happening inside the It's a bit strange that those xticks are floats, though. But maybe that's just a matplotlib behaviour. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is really strange ,what is i here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It are the values from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, just checked, matplotlib simply always returns floats. And it's only for the "integer floats" that we have a value in the index to put as xtick label. |
||
try: | ||
return pprint_thing(data.index[i]) | ||
except Exception: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
import pytest | ||
|
||
from pandas import Float64Index, Index, Int64Index, UInt64Index | ||
import pandas._testing as tm | ||
|
||
|
||
class TestContains: | ||
|
@@ -82,3 +83,16 @@ def test_contains_with_float_index(self): | |
assert 1.1 in float_index | ||
assert 1.0 not in float_index | ||
assert 1 not in float_index | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"idx", [Index([1, 2, 3]), Index([0.1, 0.2, 0.3]), Index(["a", "b", "c"])] | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could use indices fixture? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so, as the deprecation is only for those 3 index types |
||
def test_getitem_deprecated_float(idx): | ||
# https://github.com/pandas-dev/pandas/issues/34191 | ||
|
||
with tm.assert_produces_warning(FutureWarning): | ||
result = idx[1.0] | ||
|
||
expected = idx[1] | ||
assert result == expected |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
usually we dont have a trailing period. double ticks on IndexError?