Skip to content

BUG: Binary operations (__and__, __or__, etc.) do not take parameter axis and fail #39519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 of 3 tasks
YarShev opened this issue Feb 1, 2021 · 7 comments
Closed
2 of 3 tasks
Labels
Bug Needs Discussion Requires discussion from core team before further action Regression Functionality that used to work in a prior pandas version

Comments

@YarShev
Copy link
Contributor

YarShev commented Feb 1, 2021

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# Your code here
import pandas as pd
import numpy as np
df = pd.DataFrame([1, 1])
a = np.array([0, 1])
df.__and__(a, axis=0)
TypeError: new_method() got an unexpected keyword argument 'axis'

Problem description

This worked in pandas 1.1.5

Expected Output

df.__and__(a, axis=0)
   0
0  0
1  1

Output of pd.show_versions()

pandas : 1.2.1
numpy : 1.19.5
pytz : 2020.5
dateutil : 2.8.1
pip : 20.3.3
setuptools : 51.1.2.post20210112
Cython : None
pytest : 6.2.1
hypothesis : None
sphinx : None
blosc : None
feather : 0.4.1
xlsxwriter : None
lxml.etree : 4.6.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.12.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : 0.8.3
fastparquet : None
gcsfs : None
matplotlib : 3.2.2
numexpr : 2.7.2
odfpy : None
openpyxl : 3.0.6
pandas_gbq : None
pyarrow : 2.0.0
pyxlsb : None
s3fs : 0.5.2
scipy : 1.6.0
sqlalchemy : 1.3.22
tables : 3.6.1
tabulate : None
xarray : 0.16.2
xlrd : 2.0.1
xlwt : None
numba : None

@YarShev YarShev added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 1, 2021
@phofl
Copy link
Member

phofl commented Feb 2, 2021

first bad commit is 7f1c3b1 cc @jbrockmendel

@phofl phofl added the Regression Functionality that used to work in a prior pandas version label Feb 2, 2021
@simonjayhawkins
Copy link
Member

first bad commit is 7f1c3b1 cc @jbrockmendel

#36843

@simonjayhawkins
Copy link
Member

It appears this was an intentional change #36796 (comment)

@simonjayhawkins simonjayhawkins added Needs Discussion Requires discussion from core team before further action and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 12, 2021
@simonjayhawkins simonjayhawkins added this to the 1.2.5 milestone Apr 12, 2021
@simonjayhawkins
Copy link
Member

@jorisvandenbossche potential discussion item for dev meeting tomorrow.

@jorisvandenbossche
Copy link
Member

The dunder methods are not meant to call directly like that, so we shouldn't guarantee any stability here IMO

@jorisvandenbossche
Copy link
Member

Alternative solution for when the right array operand is not exactly shape-aligned to avoid the axis:

Reshape (and potentially broadcast) the array so it automatically aligns on the index dimension (axis=0):

In [5]: df & a.reshape(-1, 1)
Out[5]: 
   0
0  0
1  1

You need to do the same when using ufuncs like np.logical_and (those also don't have an axis keyword, and need correctly shaped arrays to ensure shapes are matching / broadcasting is possible).

@jorisvandenbossche jorisvandenbossche modified the milestones: 1.2.5, No action Apr 15, 2021
@jorisvandenbossche
Copy link
Member

So give that it's a dunder method that shouldn't be called directly, and given that there are alternatives, I think it is fine to close this issue.

(note, we could still discuss separately if we want to add a "flex" method for those logical operations, like we have a df.add(..) method that is equivalent to __add__ but with more flexibility and keywords)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Discussion Requires discussion from core team before further action Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

No branches or pull requests

4 participants