Always return ndarray · pandas-dev/pandas@7714e79 (original) (raw)

`@@ -739,14 +739,22 @@ def _create_method(cls, op, coerce_to_dtype=True):

`

739

739

` ----------

`

740

740

` op : function

`

741

741

` An operator that takes arguments op(a, b)

`

742

``

`-

coerce_to_dtype : bool

`

``

742

`+

coerce_to_dtype : bool, default True

`

743

743

` boolean indicating whether to attempt to convert

`

744

``

`-

the result to the underlying ExtensionArray dtype

`

745

``

`-

(default True)

`

``

744

`+

the result to the underlying ExtensionArray dtype.

`

``

745

`+

If it's not possible to create a new ExtensionArray with the

`

``

746

`+

values, an ndarray is returned instead.

`

746

747

``

747

748

` Returns

`

748

749

` -------

`

749

``

`-

A method that can be bound to a method of a class

`

``

750

`+

Callable[[Any, Any], Union[ndarray, ExtensionArray]]

`

``

751

`+

A method that can be bound to a class. When used, the method

`

``

752

`+

receives the two arguments, one of which is the instance of

`

``

753

`+

this class, and should return an ExtensionArray or an ndarray.

`

``

754

+

``

755

`+

Returning an ndarray may be necessary when the result of the

`

``

756

`` +

op cannot be stored in the ExtensionArray. The dtype of the

``

``

757

`+

ndarray uses NumPy's normal inference rules.

`

750

758

``

751

759

` Example

`

752

760

` -------

`

`@@ -757,7 +765,6 @@ def _create_method(cls, op, coerce_to_dtype=True):

`

757

765

` in the class definition of MyExtensionArray to create the operator

`

758

766

` for addition, that will be based on the operator implementation

`

759

767

` of the underlying elements of the ExtensionArray

`

760

``

-

761

768

` """

`

762

769

``

763

770

`def _binop(self, other):

`

`@@ -778,7 +785,12 @@ def convert_values(param):

`

778

785

`try:

`

779

786

`res = self._from_sequence(res)

`

780

787

`except Exception:

`

781

``

`-

res = np.asarray(res, dtype=object)

`

``

788

`+

https://github.com/pandas-dev/pandas/issues/22850

`

``

789

`+

We catch all regular exceptions here, and fall back

`

``

790

`+

to an ndarray.

`

``

791

`+

res = np.asarray(res)

`

``

792

`+

else:

`

``

793

`+

res = np.asarray(res)

`

782

794

``

783

795

`return res

`

784

796

``