Skip to content

[Data] Deprecate Pandas (#2/2) Fix aggregation/discretizer/null failures on converting to Arrow Blocks internally - #61801

Merged
ayushk7102 merged 16 commits into
ray-project:arrow_batch_formatfrom
ayushk7102:a2p_agg_fixes
Mar 19, 2026
Merged

ayushk7102 merged 16 commits into
ray-project:arrow_batch_formatfrom
ayushk7102:a2p_agg_fixes

Conversation

@ayushk7102

@ayushk7102 ayushk7102 commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #61733

This PR adds Agg/Null test fixes on top of the minimal change to ensure result batches from UDFs are converted to Arrow instead of Pandas blocks.

@ayushk7102
ayushk7102 changed the base branch from master to arrow_batch_format March 17, 2026 18:01
@ayushk7102 ayushk7102 changed the title a2p agg fixes [Data] Deprecate Pandas (#2/2) Convert Pandas UDF batches as Arrow blocks internally Mar 17, 2026
@ayushk7102 ayushk7102 added the go add ONLY when ready to merge, run all tests label Mar 17, 2026
@ayushk7102
ayushk7102 marked this pull request as ready for review March 17, 2026 18:04
@ayushk7102
ayushk7102 requested a review from a team as a code owner March 17, 2026 18:04

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces several fixes and improvements related to data handling between Pandas and Arrow formats. Key changes include making batches writable by default, safer conversion from Pandas DataFrames with object-dtype columns to Arrow tables, and fixes for null value handling. The changes are generally well-implemented and improve the robustness and consistency of data handling in Ray Data. I've included a couple of suggestions for improving code clarity and idiomatic usage.

I am having trouble creating individual review comments. Click here to see my feedback.

python/ray/data/_internal/pandas_block.py (95-98)

medium

This approach to creating non_object_table by first creating a list of non-object column names and then slicing the DataFrame is a bit indirect. A more idiomatic and potentially clearer way to achieve this in pandas is to use df.drop(). This would also make the intent of the code more explicit.

For example:

non_object_df = df.drop(columns=object_col_names)
non_object_table = pa.Table.from_pandas(non_object_df, preserve_index=False)

python/ray/data/_internal/pandas_block.py (103-111)

medium

The inline comments within this dictionary comprehension are quite long and make the code harder to read. For better readability, consider moving these explanations to a comment block just before the return statement. This would keep the comprehension itself clean and focused on the logic.

            col: (
                convert_to_pyarrow_array(
                    df[col].values, col
                )
                if col in object_col_set
                else non_object_table.column(
                    col
                )
            )

@ray-gardener ray-gardener Bot added the data Ray Data-related issues label Mar 17, 2026
…ist from numpy array, and add better logging to print first offending element in array instead of first element

Signed-off-by: Ayush Kumar <[email protected]>
raise TypeError(
"Expected a well-typed ndarray or an object-typed ndarray of "
"ndarray pointers, but got an object-typed ndarray whose "
f"subndarrays are of type {type(values[0])}."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By hard-coding the element to call out, our logging was misleading, basically saying "Expected a well-typed ndarray of ndarray pointers, but got .... type(values[0])"

So for eg. in an array of two elements {<ndarray_ref>, NaN}, we would correctly error out here. But, we would incorrectly call out and raise that the offending element was at values[0], which is a valid ndarray_ref, instead of the NoneType that is the actual culprit.

What we should do instead is call out the first offending element in the array.

@ayushk7102
ayushk7102 requested a review from a team as a code owner March 17, 2026 23:21
@ayushk7102 ayushk7102 changed the title [Data] Deprecate Pandas (#2/2) Convert Pandas UDF batches as Arrow blocks internally [Data] Deprecate Pandas (#2/2) Fix aggregation/discretization/null tests, bugs converting to Arrow Blocks internally Mar 17, 2026
@ayushk7102 ayushk7102 changed the title [Data] Deprecate Pandas (#2/2) Fix aggregation/discretization/null tests, bugs converting to Arrow Blocks internally [Data] Deprecate Pandas (#2/2) Fix aggregation/discretization/null failures on converting to Arrow Blocks internally Mar 17, 2026
@ayushk7102 ayushk7102 changed the title [Data] Deprecate Pandas (#2/2) Fix aggregation/discretization/null failures on converting to Arrow Blocks internally [Data] Deprecate Pandas (#2/2) Fix aggregation/discretizer/null failures on converting to Arrow Blocks internally Mar 17, 2026
…opy_only=False and reshape object array to preserve inner tensor shape

Signed-off-by: Ayush Kumar <[email protected]>
…ist from numpy array, and add better logging to print first offending element in array instead of first element

Signed-off-by: Ayush Kumar <[email protected]>
…opy_only=False and reshape object array to preserve inner tensor shape

Signed-off-by: Ayush Kumar <[email protected]>
@ayushk7102
ayushk7102 merged commit fbe5415 into ray-project:arrow_batch_format Mar 19, 2026
2 checks passed
@ayushk7102
ayushk7102 deleted the a2p_agg_fixes branch March 24, 2026 21:06
bveeramani pushed a commit that referenced this pull request Apr 6, 2026
…nternally (#61733)

## Description
When a UDF returns a `pd.DataFrame` or dict batch, Ray Data previously
stored it as a Pandas block in the object
store. This PR changes that behavior: UDF output is now immediately
converted to an Arrow block before being stored, making Arrow the single
internal block format


## Related issues
None

## Additional information
- Config key : `DEFAULT_BATCH_TO_BLOCK_ARROW_FORMAT` (default: `True` ).
Get via `ray.data.DataContext.get_current().batch_to_block_arrow_format`
- Implementation overview :
- In `batch_to_block`, if the above key is set (Arrow is the default
block format), convert each batched result Pandas dataframe into an
Arrow block using `for_block(batch).to_arrow()`
- `_safe_from_pandas(df)` replaces the direct pa.Table.from_pandas()
call in PandasBlockAccessor.to_arrow(). The standard PyArrow conversion
fails on object-dtype columns containing multi-dimensional numpy arrays,
PIL images, or mixed types. This function routes object-dtype columns
through `convert_to_pyarrow_array`
- The new `_make_batch_writable()` function explicitly copies any
read-only numpy arrays in the formatted batch. Wire down `ensure_copy`
through `format_batch()`. This is required, as pa.Array.to_numpy()
returns read-only views by default


- Test/Bug fixes : 
- Updated `test_map_transformer` and `test_strict_schema` to correctly
assert for Arrow blocks instead of Pandas.
- Update `arrow_serialization.py` to pass ordered_flag to from_arrays(),
dropping the flag results when deserializing Arrow dictionary arrays
- Fixed `find_partition_index` behaviour for Arrow blocks, null
semantics
- Modify empty list check in Quantile Aggregate to match numpy and list
semantics
- Fix crash in multi aggregation tests by coercing accumulator type to
list from numpy array in `/data/_internal/tensor_extensions/pandas.py`
    
> Note: #61801 was originally stacked on this PR and has since been
merged into it.

---------

Signed-off-by: Ayush Kumar <[email protected]>
Signed-off-by: Ayush Kumar <[email protected]>
Lucas61000 pushed a commit to Lucas61000/ray that referenced this pull request May 15, 2026
…nternally (ray-project#61733)

## Description
When a UDF returns a `pd.DataFrame` or dict batch, Ray Data previously
stored it as a Pandas block in the object
store. This PR changes that behavior: UDF output is now immediately
converted to an Arrow block before being stored, making Arrow the single
internal block format


## Related issues
None

## Additional information
- Config key : `DEFAULT_BATCH_TO_BLOCK_ARROW_FORMAT` (default: `True` ).
Get via `ray.data.DataContext.get_current().batch_to_block_arrow_format`
- Implementation overview :
- In `batch_to_block`, if the above key is set (Arrow is the default
block format), convert each batched result Pandas dataframe into an
Arrow block using `for_block(batch).to_arrow()`
- `_safe_from_pandas(df)` replaces the direct pa.Table.from_pandas()
call in PandasBlockAccessor.to_arrow(). The standard PyArrow conversion
fails on object-dtype columns containing multi-dimensional numpy arrays,
PIL images, or mixed types. This function routes object-dtype columns
through `convert_to_pyarrow_array`
- The new `_make_batch_writable()` function explicitly copies any
read-only numpy arrays in the formatted batch. Wire down `ensure_copy`
through `format_batch()`. This is required, as pa.Array.to_numpy()
returns read-only views by default


- Test/Bug fixes : 
- Updated `test_map_transformer` and `test_strict_schema` to correctly
assert for Arrow blocks instead of Pandas.
- Update `arrow_serialization.py` to pass ordered_flag to from_arrays(),
dropping the flag results when deserializing Arrow dictionary arrays
- Fixed `find_partition_index` behaviour for Arrow blocks, null
semantics
- Modify empty list check in Quantile Aggregate to match numpy and list
semantics
- Fix crash in multi aggregation tests by coercing accumulator type to
list from numpy array in `/data/_internal/tensor_extensions/pandas.py`
    
> Note: ray-project#61801 was originally stacked on this PR and has since been
merged into it.

---------

Signed-off-by: Ayush Kumar <[email protected]>
Signed-off-by: Ayush Kumar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data Ray Data-related issues go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant