-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
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 12 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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
cast, | ||
final, | ||
) | ||
import weakref | ||
|
||
import numpy as np | ||
|
||
|
@@ -451,12 +452,20 @@ def convert( | |
self, | ||
*, | ||
copy: bool = True, | ||
using_copy_on_write: bool = False, | ||
original_blocks: list[Block] = [], | ||
) -> list[Block]: | ||
""" | ||
attempt to coerce any object types to better types return a copy | ||
of the block (if copy = True) by definition we are not an ObjectBlock | ||
here! | ||
""" | ||
if not copy and using_copy_on_write: | ||
result = self.copy(deep=False) | ||
result._ref = weakref.ref( # type: ignore[attr-defined] | ||
original_blocks[self.mgr_locs.as_array[0]] | ||
) | ||
return [result] | ||
return [self.copy()] if copy else [self] | ||
|
||
# --------------------------------------------------------------------- | ||
|
@@ -1963,6 +1972,8 @@ def convert( | |
self, | ||
*, | ||
copy: bool = True, | ||
using_copy_on_write: bool = False, | ||
original_blocks: list[Block] = [], | ||
) -> list[Block]: | ||
""" | ||
attempt to cast any object types to better types return a copy of | ||
|
@@ -1971,6 +1982,12 @@ def convert( | |
if self.dtype != _dtype_obj: | ||
# GH#50067 this should be impossible in ObjectBlock, but until | ||
# that is fixed, we short-circuit here. | ||
if using_copy_on_write: | ||
result = self.copy(deep=False) | ||
result._ref = weakref.ref( # type: ignore[attr-defined] | ||
original_blocks[self.mgr_locs.as_array[0]] | ||
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. I was going to say: "isn't 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. Yeah the block splitting is the problem here. self is a temporary block that is dead the moment we get back to the manager level. |
||
) | ||
return [result] | ||
return [self] | ||
|
||
values = self.values | ||
|
@@ -1986,10 +2003,16 @@ def convert( | |
convert_period=True, | ||
convert_interval=True, | ||
) | ||
ref = None | ||
if copy and res_values is values: | ||
res_values = values.copy() | ||
elif res_values is values and using_copy_on_write: | ||
ref = weakref.ref(original_blocks[self.mgr_locs.as_array[0]]) | ||
|
||
res_values = ensure_block_shape(res_values, self.ndim) | ||
return [self.make_block(res_values)] | ||
result = self.make_block(res_values) | ||
result._ref = ref # type: ignore[attr-defined] | ||
return [result] | ||
|
||
|
||
# ----------------------------------------------------------------- | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -441,11 +441,27 @@ def fillna(self: T, value, limit, inplace: bool, downcast) -> T: | |||||||||||||||||||
def astype(self: T, dtype, copy: bool = False, errors: str = "raise") -> T: | ||||||||||||||||||||
return self.apply("astype", dtype=dtype, copy=copy, errors=errors) | ||||||||||||||||||||
|
||||||||||||||||||||
def convert(self: T, copy: bool) -> T: | ||||||||||||||||||||
return self.apply( | ||||||||||||||||||||
def convert(self: T, copy: bool | None) -> T: | ||||||||||||||||||||
if not copy and using_copy_on_write(): | ||||||||||||||||||||
copy = False | ||||||||||||||||||||
elif copy is None: | ||||||||||||||||||||
copy = True | ||||||||||||||||||||
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.
Suggested change
I know this does exactly the same in the end and is not shorter, but personally I find it more readable (it's clearer that only copy=None case is handled to translate it to True/False depending on the setting, while in the current code you are basically also overwriting copy=False with copy=False). (and this is also the same pattern as used elsewhere, so ideally keep things consistent either way or the other) 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. Changed |
||||||||||||||||||||
|
||||||||||||||||||||
if self.is_single_block: | ||||||||||||||||||||
original_blocks = [self.blocks[0]] * self.shape[0] | ||||||||||||||||||||
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. Why is the 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. We are splitting blocks so we need the original blocks references * the number of columns 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. Only in case of object dtype, right? (although I suppose this list multiplication is not necessarily expensive, so maybe not worth to avoid doing when not needed) 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. No it's not that easy unfortunately, Non object blocks are never copied, hence we need the references especially in this case. While object blocks might get copied (depends on the inference), so we might need the references there. |
||||||||||||||||||||
else: | ||||||||||||||||||||
original_blocks = [self.blocks[i] for i in self.blknos] | ||||||||||||||||||||
mgr = self.apply( | ||||||||||||||||||||
"convert", | ||||||||||||||||||||
copy=copy, | ||||||||||||||||||||
using_copy_on_write=using_copy_on_write(), | ||||||||||||||||||||
original_blocks=original_blocks, | ||||||||||||||||||||
) | ||||||||||||||||||||
refs = [getattr(blk, "_ref", None) for blk in mgr.blocks] | ||||||||||||||||||||
if any(ref is not None for ref in refs): | ||||||||||||||||||||
mgr.refs = refs | ||||||||||||||||||||
mgr.parent = self | ||||||||||||||||||||
return mgr | ||||||||||||||||||||
|
||||||||||||||||||||
def replace(self: T, to_replace, value, inplace: bool) -> T: | ||||||||||||||||||||
inplace = validate_bool_kwarg(inplace, "inplace") | ||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -439,6 +439,27 @@ 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) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"kwargs", | ||
[ | ||
|
Uh oh!
There was an error while loading. Please reload this page.