Skip to content

BUG: Some ExtensionArrays can return 0-d Elements #61433

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

Open
2 of 3 tasks
ilan-gold opened this issue May 12, 2025 · 4 comments
Open
2 of 3 tasks

BUG: Some ExtensionArrays can return 0-d Elements #61433

ilan-gold opened this issue May 12, 2025 · 4 comments
Labels
Bug ExtensionArray Extending pandas with custom dtypes or arrays. Indexing Related to indexing on series/frames, not to indexes themselves Needs Discussion Requires discussion from core team before further action

Comments

@ilan-gold
Copy link

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


for arr in [pd.arrays.PeriodArray(pd.PeriodIndex(['2023-01-01','2023-01-02'], freq='D')), pd.Categorical(["a", "b"])]:
    subset = arr[(0, Ellipsis)]
    assert isinstance(subset, type(arr))
    assert subset.shape == ()

Issue Description

Given what is stated on https://pandas.pydata.org/docs/reference/api/pandas.api.extensions.ExtensionArray.html, I would expect this not to be possible at all.

Expected Behavior

The reason I care is that arrow arrays do not have a 0d version, which makes it tough to develop over all ExtensionArray classes:

pd.array([1, 2], dtype="int64[pyarrow]")[(0, Ellipsis)]

gives simply the number 1.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.11.11
python-bits : 64
OS : Darwin
OS-release : 24.1.0
Version : Darwin Kernel Version 24.1.0: Thu Oct 10 21:03:15 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.3
numpy : 2.2.5
pytz : 2024.1
dateutil : 2.9.0.post0
pip : 25.0.1
Cython : None
sphinx : None
IPython : 8.32.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 : 2025.3.2
html5lib : None
hypothesis : 6.131.6
gcsfs : None
jinja2 : 3.1.5
lxml.etree : 5.3.2
matplotlib : 3.10.1
numba : 0.61.2
numexpr : 2.10.2
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 19.0.1
pyreadstat : None
pytest : 8.3.5
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.15.2
sqlalchemy : None
tables : None
tabulate : None
xarray : 2025.4.1.dev3+gd998eac1.d20250509
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.1
qtpy : None
pyqt5 : None

@ilan-gold ilan-gold added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 12, 2025
@rhshadrach
Copy link
Member

rhshadrach commented May 17, 2025

Thanks for the report!

The reason I care is that arrow arrays do not have a 0d version, which makes it tough to develop over all ExtensionArray classes

Can you give a little more detail here as to why it makes it tough? In particular, does

pd.array([1, 2], dtype="int64[pyarrow]")[0]

also cause difficulties?

@rhshadrach
Copy link
Member

Ah - I see it now. It's that with just [0], you get the type of the scalar and not the ExtensionArray type.

@rhshadrach
Copy link
Member

Thanks for the report. NumPy appears to have decided the user is asking to get back a 0-dim ndarray rather than a scalar.

print(type(np.array([1, 2, 3])[1, ...]))
# <class 'numpy.ndarray'>
print(type(np.array([1, 2, 3])[1]))
# <class 'numpy.int64'>

pandas would be consistent to agree that the user is asking for a 0-dim ExtensionArray here, and hence to raise as these are not supported.

@jbrockmendel - do you have any thoughts here?

@rhshadrach rhshadrach added Indexing Related to indexing on series/frames, not to indexes themselves Needs Discussion Requires discussion from core team before further action ExtensionArray Extending pandas with custom dtypes or arrays. and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 18, 2025
@jbrockmendel
Copy link
Member

raising would make sense, but im a bit concerned about the performance hit of adding that check (that __getitem__ method was optimized pretty hard IIRC). are there user-facing methods of reaching this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug ExtensionArray Extending pandas with custom dtypes or arrays. Indexing Related to indexing on series/frames, not to indexes themselves Needs Discussion Requires discussion from core team before further action
Projects
None yet
Development

No branches or pull requests

3 participants