-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
DEPR: raise deprecation warning in numpy ufuncs on DataFrames if not aligned + fallback to <1.2.0 behaviour #39239
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
f5e9871
e02392a
c6f6898
8700321
1a6f257
64b9430
3b66b14
4dcde0e
20be3c7
097de71
f80b780
dabd47f
eaa83ed
81e7c84
4703410
5ed00bb
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
.. _whatsnew_121: | ||
|
||
What's new in 1.2.1 (January 18, 2021) | ||
What's new in 1.2.1 (January 20, 2021) | ||
-------------------------------------- | ||
|
||
These are the changes in pandas 1.2.1. See :ref:`release` for a full changelog | ||
|
@@ -42,6 +42,79 @@ As a result, bugs reported as fixed in pandas 1.2.0 related to inconsistent tick | |
|
||
.. --------------------------------------------------------------------------- | ||
|
||
.. _whatsnew_121.ufunc_deprecation: | ||
|
||
Calling NumPy ufuncs on non-aligned DataFrames | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Before pandas 1.2.0, calling a NumPy ufunc on non-aligned DataFrames (or | ||
DataFrame / Series combination) would ignore the indices, only match | ||
the inputs by shape, and use the index/columns of the first DataFrame for | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
the result: | ||
|
||
.. code-block:: python | ||
|
||
>>> df1 = pd.DataFrame({"a": [1, 2], "b": [3, 4]}, index=[0, 1]) | ||
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. this is an incorrect format |
||
... df2 = pd.DataFrame({"a": [1, 2], "b": [3, 4]}, index=[1, 2]) | ||
>>> df1 | ||
a b | ||
0 1 3 | ||
1 2 4 | ||
>>> df2 | ||
a b | ||
1 1 3 | ||
2 2 4 | ||
|
||
>>> np.add(df1, df2) | ||
a b | ||
0 2 6 | ||
1 4 8 | ||
|
||
This contrasts with how other pandas operations work, which first align | ||
the inputs: | ||
|
||
.. code-block:: python | ||
|
||
>>> df1 + df2 | ||
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. make an actual ipython block 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 need to use some plain code-blocks since part of the example is showing old behaviour (or behaviour that will change in the future), and so prefer to use then code-blocks for all examples, for consistency within this section 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 use ipython blocks everywhere, pls do 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. would like to change these to be consistent |
||
a b | ||
0 NaN NaN | ||
1 3.0 7.0 | ||
2 NaN NaN | ||
|
||
In pandas 1.2.0, we refactored how NumPy ufuncs are called on DataFrames, and | ||
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this started to align the inputs first (:issue:`39184`), as happens in other | ||
pandas operations and as it happens for ufuncs called on Series objects. | ||
|
||
For pandas 1.2.1, we restored the previous behaviour to avoid a breaking | ||
change, but the above example of ``np.add(df1, df2)`` with non-aligned inputs | ||
will now to raise a warning, and a future pandas 2.0 release will start | ||
aligning the inputs first (:issue:`39184`). Calling a NumPy ufunc on Series | ||
objects (eg ``np.add(s1, s2)``) already aligns and continues to do so. | ||
|
||
To avoid the warning and keep the current behaviour of ignoring the indices, | ||
convert one of the arguments to a NumPy array: | ||
|
||
.. code-block:: python | ||
|
||
>>> np.add(df1, np.asarray(df2)) | ||
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. use an actual ipython format |
||
a b | ||
0 2 6 | ||
1 4 8 | ||
|
||
To obtain the future behaviour and silence the warning, you can align manually | ||
before passing the arguments to the ufunc: | ||
|
||
.. code-block:: python | ||
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. pls do not use code-blocks except to show older code. these are so error prone |
||
|
||
>>> df1, df2 = df1.align(df2) | ||
>>> np.add(df1, df2) | ||
a b | ||
0 NaN NaN | ||
1 3.0 7.0 | ||
2 NaN NaN | ||
|
||
.. --------------------------------------------------------------------------- | ||
|
||
.. _whatsnew_121.bug_fixes: | ||
|
||
Bug fixes | ||
|
Uh oh!
There was an error while loading. Please reload this page.