Skip to content

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

@sfc-gh-rdurrani

Description

@sfc-gh-rdurrani

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
df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=['col1', 'col2', 'col3'])
series = pd.Series([True, False, True])
df.where(series, axis=1)


### Issue Description

The above errors like so:
```python
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[7], line 4
      2 df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
      3 series = pd.Series([True, False, True])
----> 4 df.where(series, axis=1)

File ~/.miniconda3/envs/snowpark/lib/python3.10/site-packages/pandas/core/generic.py:10615, in NDFrame.where(self, cond, other, inplace, axis, level)
  10609             warnings.warn(
  10610                 _chained_assignment_method_msg,
  10611                 ChainedAssignmentError,
  10612                 stacklevel=2,
  10613             )
  10614 other = common.apply_if_callable(other, self)
> 10615 return self._where(cond, other, inplace, axis, level)

File ~/.miniconda3/envs/snowpark/lib/python3.10/site-packages/pandas/core/generic.py:10407, in NDFrame._where(self, cond, other, inplace, axis, level)
  10404     return self._update_inplace(result)
  10406 else:
> 10407     new_data = self._mgr.where(
  10408         other=other,
  10409         cond=cond,
  10410         align=align,
  10411     )
  10412     result = self._constructor_from_mgr(new_data, axes=new_data.axes)
  10413     return result.__finalize__(self)

File ~/.miniconda3/envs/snowpark/lib/python3.10/site-packages/pandas/core/internals/base.py:190, in DataManager.where(self, other, cond, align)
    187     align_keys = ["cond"]
    188     other = extract_array(other, extract_numpy=True)
--> 190 return self.apply_with_block(
    191     "where",
    192     align_keys=align_keys,
    193     other=other,
    194     cond=cond,
    195     using_cow=using_copy_on_write(),
    196 )

File ~/.miniconda3/envs/snowpark/lib/python3.10/site-packages/pandas/core/internals/managers.py:349, in BaseBlockManager.apply(self, f, align_keys, **kwargs)
    346                 kwargs[k] = obj.iloc[:, b.mgr_locs.indexer]._values
    347         else:
    348             # otherwise we have an ndarray
--> 349             kwargs[k] = obj[b.mgr_locs.indexer]
    351 if callable(f):
    352     applied = b.apply(f, **kwargs)

TypeError: 'float' object is not subscriptable

when it should work by transposing the Series and doing a positional join on labels.

Expected Behavior

   col1  col2  col3
0     1  None     3
1     4  None     6
2     7  None     9

Installed Versions

INSTALLED VERSIONS

commit : a671b5a
python : 3.10.13.final.0
python-bits : 64
OS : Darwin
OS-release : 23.4.0
Version : Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.1.4
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 68.2.2
pip : 23.3.1
Cython : None
pytest : 7.4.4
hypothesis : None
sphinx : 5.0.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.22.2
pandas_datareader : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 15.0.0
pyreadstat : 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

    BugNeeds 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