-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG-20629 allow .at accessor with CategoricalIndex #26298
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
1843a04
13717db
188ac73
41bf854
cb0d4d9
3d38a5f
52990a8
14d016f
2831d3d
7b598c6
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 |
---|---|---|
|
@@ -2710,13 +2710,19 @@ def _get_value(self, index, col, takeable=False): | |
|
||
try: | ||
return engine.get_value(series._values, index) | ||
except KeyError as e: | ||
if self.index.nlevels > 1: | ||
raise e # partial indexing forbidden | ||
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. can you put the comment on a separate line before the raise |
||
else: | ||
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. isn't this else surperflous? |
||
pass # GH 20629 | ||
except (TypeError, ValueError): | ||
pass | ||
|
||
# we cannot handle direct indexing | ||
# use positional | ||
col = self.columns.get_loc(col) | ||
index = self.index.get_loc(index) | ||
return self._get_value(index, col, takeable=True) | ||
# we cannot handle direct indexing | ||
# use positional | ||
col = self.columns.get_loc(col) | ||
index = self.index.get_loc(index) | ||
return self._get_value(index, col, takeable=True) | ||
_get_value.__doc__ = get_value.__doc__ | ||
|
||
def set_value(self, index, col, value, takeable=False): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -498,6 +498,8 @@ def get_value(self, series, key): | |
k = self._convert_scalar_indexer(k, kind='getitem') | ||
indexer = self.get_loc(k) | ||
return series.iloc[indexer] | ||
except AttributeError: | ||
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. hmm, what is the attribute error you are catching? 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. The above line, 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. ok,, I would then put the AttributeError catching with the Key and Type error below then.? 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. The key and type errors catch exceptions from The exceptions cannot use They also cannot all use Therefore, two cases are needed. 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.
ok so IIUC, I don't think we would need the AttributeError catching then. 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. You're right. I've applied your recommended changes and added type information (the case where |
||
return super().get_value(series, indexer) | ||
except (KeyError, TypeError): | ||
pass | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -719,3 +719,11 @@ def test_map_with_dict_or_series(self): | |
output = cur_index.map(mapper) | ||
# Order of categories in output can be different | ||
tm.assert_index_equal(expected, output) | ||
|
||
def test_at_with_categorical_index(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. test both loc and at with here |
||
# GH 20629 | ||
s = Series([1, 2, 3], index=pd.CategoricalIndex(["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. move this after the loc tests |
||
assert s.at['A'] == 1 | ||
df = DataFrame([[1, 2], [3, 4], [5, 6]], | ||
index=pd.CategoricalIndex(["A", "B", "C"])) | ||
assert df.at['B', 1] == 4 |
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.
actually don't need the e here (just do
raise
) is enough