-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: inconsistent return types from __getitem__ vs iteration #60560
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
Comments
#59838 is loosely related. |
Thanks for the report, somewhat related to #25969. I also thought we had an issue as whether to return Python or NumPy scalars across the API, but can't seem to find it. I'm not sure whether there has been any prior discussions on this, cc @pandas-dev/pandas-core. I'll also add there is inconsistency in reductions. On NumPy dtypes (e.g. NumPy scalars:
Python scalars:
On PyArrow dtypes (e.g |
Thanks @rhshadrach. It's been a couple months so I just wanted to check back on this to see if any further information is needed? |
This is a pretty old feature. Here's the release note about it in v0.21.0, from over 7 years ago: With >>> import numpy as np
>>> import pandas as pd
>>>
>>> print(np.__version__) # 2.0.2
1.26.4
>>> print(pd.__version__) # 2.2.3
2.2.2
>>>
>>> data = pd.Series([333, 555])
>>>
>>> # accessing scalar via __getitem__ returns <class 'numpy.int64'>
>>> print(type(data[0]))
<class 'numpy.int64'>
>>>
>>> # accessing scalar via iteration returns <class 'int'>
>>> print(type(next(iter(data))))
<class 'int'> So with older versions of |
I would prefer if all pandas operations that returned scalars returned Python, rather than NumPy objects. This is especially true if we are wanting to make migration to arrow-backed dtypes easier. |
cc @pandas-dev/pandas-core |
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
numpy 2.0 recently changed its representation of scalars to include type information. However, pandas produces inconsistent return types when one is accessing scalars with
__getitem__
vs iterating over items, as demonstrated in the example code snippet.This inconsistency is showing up in downstream projects like NetworkX: networkx/networkx#7763 (comment)
Expected Behavior
pandas should produce consistent return types when one is accessing scalars with
__getitem__
vs iterating over itemsInstalled Versions
INSTALLED VERSIONS
commit : 0691c5c
python : 3.12.8
python-bits : 64
OS : Linux
OS-release : 6.8.0-50-generic
Version : #51-Ubuntu SMP PREEMPT_DYNAMIC Sat Nov 9 17:58:29 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.2.3
numpy : 2.0.2
pytz : 2024.1
dateutil : 2.9.0.post0
pip : 24.3.1
Cython : None
sphinx : 8.1.3
IPython : 8.30.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
blosc : None
bottleneck : 1.4.2
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.4
lxml.etree : 5.3.0
matplotlib : 3.9.3
numba : 0.60.0
numexpr : 2.10.2
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : 8.3.4
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : 0.23.0
tzdata : 2024.2
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: