Repository navigation
[Data] Deprecate Pandas (#2/2) Fix aggregation/discretizer/null failures on converting to Arrow Blocks internally - #61801
Conversation
There was a problem hiding this comment.
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)
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)
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
)
)
Signed-off-by: Ayush Kumar <[email protected]>
Signed-off-by: Ayush Kumar <[email protected]>
9af4b70 to
b2c0483
Compare
…nd list semantics 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]>
| 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])}." |
There was a problem hiding this comment.
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.
…nary arrays 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]>
Signed-off-by: Ayush Kumar <[email protected]>
Signed-off-by: Ayush Kumar <[email protected]>
Signed-off-by: Ayush Kumar <[email protected]>
Signed-off-by: Ayush Kumar <[email protected]>
Signed-off-by: Ayush Kumar <[email protected]>
…nd list semantics 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]>
…nary arrays 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]>
…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]>
…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]>
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.