Skip to content

BUG: arithmetic operations disobey array priority #45803

Closed
@FabianHofmann

Description

@FabianHofmann

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np

class AlwaysOne(float):

    __array_priority__ = 10000
    __array_ufunc__ = None

    def __rmul__(self, other): 
        return 1

    def __mul__(self, other): 
        return 1
    
one = AlwaysOne()

# Test __mul__
print(one * 5) # returns 1
print(one * np.array([5,5])) # returns 1
print(one * pd.Series([5,5])) # returns 1

# Test __rmul__
print(5 * one) # returns 1
print(np.array([5,5]) * one) # returns 1
print(pd.Series([5,5]) * one) # returns pandas Series filled with ones.

Issue Description

When multiplying a pandas.Series or a pandas.DataFrame with another object, a comparison of their __array_priority__'s should determine which operation to choose: If the object on the left has a higher __array_priority__ value, the __mul__ of this class is executed, if it stands on the right its __rmul__ operation is executed.

Pandas objects (Series & DataFrame) respect this rule however, they determine the output class when they stand left in the multiplication with an object of a higher __array_priority__. Note, this is not not the case for numpy arrays.

Expected Behavior

If the __array_priority__ of the other class, which stands on the right side in the multiplication, is higher, the other class should determine the multiplication operation and thus the class of the result.

Installed Versions

NSTALLED VERSIONS

commit : bb1f651
python : 3.9.9.final.0
python-bits : 64
OS : Linux
OS-release : 5.13.0-27-generic
Version : #29~20.04.1-Ubuntu SMP Fri Jan 14 00:32:30 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.0
numpy : 1.21.5
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 60.5.0
Cython : None
pytest : 6.2.5
hypothesis : None
sphinx : 4.3.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 7.29.0
pandas_datareader: None
bs4 : None
bottleneck : 1.3.2
fastparquet : None
fsspec : 2022.01.0
gcsfs : None
matplotlib : 3.5.1
numba : 0.55.0
numexpr : 2.8.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.7.3
sqlalchemy : None
tables : 3.7.0
tabulate : 0.8.9
xarray : 0.20.2
xlrd : None
xlwt : None
zstandard : None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions