-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Add CoW optimization to interpolate #51249
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 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2cef799
ENH: Implement CoW for interpolate
phofl 26f3972
Fix and add first test
phofl 27648c6
Add tests
phofl 80c95de
Add tests
phofl d132161
ENH: Add CoW optimization to interpolate
phofl 644af0c
Remove comment
phofl 425f8f4
Fix
phofl 3119c84
Fix
phofl 58bffc9
Fix tests
phofl 254de7d
Simplify
phofl 30ac5f3
Simplify
phofl 1e82658
Merge branch 'main' into cow_interp
phofl 64b40ce
Update pandas/tests/copy_view/test_interp_fillna.py
phofl 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
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 |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas import ( | ||
DataFrame, | ||
NaT, | ||
Series, | ||
Timestamp, | ||
) | ||
import pandas._testing as tm | ||
from pandas.tests.copy_view.util import get_array | ||
|
||
|
||
@pytest.mark.parametrize("method", ["pad", "nearest", "linear"]) | ||
def test_interpolate_no_op(using_copy_on_write, method): | ||
df = DataFrame({"a": [1, 2]}) | ||
df_orig = df.copy() | ||
|
||
result = df.interpolate(method=method) | ||
|
||
if using_copy_on_write: | ||
assert np.shares_memory(get_array(result, "a"), get_array(df, "a")) | ||
else: | ||
assert not np.shares_memory(get_array(result, "a"), get_array(df, "a")) | ||
|
||
result.iloc[0, 0] = 100 | ||
|
||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(result, "a"), get_array(df, "a")) | ||
tm.assert_frame_equal(df, df_orig) | ||
|
||
|
||
@pytest.mark.parametrize("func", ["ffill", "bfill"]) | ||
def test_interp_fill_functions(using_copy_on_write, func): | ||
# Check that these takes the same code paths as interpolate | ||
df = DataFrame({"a": [1, 2]}) | ||
df_orig = df.copy() | ||
|
||
result = getattr(df, func)() | ||
|
||
if using_copy_on_write: | ||
assert np.shares_memory(get_array(result, "a"), get_array(df, "a")) | ||
else: | ||
assert not np.shares_memory(get_array(result, "a"), get_array(df, "a")) | ||
|
||
result.iloc[0, 0] = 100 | ||
|
||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(result, "a"), get_array(df, "a")) | ||
tm.assert_frame_equal(df, df_orig) | ||
|
||
|
||
@pytest.mark.parametrize("func", ["ffill", "bfill"]) | ||
@pytest.mark.parametrize( | ||
"vals", [[1, np.nan, 2], [Timestamp("2019-12-31"), NaT, Timestamp("2020-12-31")]] | ||
) | ||
def test_interpolate_triggers_copy(using_copy_on_write, vals, func): | ||
df = DataFrame({"a": vals}) | ||
result = getattr(df, func)() | ||
|
||
assert not np.shares_memory(get_array(result, "a"), get_array(df, "a")) | ||
if using_copy_on_write: | ||
# Check that we don't have references when triggering a copy | ||
assert result._mgr._has_no_reference(0) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"vals", [[1, np.nan, 2], [Timestamp("2019-12-31"), NaT, Timestamp("2020-12-31")]] | ||
) | ||
def test_interpolate_inplace_no_reference_no_copy(using_copy_on_write, vals): | ||
df = DataFrame({"a": vals}) | ||
arr = get_array(df, "a") | ||
df.interpolate(method="linear", inplace=True) | ||
|
||
assert np.shares_memory(arr, get_array(df, "a")) | ||
if using_copy_on_write: | ||
# Check that we don't have references when triggering a copy | ||
assert df._mgr._has_no_reference(0) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"vals", [[1, np.nan, 2], [Timestamp("2019-12-31"), NaT, Timestamp("2020-12-31")]] | ||
) | ||
def test_interpolate_inplace_with_refs(using_copy_on_write, vals): | ||
df = DataFrame({"a": [1, np.nan, 2]}) | ||
df_orig = df.copy() | ||
arr = get_array(df, "a") | ||
view = df[:] | ||
df.interpolate(method="linear", inplace=True) | ||
|
||
if using_copy_on_write: | ||
# Check that copy was triggered in interpolate and that we don't | ||
# have any references left | ||
assert not np.shares_memory(arr, get_array(df, "a")) | ||
tm.assert_frame_equal(df_orig, view) | ||
assert df._mgr._has_no_reference(0) | ||
assert view._mgr._has_no_reference(0) | ||
else: | ||
assert np.shares_memory(arr, get_array(df, "a")) | ||
|
||
|
||
def test_interpolate_cleaned_fill_method(using_copy_on_write): | ||
# Check that "method is set to None" case works correctly | ||
df = DataFrame({"a": ["a", np.nan, "c"], "b": 1}) | ||
df_orig = df.copy() | ||
|
||
result = df.interpolate(method="asfreq") | ||
|
||
if using_copy_on_write: | ||
assert np.shares_memory(get_array(result, "a"), get_array(df, "a")) | ||
else: | ||
assert not np.shares_memory(get_array(result, "a"), get_array(df, "a")) | ||
|
||
result.iloc[0, 0] = Timestamp("2021-12-31") | ||
|
||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(result, "a"), get_array(df, "a")) | ||
tm.assert_frame_equal(df, df_orig) | ||
|
||
|
||
def test_interpolate_object_convert_no_op(using_copy_on_write): | ||
df = DataFrame({"a": ["a", "b", "c"], "b": 1}) | ||
arr_a = get_array(df, "a") | ||
df.interpolate(method="pad", inplace=True) | ||
|
||
# No CoW makes a copy, it should not! | ||
if using_copy_on_write: | ||
assert df._mgr._has_no_reference(0) | ||
assert np.shares_memory(arr_a, get_array(df, "a")) | ||
|
||
|
||
def test_interpolate_object_convert_copies(using_copy_on_write): | ||
df = DataFrame({"a": Series([1, 2], dtype=object), "b": 1}) | ||
arr_a = get_array(df, "a") | ||
df.interpolate(method="pad", inplace=True) | ||
|
||
if using_copy_on_write: | ||
assert df._mgr._has_no_reference(0) | ||
assert not np.shares_memory(arr_a, get_array(df, "a")) | ||
|
||
|
||
def test_interpolate_downcast(using_copy_on_write): | ||
df = DataFrame({"a": [1, np.nan, 2.5], "b": 1}) | ||
arr_a = get_array(df, "a") | ||
df.interpolate(method="pad", inplace=True, downcast="infer") | ||
|
||
if using_copy_on_write: | ||
assert df._mgr._has_no_reference(0) | ||
assert np.shares_memory(arr_a, get_array(df, "a")) | ||
|
||
|
||
def test_interpolate_downcast_reference_triggers_copy(using_copy_on_write): | ||
df = DataFrame({"a": [1, np.nan, 2.5], "b": 1}) | ||
df_orig = df.copy() | ||
arr_a = get_array(df, "a") | ||
view = df[:] | ||
df.interpolate(method="pad", inplace=True, downcast="infer") | ||
|
||
if using_copy_on_write: | ||
assert df._mgr._has_no_reference(0) | ||
assert not np.shares_memory(arr_a, get_array(df, "a")) | ||
tm.assert_frame_equal(df_orig, view) | ||
else: | ||
tm.assert_frame_equal(df, view) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.