Skip to content

BUG: df.where(series, axis=1) always results in TypeError #58190

@wjandrea

Description

@wjandrea

Pandas version checks

Reproducible Example

Based on this Stack Overflow question:

A = pd.DataFrame(
    [[0.0, 0.5, 0.0],
     [0.1, 0.0, 0.2],
     [0.2, 0.0, 0.0]])
B = pd.Series([1.0, 1.0, np.nan])

Then:

A.where(B.notna(), axis=1)

Issue Description

Result: TypeError: 'float' object is not subscriptable

It seems to be trying to index other, which is NaN.

Traceback
Traceback (most recent call last):

  Cell In[11], line 2
    A.where(B.notna(), axis=1)

  File ~/Programs/micromamba/envs/pandas_pre/lib/python3.12/site-packages/pandas/core/generic.py:9897 in where
    return self._where(cond, other, inplace=inplace, axis=axis, level=level)

  File ~/Programs/micromamba/envs/pandas_pre/lib/python3.12/site-packages/pandas/core/generic.py:9686 in _where
    new_data = self._mgr.where(

  File ~/Programs/micromamba/envs/pandas_pre/lib/python3.12/site-packages/pandas/core/internals/managers.py:468 in where
    return self.apply(

  File ~/Programs/micromamba/envs/pandas_pre/lib/python3.12/site-packages/pandas/core/internals/managers.py:432 in apply
    kwargs[k] = obj[b.mgr_locs.indexer]

TypeError: 'float' object is not subscriptable

Expected Behavior

B.notna() is broadcast to A along the columns.

     0    1    2
0  0.0  0.5  NaN
1  0.1  0.0  NaN
2  0.2  0.0  NaN

Cf. A.mul(B, axis=1):

     0    1   2
0  0.0  0.5 NaN
1  0.1  0.0 NaN
2  0.2  0.0 NaN

Installed Versions

INSTALLED VERSIONS

commit : b8a4691
python : 3.12.0.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-174-generic
Version : #193-Ubuntu SMP Thu Mar 7 14:29:28 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : fr_CA.UTF-8
LOCALE : fr_CA.UTF-8

pandas : 3.0.0.dev0+680.gb8a4691647
numpy : 2.1.0.dev0+git20240403.e59c074
pytz : 2024.1
dateutil : 2.8.2
setuptools : 68.2.2
pip : 24.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.22.2
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugConditionalsE.g. where, mask, case_whenNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions