-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
API / CoW: constructing DataFrame from DataFrame/BlockManager creates lazy copy #51239
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 1 commit
692af75
9d8902f
b1f20aa
9ce8b4a
dd40d8c
e090f8a
382ac97
f7b38e0
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 |
---|---|---|
|
@@ -238,6 +238,9 @@ Copy-on-Write improvements | |
a modification to the data happens) when constructing a Series from an existing | ||
Series with the default of ``copy=False`` (:issue:`50471`) | ||
|
||
- The :class:`DataFrame` constructor now will keep track of references when called | ||
with another :class:`DataFrame` or ``BlockManager``. | ||
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. Not sure if mentioning BlockManager here is worth it? 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, I wouldn't mention that (normal users should never pass 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. Thx, removed |
||
|
||
- Trying to set values using chained assignment (for example, ``df["a"][1:3] = 0``) | ||
will now always raise an exception when Copy-on-Write is enabled. In this mode, | ||
chained assignment can never work because we are always setting into a temporary | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -654,6 +654,8 @@ def __init__( | |
data = data._mgr | ||
|
||
if isinstance(data, (BlockManager, ArrayManager)): | ||
if using_copy_on_write(): | ||
data = data.copy(deep=False) | ||
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. The previous PR only did this for DataFrame, not for BlockManager. We have many places where we have the pattern of But, it should also be harmless (except for a bit of overheead), since those intermediate manager / blocks will go out of scope? 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 wanted to do this also for a manager, but this caused all sorts of problems because we kept the Manager alive. Yeah this is a safety net for something like
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. Yes if they are a true intermediate manager they go out of scope immediately. But if we forgot performing a shallow copy for some reason, this catches this 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. One more thing here, do you know what is the overhead of this shallow copy? Because our "fastpath" for DataFrame creation goes through here 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. Small test (not using this branch):
So in relative terms, it's not an insignificant change .. Not fully sure how important this is in real-world cases though. 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 it's not totally cheap, it's from 1 to 3 on my machine. We could keep it out of here if you prefer and investigate other methods of keeping the reference here? |
||
# first check if a Manager is passed without any other arguments | ||
# -> use fastpath (without checking Manager type) | ||
if index is None and columns is None and dtype is None and not copy: | ||
|
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.
IIRC, we don't tell users about how CoW is implemented under the hood.
Maybe we can say that the constructor will obey CoW rules when called with a DataFrame.
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.
Yeah this makes more sense, will change
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.
I would match this sentence with the bullet point above, since it is basically the same enhancement but for DataFrame instead of Series
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.
Done