-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
CoW warning mode: clean-up some TODOs #56080
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
phofl
merged 3 commits into
pandas-dev:main
from
jorisvandenbossche:cow-warnings-cleanup
Nov 20, 2023
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 |
---|---|---|
|
@@ -163,7 +163,6 @@ def test_subset_column_slice( | |
subset.iloc[0, 0] = 0 | ||
assert not np.shares_memory(get_array(subset, "b"), get_array(df, "b")) | ||
elif warn_copy_on_write: | ||
# TODO(CoW-warn) should warn | ||
with tm.assert_cow_warning(single_block): | ||
subset.iloc[0, 0] = 0 | ||
else: | ||
|
@@ -334,7 +333,6 @@ def test_subset_set_with_row_indexer( | |
): | ||
pytest.skip("setitem with labels selects on columns") | ||
|
||
# TODO(CoW-warn) should warn | ||
if using_copy_on_write: | ||
indexer_si(subset)[indexer] = 0 | ||
elif warn_copy_on_write: | ||
|
@@ -369,7 +367,8 @@ def test_subset_set_with_mask(backend, using_copy_on_write, warn_copy_on_write): | |
|
||
mask = subset > 3 | ||
|
||
# TODO(CoW-warn) should warn | ||
# TODO(CoW-warn) should warn -> mask is a DataFrame, which ends up going through | ||
# DataFrame._where(..., inplace=True) | ||
if using_copy_on_write or warn_copy_on_write: | ||
subset[mask] = 0 | ||
else: | ||
|
@@ -403,7 +402,6 @@ def test_subset_set_column(backend, using_copy_on_write, warn_copy_on_write): | |
else: | ||
arr = pd.array([10, 11], dtype="Int64") | ||
|
||
# TODO(CoW-warn) should warn | ||
if using_copy_on_write or warn_copy_on_write: | ||
subset["a"] = arr | ||
else: | ||
|
@@ -512,7 +510,6 @@ def test_subset_set_columns(backend, using_copy_on_write, warn_copy_on_write, dt | |
df_orig = df.copy() | ||
subset = df[1:3] | ||
|
||
# TODO(CoW-warn) should warn | ||
if using_copy_on_write or warn_copy_on_write: | ||
subset[["a", "c"]] = 0 | ||
Comment on lines
-515
to
514
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. Similarly here, replacing full columns, so don't have to warn |
||
else: | ||
|
@@ -877,6 +874,8 @@ def test_series_subset_set_with_indexer( | |
) | ||
if warn_copy_on_write: | ||
# TODO(CoW-warn) should also warn for setting with mask | ||
# -> Series.__setitem__ with boolean mask ends up using Series._set_values | ||
# or Series._where depending on value being set | ||
with tm.assert_cow_warning( | ||
not is_mask, raise_on_extra_warnings=warn is not None | ||
): | ||
|
@@ -1006,6 +1005,7 @@ def test_column_as_series_set_with_upcast( | |
s[0] = "foo" | ||
expected = Series([1, 2, 3], name="a") | ||
elif using_copy_on_write or warn_copy_on_write or using_array_manager: | ||
# TODO(CoW-warn) assert the FutureWarning for CoW is also raised | ||
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"): | ||
s[0] = "foo" | ||
expected = Series(["foo", 2, 3], dtype=object, name="a") | ||
|
@@ -1130,6 +1130,7 @@ def test_set_value_copy_only_necessary_column( | |
view = df[:] | ||
|
||
if val == "a" and indexer[0] != slice(None): | ||
# TODO(CoW-warn) assert the FutureWarning for CoW is also raised | ||
with tm.assert_produces_warning( | ||
FutureWarning, match="Setting an item of incompatible dtype is deprecated" | ||
): | ||
|
@@ -1154,6 +1155,8 @@ def test_series_midx_slice(using_copy_on_write): | |
ser = Series([1, 2, 3], index=pd.MultiIndex.from_arrays([[1, 1, 2], [3, 4, 5]])) | ||
result = ser[1] | ||
assert np.shares_memory(get_array(ser), get_array(result)) | ||
# TODO(CoW-warn) should warn -> reference is only tracked in CoW mode, so | ||
# warning is not triggered | ||
result.iloc[0] = 100 | ||
if using_copy_on_write: | ||
expected = Series( | ||
|
@@ -1162,7 +1165,9 @@ def test_series_midx_slice(using_copy_on_write): | |
tm.assert_series_equal(ser, expected) | ||
|
||
|
||
def test_getitem_midx_slice(using_copy_on_write, using_array_manager): | ||
def test_getitem_midx_slice( | ||
using_copy_on_write, warn_copy_on_write, using_array_manager | ||
): | ||
df = DataFrame({("a", "x"): [1, 2], ("a", "y"): 1, ("b", "x"): 2}) | ||
df_orig = df.copy() | ||
new_df = df[("a",)] | ||
|
@@ -1175,6 +1180,15 @@ def test_getitem_midx_slice(using_copy_on_write, using_array_manager): | |
if using_copy_on_write: | ||
new_df.iloc[0, 0] = 100 | ||
tm.assert_frame_equal(df_orig, df) | ||
else: | ||
if warn_copy_on_write: | ||
with tm.assert_cow_warning(): | ||
new_df.iloc[0, 0] = 100 | ||
else: | ||
with pd.option_context("chained_assignment", "warn"): | ||
with tm.assert_produces_warning(SettingWithCopyWarning): | ||
new_df.iloc[0, 0] = 100 | ||
assert df.iloc[0, 0] == 100 | ||
|
||
|
||
def test_series_midx_tuples_slice(using_copy_on_write): | ||
|
@@ -1184,6 +1198,8 @@ def test_series_midx_tuples_slice(using_copy_on_write): | |
) | ||
result = ser[(1, 2)] | ||
assert np.shares_memory(get_array(ser), get_array(result)) | ||
# TODO(CoW-warn) should warn -> reference is only tracked in CoW mode, so | ||
# warning is not triggered | ||
result.iloc[0] = 100 | ||
if using_copy_on_write: | ||
expected = Series( | ||
|
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
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.
We are replacing a full column (so never updating the original), so while this gives a SettingWithCopyWarning right now, we don't have to warn anything here for CoW