-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC clarify inplace operation section in 1.5 whats_new #47433
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
mroeschke
merged 5 commits into
pandas-dev:main
from
lesteve:enh-inplace-iloc-whats-new
Jun 30, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5334e6a
DOC make inplace operation section in whats_new clearer
lesteve fae74ac
tweak
lesteve 23cca41
Can happen with both loc and iloc when setting entire column
lesteve d189395
Use more meaningful data
lesteve fbe6202
Merge branch 'main' into enh-inplace-iloc-whats-new
mroeschke 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -572,31 +572,37 @@ As ``group_keys=True`` is the default value of :meth:`DataFrame.groupby` and | |
raise a ``FutureWarning``. This can be silenced and the previous behavior | ||
retained by specifying ``group_keys=False``. | ||
|
||
.. _whatsnew_150.notable_bug_fixes.setitem_column_try_inplace: | ||
.. _whatsnew_150.deprecations.setitem_column_try_inplace: | ||
_ see also _whatsnew_130.notable_bug_fixes.setitem_column_try_inplace | ||
|
||
Try operating inplace when setting values with ``loc`` and ``iloc`` | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
Inplace operation when setting values with ``loc`` and ``iloc`` | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
Most of the time setting values with ``frame.iloc`` attempts to set values | ||
in-place, only falling back to inserting a new array if necessary. In the past, | ||
setting entire columns has been an exception to this rule: | ||
inplace, only falling back to inserting a new array if necessary. There are | ||
some cases where this rule is not followed, for example when setting an entire | ||
column from an array with different dtype: | ||
|
||
.. ipython:: python | ||
|
||
values = np.arange(4).reshape(2, 2) | ||
df = pd.DataFrame(values) | ||
ser = df[0] | ||
df = pd.DataFrame({'price': [11.1, 12.2]}, index=['book1', 'book2']) | ||
original_prices = df['price'] | ||
new_prices = np.array([98, 99]) | ||
|
||
*Old behavior*: | ||
|
||
.. code-block:: ipython | ||
|
||
In [3]: df.iloc[:, 0] = np.array([10, 11]) | ||
In [4]: ser | ||
In [3]: df.iloc[:, 0] = new_prices | ||
In [4]: df.iloc[:, 0] | ||
Out[4]: | ||
0 0 | ||
1 2 | ||
Name: 0, dtype: int64 | ||
book1 98 | ||
book2 99 | ||
Name: price, dtype: int64 | ||
In [5]: original_prices | ||
Out[5]: | ||
book1 11.1 | ||
book2 12.2 | ||
Name: price, float: 64 | ||
|
||
This behavior is deprecated. In a future version, setting an entire column with | ||
iloc will attempt to operate inplace. | ||
|
@@ -605,39 +611,52 @@ iloc will attempt to operate inplace. | |
|
||
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. Maybe worth adding |
||
.. code-block:: ipython | ||
|
||
In [3]: df.iloc[:, 0] = np.array([10, 11]) | ||
In [4]: ser | ||
In [3]: df.iloc[:, 0] = new_prices | ||
In [4]: df.iloc[:, 0] | ||
Out[4]: | ||
0 10 | ||
1 11 | ||
Name: 0, dtype: int64 | ||
book1 98.0 | ||
book2 99.0 | ||
Name: price, dtype: float64 | ||
In [5]: original_prices | ||
Out[5]: | ||
book1 98.0 | ||
book2 99.0 | ||
Name: price, dtype: float64 | ||
|
||
To get the old behavior, use :meth:`DataFrame.__setitem__` directly: | ||
|
||
*Future behavior*: | ||
|
||
.. code-block:: ipython | ||
|
||
In [5]: df[0] = np.array([21, 31]) | ||
In [4]: ser | ||
Out[4]: | ||
0 10 | ||
1 11 | ||
Name: 0, dtype: int64 | ||
|
||
In the case where ``df.columns`` is not unique, use :meth:`DataFrame.isetitem`: | ||
|
||
*Future behavior*: | ||
In [3]: df[df.columns[0]] = new_prices | ||
In [4]: df.iloc[:, 0] | ||
Out[4] | ||
book1 98 | ||
book2 99 | ||
Name: price, dtype: int64 | ||
In [5]: original_prices | ||
Out[5]: | ||
book1 11.1 | ||
book2 12.2 | ||
Name: price, dtype: float64 | ||
|
||
To get the old behaviour when ``df.columns`` is not unique and you want to | ||
change a single column by index, you can use :meth:`DataFrame.isetitem`, which | ||
has been added in pandas 1.5: | ||
|
||
.. code-block:: ipython | ||
|
||
In [5]: df.columns = ["A", "A"] | ||
In [5]: df.isetitem(0, np.array([21, 31])) | ||
In [4]: ser | ||
In [3]: df_with_duplicated_cols = pd.concat([df, df], axis='columns') | ||
In [3]: df_with_duplicated_cols.isetitem(0, new_prices) | ||
In [4]: df_with_duplicated_cols.iloc[:, 0] | ||
Out[4]: | ||
0 10 | ||
1 11 | ||
Name: 0, dtype: int64 | ||
book1 98 | ||
book2 99 | ||
Name: price, dtype: int64 | ||
In [5]: original_prices | ||
Out[5]: | ||
book1 11.1 | ||
book2 12.2 | ||
Name: 0, dtype: float64 | ||
|
||
.. _whatsnew_150.deprecations.numeric_only_default: | ||
|
||
|
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.
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.
side-comment: the
_ see also ...
line is not rendered in the HTML, not sure whether it was supposed to be a link to https://pandas.pydata.org/docs/dev/whatsnew/v1.3.0.html#try-operating-inplace-when-setting-values-with-loc-and-ilocThere 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 it looks like it's supposed to reference that section. Mind correcting it? Can be in the body of the paragraph too like
Please reference `this section <whatsnew_130.notable_bug_fixes.setitem_column_try_inplace>` of the 1.3 whatsnew file.