-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: DataFrame.update not operating in-place for datetime64[ns, UTC] dtype #56228
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 |
---|---|---|
|
@@ -8838,14 +8838,14 @@ def update( | |
in the original dataframe. | ||
|
||
>>> df = pd.DataFrame({'A': [1, 2, 3], | ||
... 'B': [400, 500, 600]}) | ||
... 'B': [400., 500., 600.]}) | ||
>>> new_df = pd.DataFrame({'B': [4, np.nan, 6]}) | ||
>>> df.update(new_df) | ||
>>> df | ||
A B | ||
0 1 4 | ||
1 2 500 | ||
2 3 6 | ||
A B | ||
0 1 4.0 | ||
1 2 500.0 | ||
2 3 6.0 | ||
""" | ||
if not PYPY and using_copy_on_write(): | ||
if sys.getrefcount(self) <= REF_COUNT: | ||
|
@@ -8862,8 +8862,6 @@ def update( | |
stacklevel=2, | ||
) | ||
|
||
from pandas.core.computation import expressions | ||
|
||
# TODO: Support other joins | ||
if join != "left": # pragma: no cover | ||
raise NotImplementedError("Only left join is supported") | ||
|
@@ -8897,7 +8895,7 @@ def update( | |
if mask.all(): | ||
continue | ||
|
||
self.loc[:, col] = expressions.where(mask, this, that) | ||
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 I suppose numexpr generally will prevent inplace operations? 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. Being inplace here is generally not a good idea since non inplace methods are faster if you replace the whole column |
||
self.loc[:, col] = self[col].where(mask, that) | ||
|
||
# ---------------------------------------------------------------------- | ||
# Data reshaping | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,6 +140,22 @@ def test_update_datetime_tz(self): | |
expected = DataFrame([pd.Timestamp("2019", tz="UTC")]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_update_datetime_tz_in_place(self, using_copy_on_write, warn_copy_on_write): | ||
# https://github.com/pandas-dev/pandas/issues/56227 | ||
result = DataFrame([pd.Timestamp("2019", tz="UTC")]) | ||
orig = result.copy() | ||
view = result[:] | ||
with tm.assert_produces_warning( | ||
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 use |
||
FutureWarning if warn_copy_on_write else None, match="Setting a value" | ||
): | ||
result.update(result + pd.Timedelta(days=1)) | ||
expected = DataFrame([pd.Timestamp("2019-01-02", tz="UTC")]) | ||
tm.assert_frame_equal(result, expected) | ||
if not using_copy_on_write: | ||
tm.assert_frame_equal(view, expected) | ||
else: | ||
tm.assert_frame_equal(view, orig) | ||
|
||
def test_update_with_different_dtype(self, using_copy_on_write): | ||
# GH#3217 | ||
df = DataFrame({"a": [1, 3], "b": [np.nan, 2]}) | ||
|
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.
Changing these to floats, because otherwise we'd get
which looks correct because we're trying to update an int column
df['B']
with a float onenew_df['B']