anishmehta24 opened a new issue, #41662:
URL: https://github.com/apache/beam/issues/41662
### What happened?
`idxmin()` and `idxmax()` on a deferred Series or DataFrame fail with
`ValueError: attempt to get argmax of an empty sequence` when one partition has
no rows, which happens easily after a filter:
```python
import apache_beam as beam
import pandas as pd
from apache_beam.dataframe.transforms import DataframeTransform
parts = [
pd.DataFrame({'x': [10.0, 30.0]}),
pd.DataFrame({'x': [1.0, 2.0]}, index=[2, 3]),
]
with beam.Pipeline() as p:
_ = (
p | beam.Create(parts)
| DataframeTransform(
lambda df: df[df.x > 5].x.idxmax(),
proxy=parts[0].iloc[:0],
yield_elements='pandas')
| beam.Map(print))
# pandas gives 1, Beam raises ValueError: attempt to get argmax of an empty
sequence
```
`df[df.x > 5].idxmax()` fails the same way. It also fails when a datetime
column has only `NaT` in one partition, for example parts `pd.DataFrame({'t':
pd.to_datetime(['2024-01-02', '2024-01-05'])})` and `pd.DataFrame({'t':
pd.to_datetime([None, None])}, index=[2, 3])` with `df.t.idxmax()`: pandas
returns 1, Beam raises the same error. Seen on 2.77.0 (Prism) and on master.
`_idxmaxmin_helper` in `frames.py` calls pandas `idxmax`/`idxmin` on every
partition first (`compute_idx`), and pandas raises on an empty partition, and
on an all-NA one for datetime, string and nullable dtypes. Passing such a
partition on unchanged lets the final step give the pandas result.
### Issue Priority
Priority: 2 (default / most bugs should be filed as P2)
### Issue Components
- [x] Component: Python SDK
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]