Created
December 14, 2017 20:44
-
-
Save ssanderson/ae8cb7c71958b02b9a819e743e87fc58 to your computer and use it in GitHub Desktop.
Copying Behavior in DataFrame.rename
This file contains 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
In [44]: df | |
Out[44]: | |
a b | |
0 1 2 | |
1 2 3 | |
2 3 4 | |
In [45]: renamed = df.rename(columns={'b': 'c'}) | |
In [46]: renamed.iloc[0, 0] = 1000 | |
In [47]: renamed | |
Out[47]: | |
a c | |
0 1000 2 | |
1 2 3 | |
2 3 4 | |
In [48]: df # renamed has a copy of the data, so mutating it doesn't affect the parent. | |
Out[48]: | |
a b | |
0 1 2 | |
1 2 3 | |
2 3 4 | |
In [49]: renamed_no_copy = df.rename(columns={'b': 'c'}, copy=False) # Passing copy=False avoids copies when possible. | |
In [50]: renamed_no_copy.iloc[0, 0] = 1000 | |
In [51]: renamed_no_copy | |
Out[51]: | |
a c | |
0 1000 2 | |
1 2 3 | |
2 3 4 | |
In [52]: df # Changes to the child now propagate to the parent, because they share the same underlying data. | |
Out[52]: # This is good when you care about saving memory, but can be a source of bugs if you're not expecting | |
a b # it, which is (I assume) why it's not the default behavior. | |
0 1000 2 | |
1 2 3 | |
2 3 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment