Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

BUG: np.where called with ps.Series returns np.array instead of pd.Series #58329

Copy link
Copy link
Open
@domsmrz

Description

@domsmrz
Issue body actions

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
s = pd.Series([1,2,3], name='a')
res = np.where(s > 2, s, -s)
print(res)
# res is np.array([-1, -2, 3])

Issue Description

res in the example is np.array. Such behavior is inconsistent with other numpy functions such as np.floor or np.arctan2.

Expected Behavior

res should be Series instead (particularly pd.Series([-1, -2, 3], name='a') in this case.

As for the cases where x and y (i.e., the two last params of np.where) are of mixed types or names, we should probably keep consistency with np.arctan2, meaning:

  • if only one of x and y is pd.Series and the other is np.array we should return pd.Series with the same name as input Series
  • if both x and y are pd.Series of the same name we should return a pd.Series with that name
  • if both x and y are pd.Series but with different names we should return unnamed pd.Series

Installed Versions

INSTALLED VERSIONS

commit : bdc79c1
python : 3.12.3.final.0
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.22631
machine : AMD64
processor : AMD64 Family 25 Model 33 Stepping 2, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_Germany.1252

pandas : 2.2.1
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : None
pip : 23.3.2
Cython : None
pytest : 8.1.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.23.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : 2.10.0
odfpy : None
openpyxl : None
pandas_gbq : 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

Labels

BugUpstream issueIssue related to pandas dependencyIssue related to pandas dependency

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.