Skip to content
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

BUG: iterrows() on an awkward array with equal-length rows results in a ValueError #58927

Closed
2 of 3 tasks
Girmii opened this issue Jun 5, 2024 · 3 comments
Closed
2 of 3 tasks
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@Girmii
Copy link

Girmii commented Jun 5, 2024

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 awkward as ak
import awkward_pandas as akpd
import pandas as pd

# numbers = [[1, 2, 3], [4, 5], [6]]
numbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
letters = ["A", "B", "C"]

numbers_ak = ak.from_iter(numbers)
numbers_akpd = akpd.from_awkward(numbers_ak)

df = pd.DataFrame({"letters": letters, "numbers": numbers_akpd})

for idx, row in df.iterrows():
    print(f"{idx} - {row['letters']}, {row['numbers']}")
File .venv/lib/python3.11/site-packages/pandas/core/internals/blocks.py:2253, in EABackedBlock.get_values(self, dtype)
   [2251](.venv/lib/python3.11/site-packages/pandas/core/internals/blocks.py:2251)     values = values.astype(object)
   [2252](.venv/lib/python3.11/site-packages/pandas/core/internals/blocks.py:2252) # TODO(EA2D): reshape not needed with 2D EAs
-> [2253](.venv/lib/python3.11/site-packages/pandas/core/internals/blocks.py:2253) return np.asarray(values).reshape(self.shape)

ValueError: cannot reshape array of size 9 into shape

Issue Description

I'm not really knowledgeable on the internals of Pandas and array extensions, so I'm not sure if this is the right place to report the bug, but since it occurred in the Pandas core code, I came here first. Please let me know if this is actually an issue with the awkward or awkward_pandas module.


When calling iterrows() on a DataFrame which contains an awkward array as a column, a ValueError occurs (see stacktrace example). This error only occurs when all rows of the awkward array are of equal length. In this case the calls to values.astype(object) and/or np.asarray(values) in the get_values function in the pandas/core/internals/blocks.py module result in a 2D array, instead of a 1D array with nested lists.
When the awkward array is actually jagged, the call results in the correct format of the array (see commented line in code example) and iterrows() works as intended.

Expected Behavior

I would expect iterrows() to iterate over the DataFrame rows without throwing an error, but instead returning a Series with the value of the awkward array at the index of the row set correctly.

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.11.0.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.146.1-microsoft-standard-WSL2
Version : #1 SMP Thu Jan 11 04:09:03 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.2
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 65.5.0
pip : 22.3
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.25.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.6.0
gcsfs : None
matplotlib : None
numba : None
numexpr : None
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

@Girmii Girmii added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 5, 2024
@Aloqeely
Copy link
Member

Aloqeely commented Jun 8, 2024

Thanks for the report! While this might be a problem with pandas, we don't know enough about awkward pandas to be able to debug this issue, it might be better to open this issue in awkward_pandas and see if the issue is actually on their end or our end.

Going to close, but if the people at awkward-pandas suggest the issue is actually on our end, feel free to re-open this issue and we can continue our discussion here.

@Aloqeely Aloqeely closed this as completed Jun 8, 2024
@Girmii
Copy link
Author

Girmii commented Jun 8, 2024

Alright, thanks for the feedback. I will take it up with awkward_pandas.

@Girmii
Copy link
Author

Girmii commented Jun 12, 2024

A quick update for anyone who might come across this issue:
The developer of awkward_pandas replied to the issue I posted on their page as well. It seems this problem will be solved soon in a future release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants