-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: DataFrame.at with CategoricalIndex #41846
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,6 +158,7 @@ | |
from pandas.core.indexers import check_key_length | ||
from pandas.core.indexes import base as ibase | ||
from pandas.core.indexes.api import ( | ||
CategoricalIndex, | ||
DatetimeIndex, | ||
Index, | ||
PeriodIndex, | ||
|
@@ -3569,6 +3570,11 @@ def _get_value(self, index, col, takeable: bool = False) -> Scalar: | |
Returns | ||
------- | ||
scalar | ||
|
||
Notes | ||
----- | ||
Assumes that index and columns both have ax._index_as_unique; | ||
caller is responsible for checking. | ||
""" | ||
if takeable: | ||
series = self._ixs(col, axis=1) | ||
|
@@ -3577,20 +3583,21 @@ def _get_value(self, index, col, takeable: bool = False) -> Scalar: | |
series = self._get_item_cache(col) | ||
engine = self.index._engine | ||
|
||
if isinstance(self.index, CategoricalIndex): | ||
# Trying to use the engine fastpath may give incorrect results | ||
# if our categories are integers that dont match our codes | ||
col = self.columns.get_loc(col) | ||
index = self.index.get_loc(index) | ||
return self._get_value(index, col, takeable=True) | ||
|
||
try: | ||
loc = engine.get_loc(index) | ||
return series._values[loc] | ||
except KeyError: | ||
# GH 20629 | ||
if self.index.nlevels > 1: | ||
# partial indexing forbidden | ||
raise | ||
|
||
# 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) | ||
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. on the list to add? 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. once this is merged ill push my branch that implements IntervalTree.get_loc, getting rid of the AttributeError |
||
# IntervalTree has no get_loc | ||
col = self.columns.get_loc(col) | ||
index = self.index.get_loc(index) | ||
return self._get_value(index, col, takeable=True) | ||
|
||
def __setitem__(self, key, value): | ||
key = com.apply_if_callable(key, self) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
don't we have this as a base Index property? (or it can raise NotImplementedError), e.g. dont' like the AttributeError checking here.
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.
ill rephrase, it isn't that they have the attribute, it is that this is only called
if self.index._index_as_unique and self.columns._index_as_unique