-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
BUG/API: make setitem-inplace preserve dtype when possible with PandasArray, IntegerArray, FloatingArray #39044
Conversation
…sArray, IntegerArray, FloatingArray
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.
will really need to look closely. this is adding non-trivial code.
@@ -28,6 +32,31 @@ def dtype(request): | |||
return PandasDtype(np.dtype(request.param)) | |||
|
|||
|
|||
orig_setitem = pd.core.internals.Block.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.
can you use monkeypatch instead?
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 does use monkeypatch. the monkeypatched method calls the original method
rebased + green |
docstring updated + green |
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.
Can you summarize which behaviours are changed?
Eg from the description I would suppose that
df = pd.DataFrame({'a': [1, 2, 3]})
df.loc[:, 'a'] = pd.array([3, 4, 5])
changed behaviour to preserve the original dtype, but I don't see that directly tested?
Does this need a whatsnew?
@@ -193,7 +197,20 @@ class TestGetitem(base.BaseGetitemTests): | |||
|
|||
|
|||
class TestSetitem(base.BaseSetitemTests): | |||
pass | |||
def test_setitem_series(self, data, full_indexer): |
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.
Can you indicate here why it is overriding the base class?
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.
comment added
if not data._mask.any(): | ||
# GH#38896 like we do with ndarray, we set the values inplace | ||
# but cast to the new numpy dtype | ||
expected = pd.Series(data.to_numpy(data.dtype.numpy_dtype), name="data") |
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.
Why are we converting to the numpy dtype here? That's also not the original dtype?
am ok with this, @jorisvandenbossche if any addl comments. |
There are some questions in my last review above for which I was still waiting on an answer |
let's stick a pin in this until #39163 goes in; it will be easier to address outstanding questions/comments at that point |
mothballing until after #39163 |
xref #38896 (doesn't close). In general
df[:, "A"] = foo
tries to operate in-place before falling back to casting. This makes sure we do that whenfoo
is aPandasArray
,IntegerArray
, orFloatingArray
I guess could/should do the same for BooleanArray