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
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
importpandasaspdimportnumpyasnpclassAlwaysOne(float):
__array_priority__=10000__array_ufunc__=Nonedef__rmul__(self, other):
return1def__mul__(self, other):
return1one=AlwaysOne()
# Test __mul__print(one*5) # returns 1print(one*np.array([5,5])) # returns 1print(one*pd.Series([5,5])) # returns 1# Test __rmul__print(5*one) # returns 1print(np.array([5,5]) *one) # returns 1print(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 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.
AFAIK pandas doesn't check __array_priority__ anywhere. That's a numpy convention.
Yeah though pandas is backed by numpy arrays, pandas objects don't use __array_priority__ semantics during arithmetic operations to determine the returned object.
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
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
The text was updated successfully, but these errors were encountered: