Merge remote-tracking branch 'upstream/master' into ea-divmod · pandas-dev/pandas@cc2bfc8 (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):

`

`@@ -774,20 +781,24 @@ def convert_values(param):

`

774

781

`# a TypeError should be raised

`

775

782

`res = [op(a, b) for (a, b) in zip(lvalues, rvalues)]

`

776

783

``

777

``

`-

if coerce_to_dtype:

`

778

``

`-

if op.name in {'divmod', 'rdivmod'}:

`

``

784

`+

def _maybe_convert(arr):

`

``

785

`+

if coerce_to_dtype:

`

``

786

`+

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

`

``

787

`+

We catch all regular exceptions here, and fall back

`

``

788

`+

to an ndarray.

`

779

789

`try:

`

780

``

`-

a, b = zip(*res)

`

781

``

`-

res = (self._from_sequence(a),

`

782

``

`-

self._from_sequence(b))

`

783

``

`-

except TypeError:

`

784

``

`-

pass

`

``

790

`+

res = self._from_sequnce(arr)

`

``

791

`+

except Exception:

`

``

792

`+

res = np.asarray(arr)

`

785

793

`else:

`

786

``

`-

try:

`

787

``

`-

res = self._from_sequence(res)

`

788

``

`-

except TypeError:

`

789

``

`-

pass

`

``

794

`+

res = np.asarray(arr)

`

``

795

`+

return res

`

790

796

``

``

797

`+

if op.name in {'divmod', 'rdivmod'}:

`

``

798

`+

a, b = zip(*res)

`

``

799

`+

res = _maybe_convert(a), _maybe_convert(b)

`

``

800

`+

else:

`

``

801

`+

res = _maybe_convert(res)

`

791

802

`return res

`

792

803

``

793

804

`op_name = ops._get_op_name(op, True)

`