-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
API/BUG: make .at raise same exceptions as .loc #31724
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 all commits
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 |
---|---|---|
|
@@ -129,38 +129,79 @@ def test_imethods_with_dups(self): | |
result = df.iat[2, 0] | ||
assert result == 2 | ||
|
||
def test_at_to_fail(self): | ||
def test_series_at_raises_type_error(self): | ||
# at should not fallback | ||
# GH 7814 | ||
s = Series([1, 2, 3], index=list("abc")) | ||
result = s.at["a"] | ||
# GH#31724 .at should match .loc | ||
ser = Series([1, 2, 3], index=list("abc")) | ||
result = ser.at["a"] | ||
assert result == 1 | ||
result = ser.loc["a"] | ||
assert result == 1 | ||
|
||
msg = ( | ||
"At based indexing on an non-integer index can only have " | ||
"non-integer indexers" | ||
"cannot do label indexing on <class 'pandas.core.indexes.base.Index'> " | ||
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 have to say that I found the previous error message more readable .. 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 generally agree, that suggests we should improve the existing messages for "loc" |
||
r"with these indexers \[0\] of <class 'int'>" | ||
) | ||
with pytest.raises(ValueError, match=msg): | ||
s.at[0] | ||
with pytest.raises(TypeError, match=msg): | ||
ser.at[0] | ||
with pytest.raises(TypeError, match=msg): | ||
ser.loc[0] | ||
|
||
def test_frame_raises_type_error(self): | ||
# GH#31724 .at should match .loc | ||
df = DataFrame({"A": [1, 2, 3]}, index=list("abc")) | ||
result = df.at["a", "A"] | ||
assert result == 1 | ||
with pytest.raises(ValueError, match=msg): | ||
result = df.loc["a", "A"] | ||
assert result == 1 | ||
|
||
msg = ( | ||
"cannot do label indexing on <class 'pandas.core.indexes.base.Index'> " | ||
r"with these indexers \[0\] of <class 'int'>" | ||
) | ||
with pytest.raises(TypeError, match=msg): | ||
df.at["a", 0] | ||
with pytest.raises(TypeError, match=msg): | ||
df.loc["a", 0] | ||
|
||
def test_series_at_raises_key_error(self): | ||
# GH#31724 .at should match .loc | ||
|
||
s = Series([1, 2, 3], index=[3, 2, 1]) | ||
result = s.at[1] | ||
ser = Series([1, 2, 3], index=[3, 2, 1]) | ||
result = ser.at[1] | ||
assert result == 3 | ||
msg = "At based indexing on an integer index can only have integer indexers" | ||
with pytest.raises(ValueError, match=msg): | ||
s.at["a"] | ||
result = ser.loc[1] | ||
assert result == 3 | ||
|
||
with pytest.raises(KeyError, match="a"): | ||
ser.at["a"] | ||
with pytest.raises(KeyError, match="a"): | ||
# .at should match .loc | ||
ser.loc["a"] | ||
|
||
def test_frame_at_raises_key_error(self): | ||
# GH#31724 .at should match .loc | ||
|
||
df = DataFrame({0: [1, 2, 3]}, index=[3, 2, 1]) | ||
|
||
result = df.at[1, 0] | ||
assert result == 3 | ||
with pytest.raises(ValueError, match=msg): | ||
result = df.loc[1, 0] | ||
assert result == 3 | ||
|
||
with pytest.raises(KeyError, match="a"): | ||
df.at["a", 0] | ||
with pytest.raises(KeyError, match="a"): | ||
df.loc["a", 0] | ||
|
||
with pytest.raises(KeyError, match="a"): | ||
df.at[1, "a"] | ||
with pytest.raises(KeyError, match="a"): | ||
df.loc[1, "a"] | ||
|
||
# TODO: belongs somewhere else? | ||
def test_getitem_list_missing_key(self): | ||
# GH 13822, incorrect error string with non-unique columns when missing | ||
# column is accessed | ||
df = DataFrame({"x": [1.0], "y": [2.0], "z": [3.0]}) | ||
|
Uh oh!
There was an error while loading. Please reload this page.