-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Use lazy copy in infer objects #50428
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
3c4cee3
db5dba1
9eb4d85
081dd29
76c443b
f7724ff
a7b4e27
1d4f726
018cfe6
1696d8a
1782fbd
8cb6355
cead228
47d85b3
a3d0a2b
2e2ed0f
3a84382
64d550b
716cef8
f693829
97fa214
c3e4e66
5d687dd
f5bf65f
af8af95
f47f6dd
e357b64
bf1bb3b
5624983
9a6d516
8b0e2b0
3f832ba
9f11f0a
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 |
---|---|---|
|
@@ -748,6 +748,82 @@ def test_head_tail(method, using_copy_on_write): | |
tm.assert_frame_equal(df, df_orig) | ||
|
||
|
||
def test_infer_objects(using_copy_on_write): | ||
df = DataFrame({"a": [1, 2], "b": "c", "c": 1, "d": "x"}) | ||
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. It might be worth adding a test where one of the object dtype columns actually gets converted? (eg change d to 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. Good point, adjusted d in the two tests below to cover both cases. Good to go apart from that? |
||
df_orig = df.copy() | ||
df2 = df.infer_objects() | ||
|
||
if using_copy_on_write: | ||
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a")) | ||
assert np.shares_memory(get_array(df2, "b"), get_array(df, "b")) | ||
|
||
else: | ||
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a")) | ||
assert not np.shares_memory(get_array(df2, "b"), get_array(df, "b")) | ||
|
||
df2.iloc[0, 0] = 0 | ||
df2.iloc[0, 1] = "d" | ||
if using_copy_on_write: | ||
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a")) | ||
assert not np.shares_memory(get_array(df2, "b"), get_array(df, "b")) | ||
tm.assert_frame_equal(df, df_orig) | ||
|
||
|
||
def test_infer_objects_no_reference(using_copy_on_write): | ||
df = DataFrame( | ||
{ | ||
"a": [1, 2], | ||
"b": "c", | ||
"c": 1, | ||
"d": Series( | ||
[Timestamp("2019-12-31"), Timestamp("2020-12-31")], dtype="object" | ||
), | ||
"e": "b", | ||
} | ||
) | ||
df = df.infer_objects() | ||
|
||
arr_a = get_array(df, "a") | ||
arr_b = get_array(df, "b") | ||
arr_d = get_array(df, "d") | ||
|
||
df.iloc[0, 0] = 0 | ||
df.iloc[0, 1] = "d" | ||
df.iloc[0, 3] = Timestamp("2018-12-31") | ||
if using_copy_on_write: | ||
assert np.shares_memory(arr_a, get_array(df, "a")) | ||
# TODO(CoW): Block splitting causes references here | ||
assert not np.shares_memory(arr_b, get_array(df, "b")) | ||
assert np.shares_memory(arr_d, get_array(df, "d")) | ||
|
||
|
||
def test_infer_objects_reference(using_copy_on_write): | ||
df = DataFrame( | ||
{ | ||
"a": [1, 2], | ||
"b": "c", | ||
"c": 1, | ||
"d": Series( | ||
[Timestamp("2019-12-31"), Timestamp("2020-12-31")], dtype="object" | ||
), | ||
} | ||
) | ||
view = df[:] # noqa: F841 | ||
df = df.infer_objects() | ||
|
||
arr_a = get_array(df, "a") | ||
arr_b = get_array(df, "b") | ||
arr_d = get_array(df, "d") | ||
|
||
df.iloc[0, 0] = 0 | ||
df.iloc[0, 1] = "d" | ||
df.iloc[0, 3] = Timestamp("2018-12-31") | ||
if using_copy_on_write: | ||
assert not np.shares_memory(arr_a, get_array(df, "a")) | ||
assert not np.shares_memory(arr_b, get_array(df, "b")) | ||
assert np.shares_memory(arr_d, get_array(df, "d")) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"kwargs", | ||
[ | ||
|
Uh oh!
There was an error while loading. Please reload this page.