BUG: fix DataFrame+DataFrame op with timedelta64 dtype (#22696) · pandas-dev/pandas@08ecba8 (original) (raw)

`@@ -900,6 +900,42 @@ def invalid_comparison(left, right, op):

`

900

900

`return res_values

`

901

901

``

902

902

``

``

903

`+

-----------------------------------------------------------------------------

`

``

904

`+

Dispatch logic

`

``

905

+

``

906

`+

def should_series_dispatch(left, right, op):

`

``

907

`+

"""

`

``

908

`+

Identify cases where a DataFrame operation should dispatch to its

`

``

909

`+

Series counterpart.

`

``

910

+

``

911

`+

Parameters

`

``

912

`+


`

``

913

`+

left : DataFrame

`

``

914

`+

right : DataFrame

`

``

915

`+

op : binary operator

`

``

916

+

``

917

`+

Returns

`

``

918

`+


`

``

919

`+

override : bool

`

``

920

`+

"""

`

``

921

`+

if left._is_mixed_type or right._is_mixed_type:

`

``

922

`+

return True

`

``

923

+

``

924

`+

if not len(left.columns) or not len(right.columns):

`

``

925

`+

ensure obj.dtypes[0] exists for each obj

`

``

926

`+

return False

`

``

927

+

``

928

`+

ldtype = left.dtypes.iloc[0]

`

``

929

`+

rdtype = right.dtypes.iloc[0]

`

``

930

+

``

931

`+

if ((is_timedelta64_dtype(ldtype) and is_integer_dtype(rdtype)) or

`

``

932

`+

(is_timedelta64_dtype(rdtype) and is_integer_dtype(ldtype))):

`

``

933

`+

numpy integer dtypes as timedelta64 dtypes in this scenario

`

``

934

`+

return True

`

``

935

+

``

936

`+

return False

`

``

937

+

``

938

+

903

939

`# -----------------------------------------------------------------------------

`

904

940

`# Functions that add arithmetic methods to objects, given arithmetic factory

`

905

941

`# methods

`

`@@ -1803,8 +1839,10 @@ def f(self, other, axis=default_axis, level=None, fill_value=None):

`

1803

1839

``

1804

1840

`other = _align_method_FRAME(self, other, axis)

`

1805

1841

``

1806

``

`-

if isinstance(other, ABCDataFrame): # Another DataFrame

`

1807

``

`-

return self._combine_frame(other, na_op, fill_value, level)

`

``

1842

`+

if isinstance(other, ABCDataFrame):

`

``

1843

`+

Another DataFrame

`

``

1844

`+

pass_op = op if should_series_dispatch(self, other, op) else na_op

`

``

1845

`+

return self._combine_frame(other, pass_op, fill_value, level)

`

1808

1846

`elif isinstance(other, ABCSeries):

`

1809

1847

`return _combine_series_frame(self, other, na_op,

`

1810

1848

`fill_value=fill_value, axis=axis,

`