You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In order to control how arithmetic works between a custom type and a pandas type,
498
+
implement ``__pandas_priority__``. Similar to numpy's ``__array_priority__``
499
+
semantics, arithmetic methods on :class:`DataFrame`, :class:`Series`, and :class:`Index`
500
+
objects will delegate to ``other``, if it has an attribute ``__pandas_priority__`` with a higher value.
501
+
502
+
By default, pandas objects try to operate with other objects, even if they are not types known to pandas:
503
+
504
+
.. code-block:: python
505
+
506
+
>>> pd.Series([1, 2]) + [10, 20]
507
+
011
508
+
122
509
+
dtype: int64
510
+
511
+
In the example above, if ``[10, 20]`` was a custom type that can be understood as a list, pandas objects will still operate with it in the same way.
512
+
513
+
In some cases, it is useful to delegate to the other type the operation. For example, consider I implement a
514
+
custom list object, and I want the result of adding my custom list with a pandas :class:`Series` to be an instance of my list
515
+
and not a :class:`Series` as seen in the previous example. This is now possible by defining the ``__pandas_priority__`` attribute
516
+
of my custom list, and setting it to a higher value, than the priority of the pandas objects I want to operate with.
517
+
518
+
The ``__pandas_priority__`` of :class:`DataFrame`, :class:`Series`, and :class:`Index` are ``4000``, ``3000``, and ``2000`` respectively. The base ``ExtensionArray.__pandas_priority__`` is ``1000``.
519
+
520
+
.. code-block:: python
521
+
522
+
classCustomList(list):
523
+
__pandas_priority__ =5000
524
+
525
+
def__radd__(self, other):
526
+
# return `self` and not the addition for simplicity
527
+
returnself
528
+
529
+
custom = CustomList()
530
+
series = pd.Series([1, 2, 3])
531
+
532
+
# Series refuses to add custom, since it's an unknown type with higher priority
533
+
assert series.__add__(custom) isNotImplemented
534
+
535
+
# This will cause the custom class `__radd__` being used instead
Copy file name to clipboardExpand all lines: doc/source/whatsnew/v2.1.0.rst
+1
Original file line number
Diff line number
Diff line change
@@ -28,6 +28,7 @@ enhancement2
28
28
29
29
Other enhancements
30
30
^^^^^^^^^^^^^^^^^^
31
+
- Implemented ``__pandas_priority__`` to allow custom types to take precedence over :class:`DataFrame`, :class:`Series`, :class:`Index`, or :class:`ExtensionArray` for arithmetic operations, :ref:`see the developer guide <extending.pandas_priority>` (:issue:`48347`)
31
32
- :meth:`MultiIndex.sort_values` now supports ``na_position`` (:issue:`51612`)
32
33
- :meth:`MultiIndex.sortlevel` and :meth:`Index.sortlevel` gained a new keyword ``na_position`` (:issue:`51612`)
33
34
- Improve error message when setting :class:`DataFrame` with wrong number of columns through :meth:`DataFrame.isetitem` (:issue:`51701`)
0 commit comments