-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: np.inf now causes Index to upcast from int to float #16996
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 1 commit
b424439
f040f4e
6f3886f
e029708
61d82f6
5de878f
7310c36
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 |
---|---|---|
|
@@ -63,6 +63,35 @@ def f(): | |
|
||
pytest.raises(ValueError, f) | ||
|
||
def test_inf_upcast(self): | ||
# GH 16957 | ||
# We should be able to use np.inf as a key | ||
# np.inf should cause an index to convert to float | ||
|
||
# Test with np.inf in rows | ||
df = pd.DataFrame(columns=[0]) | ||
df.loc[1] = 1 | ||
df.loc[2] = 2 | ||
df.loc[np.inf] = 3 | ||
|
||
# make sure we can look up the value | ||
result = df.loc[np.inf, 0] | ||
tm.assert_almost_equal(result, 3) | ||
|
||
result = df.index | ||
expected = pd.Float64Index([1, 2, np.inf]) | ||
tm.assert_index_equal(result, expected) | ||
|
||
# Test with np.inf in columns | ||
df = pd.DataFrame() | ||
df.loc[0, 0] = 1 | ||
df.loc[1, 1] = 2 | ||
df.loc[0, np.inf] = 3 | ||
|
||
result = df.columns | ||
expected = pd.Float64Index([0, 1, np.inf]) | ||
tm.assert_index_equal(result, expected) | ||
|
||
def test_setitem_dtype_upcast(self): | ||
|
||
# GH3216 | ||
|
@@ -542,6 +571,18 @@ def test_astype_assignment_with_dups(self): | |
# result = df.get_dtype_counts().sort_index() | ||
# expected = Series({'float64': 2, 'object': 1}).sort_index() | ||
|
||
def test_coercion_with_contains(self): | ||
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. Add a couple of examples where the expected result is |
||
# Related to GH 16957 | ||
# Checking if Int64Index contains np.inf should catch the OverflowError | ||
for val in [np.inf, np.nan]: | ||
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. We can avoid this |
||
index = pd.Int64Index([1, 2, 3]) | ||
result = val in index | ||
tm.assert_almost_equal(result, False) | ||
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 think you should be able to write:
instead of the two lines above. |
||
|
||
index = pd.UInt64Index([1, 2, 3]) | ||
result = np.inf in index | ||
tm.assert_almost_equal(result, False) | ||
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. Same comment as above |
||
|
||
def test_index_type_coercion(self): | ||
|
||
with catch_warnings(record=True): | ||
|
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.
Right location, though I would shorten to this: