-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
REF: Add Manager.column_setitem to set values into a single column (without intermediate series) #47074
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
REF: Add Manager.column_setitem to set values into a single column (without intermediate series) #47074
Changes from 1 commit
0e4c58e
a2aa8aa
ce0649b
103d1fe
d20b0cb
453eaba
be740ad
e63c7f6
025a3d4
caf7be8
8d7ee1a
25e903b
5e30199
9d4566f
faed070
ea063e6
3f30cab
db8e866
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3924,15 +3924,17 @@ def _set_value( | |
Sets whether or not index/col interpreted as indexers | ||
""" | ||
try: | ||
# setitem_inplace will do validation that may raise TypeError, | ||
# setitem will do validation that may raise TypeError, | ||
# ValueError, or LossySetitemError | ||
# breakpoint() | ||
if takeable: | ||
self._mgr.column_setitem(col, index, value) | ||
# error: Argument 2 to "column_setitem" of "BlockManager" has | ||
# incompatible type "Union[Hashable, Sequence[Hashable]]"; | ||
# expected "Union[int, slice, ndarray[Any, Any]]" | ||
self._mgr.column_setitem(col, index, value) # type: ignore[arg-type] | ||
else: | ||
icol = self.columns.get_loc(col) | ||
index = self.index.get_loc(index) | ||
self._mgr.column_setitem(icol, index, value) | ||
self._mgr.column_setitem(icol, index, value) # type: ignore[arg-type] | ||
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self._clear_item_cache() | ||
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 we make column_setitem always-inplace and make the clear_item_cache unecesssary? API-wise i think the always-inplace method is a lot nicer than the less predictable one 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. or maybe could make setitem_inplace ignore CoW since it is explicitly inplace? 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 main use case of The case here is only for I could add a separate inplace version or add an inplace keyword to
Even something that is explicitly inplace from a usage perspective will need to take care of CoW in #46958 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 pushed a version with an 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. @jbrockmendel do you have a preference on keeping this change with the 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. Actually it seems we still need to catch TypeError, for example for a MultiIndex case where So only removed LossySetitemError for now (and added a test case for setting with 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, can you add a comment about why TypeError is needed. what about ValueError? 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. Will add a comment about TypeError. Now, this catching of a ValueError alraedy was here before, so I am hesitant to remove it without looking further in detail at it. I would prefer to leave that for another PR. 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. So there are some cases where setitem actually raises a ValueError, eg when setting with an array-like that is not of length 1 (in this case of scalar 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. For example:
Without catching/reraising in iloc, the error would be "ValueError: setting an array element with a sequence", which is slightly less informative. I suppose we should actually see to make this handling consistent in the different code paths (so it directly raises the more informative error message in the first place), but that's out of scope for this PR. |
||
|
||
except (KeyError, TypeError, ValueError, LossySetitemError): | ||
|
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.
should this comment refer to column_setitem instead of just setitem?
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.
it also looks like the new method doesn't actually do the validation this comment refers to?
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.
This is an existing comment (originally a few lines below), but so I suppose this comment was actually already not up to date anymore.
So before this PR the comment was about
setitem_inplace
, and that also doesn't do any validation.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.
setitem_inplace calls np_can_hold_element, which raises on failure
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.
Ah, yes, I see. That is doing validation that can raise the LossySetitemError. So the new
column_setitem
callssetitem
, which will also callnp_can_hold_element
, but there catching the LossySetitemError and coercing to the target dtype if needed.That is something that the loc/iloc fallback below otherwise will also do, so I suppose this change is OK (but the comment is then indeed no longer correct, and we also don't need to catch LossySetitemError 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.
yah, possibly also TypeError and ValueError