Repository navigation
[Data] Don't convert Arrow null columns to null[pyarrow] in to_pandas - #65187
Conversation
Arrow's null type carries no type information, and pandas cannot box a non-null value into a null[pyarrow] column, so fillna and masked assignment raise ArrowInvalid (and can abort the worker from Arrow C++). This shows up in pandas UDFs whenever a block happens to be all-null for a column, e.g. `map_batches(lambda df: df.fillna(...))`. Fall back to pandas' default conversion for null-typed columns. PandasBlockAccessor.to_arrow() coerces all-null columns back to pa.null(), so the round-trip is unchanged. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: Goutam <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request modifies the Arrow-to-Pandas conversion logic in Ray Data to prevent Arrow's null type from being backed by pd.ArrowDtype. Instead, it falls back to Pandas' default conversion, which allows operations like fillna on all-null columns without raising ArrowInvalid errors. New unit tests have been added to verify this behavior. I have no feedback to provide as there are no review comments.
|
Nit:
Can we also add tests for the masked assignment case? |
ayushk7102
left a comment
There was a problem hiding this comment.
LGTM, can take another pass if we choose to add more tests to catch these pandas/pyarrow errors
Since ray-project#63017 (Ray 2.56) `BlockAccessor.to_pandas` returns Arrow-backed columns, so a missing value is `pd.NA` rather than `np.nan`. `pd.NA` propagates through comparisons as "unknown" instead of `False`, and it cannot be stored in a numeric NumPy array. Four preprocessors broke: - `PowerTransformer` raised `IndexError`, because `series >= 0` yields a three-valued `bool[pyarrow]` mask that cannot index a NumPy array. - `Categorizer` raised `TypeError: boolean value of NA is ambiguous`, because `is_null` did not recognize `pd.NA` and so it survived the filter into the category sort. - `Concatenator` silently emitted a column of pickled Python objects instead of a tensor, and raised `TypeError: ... not 'NAType'` when given an integer `dtype` or `flatten=True`. - `FeatureHasher` silently emitted pickled objects as well: `0 + pd.NA` is `pd.NA`, so a single null poisons every hash bucket. `is_null` now recognizes `pd.NA` and `pd.NaT`. The other three convert the columns they operate on through a new `to_numpy_backed` helper, which reproduces the conversion pandas performs when Arrow-backing is disabled (`int64` with nulls widens to `float64` with `np.nan`, strings become `object` with `None`), and is a no-op on already-NumPy-backed input. All of the tests are new. The existing preprocessor tests build their input with `from_pandas`, which never goes through `to_pandas`, so none of them could observe any of this. The new file also covers the null-typed-column `SimpleImputer` fix from ray-project#65187, which landed without a regression test. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: Aarrya <[email protected]>
Since ray-project#63017 (Ray 2.56) `BlockAccessor.to_pandas` returns Arrow-backed columns, so a missing value is `pd.NA` rather than `np.nan`. `pd.NA` propagates through comparisons as "unknown" instead of `False`, and it cannot be stored in a numeric NumPy array. Four preprocessors broke: - `PowerTransformer` raised `IndexError`, because `series >= 0` yields a three-valued `bool[pyarrow]` mask that cannot index a NumPy array. - `Categorizer` raised `TypeError: boolean value of NA is ambiguous`, because `is_null` did not recognize `pd.NA` and so it survived the filter into the category sort. - `Concatenator` silently emitted a column of pickled Python objects instead of a tensor, and raised `TypeError: ... not 'NAType'` when given an integer `dtype` or `flatten=True`. - `FeatureHasher` silently emitted pickled objects as well: `0 + pd.NA` is `pd.NA`, so a single null poisons every hash bucket. `is_null` now recognizes `pd.NA` and `pd.NaT`. The other three convert the columns they operate on through a new `to_numpy_backed` helper, which reproduces the conversion pandas performs when Arrow-backing is disabled (`int64` with nulls widens to `float64` with `np.nan`, strings become `object` with `None`), and is a no-op on already-NumPy-backed input. All of the tests are new. The existing preprocessor tests build their input with `from_pandas`, which never goes through `to_pandas`, so none of them could observe any of this. The new file also covers the null-typed-column `SimpleImputer` fix from ray-project#65187, which landed without a regression test. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: Aarrya <[email protected]>
Since ray-project#63017 (Ray 2.56) `BlockAccessor.to_pandas` returns Arrow-backed columns, so a missing value is `pd.NA` rather than `np.nan`. `pd.NA` propagates through comparisons as "unknown" instead of `False`, and it cannot be stored in a numeric NumPy array. Four preprocessors broke: - `PowerTransformer` raised `IndexError`, because `series >= 0` yields a three-valued `bool[pyarrow]` mask that cannot index a NumPy array. - `Categorizer` raised `TypeError: boolean value of NA is ambiguous`, because `is_null` did not recognize `pd.NA` and so it survived the filter into the category sort. - `Concatenator` silently emitted a column of pickled Python objects instead of a tensor, and raised `TypeError: ... not 'NAType'` when given an integer `dtype` or `flatten=True`. - `FeatureHasher` silently emitted pickled objects as well: `0 + pd.NA` is `pd.NA`, so a single null poisons every hash bucket. `is_null` now recognizes `pd.NA` and `pd.NaT`. The other three convert the columns they operate on through a new `to_numpy_backed` helper, which reproduces the conversion pandas performs when Arrow-backing is disabled (`int64` with nulls widens to `float64` with `np.nan`, strings become `object` with `None`), and is a no-op on already-NumPy-backed input. All of the tests are new. The existing preprocessor tests build their input with `from_pandas`, which never goes through `to_pandas`, so none of them could observe any of this. The new file also covers the null-typed-column `SimpleImputer` fix from ray-project#65187, which landed without a regression test. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: Aarrya <[email protected]>
…ray-project#65187) ## Why are these changes needed? Follow-up to ray-project#64765 / ray-project#63017. `ArrowBlockAccessor.to_pandas()` maps Arrow types to `pd.ArrowDtype` via a `_types_mapper`, with carve-outs for extension and dictionary types. Arrow's `null` type needs the same carve-out. `null[pyarrow]` is unusable from pandas: the type carries no type information, so pandas cannot box a non-null value into such a column. `fillna` and masked assignment raise `ArrowInvalid`, and in some pyarrow/pandas combinations the failure comes out of Arrow C++ and aborts the worker process rather than raising. This is easy to hit in practice because the type is assigned per block, not per dataset. A column that has values overall can still be entirely null within one block — which is common with small blocks — so a pandas UDF like `map_batches(lambda df: df.fillna(...), batch_format="pandas")` fails on whichever block happens to hold only nulls: ```python ds = ray.data.from_items( [{"a": 1.0, "b": 2.0}, {"a": 3.0, "b": None}, {"a": None, "b": 4.0}], override_num_blocks=3, ) ds.map_batches(lambda df: df.fillna({"a": 0.0, "b": 0.0}), batch_format="pandas").take_all() # ArrowInvalid on the block where "a" (or "b") is all-null ``` The fix returns `None` from `_types_mapper` for null-typed columns, falling back to pandas' default conversion. The Arrow round-trip is unchanged: `PandasBlockAccessor.to_arrow()` already coerces all-null columns back to `pa.null()`. The one behavioral consequence, noted in a comment: a column that is all-null in *every* block stays null-typed instead of being promoted — same as before this change. ## Related issue number Follow-up to ray-project#64765. Two tests added: - `test_arrow_block_to_pandas_null_type_is_not_arrow_backed` — a `pa.null()` column is not `pd.ArrowDtype`, still round-trips back to `pa.null()` untouched, and `fillna` now works and adopts the fill value's type. - `test_pandas_udf_can_fill_per_block_null_columns` — end-to-end regression test for the `map_batches` + `fillna` failure above. Signed-off-by: Goutam <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
…ray-project#65187) ## Why are these changes needed? Follow-up to ray-project#64765 / ray-project#63017. `ArrowBlockAccessor.to_pandas()` maps Arrow types to `pd.ArrowDtype` via a `_types_mapper`, with carve-outs for extension and dictionary types. Arrow's `null` type needs the same carve-out. `null[pyarrow]` is unusable from pandas: the type carries no type information, so pandas cannot box a non-null value into such a column. `fillna` and masked assignment raise `ArrowInvalid`, and in some pyarrow/pandas combinations the failure comes out of Arrow C++ and aborts the worker process rather than raising. This is easy to hit in practice because the type is assigned per block, not per dataset. A column that has values overall can still be entirely null within one block — which is common with small blocks — so a pandas UDF like `map_batches(lambda df: df.fillna(...), batch_format="pandas")` fails on whichever block happens to hold only nulls: ```python ds = ray.data.from_items( [{"a": 1.0, "b": 2.0}, {"a": 3.0, "b": None}, {"a": None, "b": 4.0}], override_num_blocks=3, ) ds.map_batches(lambda df: df.fillna({"a": 0.0, "b": 0.0}), batch_format="pandas").take_all() # ArrowInvalid on the block where "a" (or "b") is all-null ``` The fix returns `None` from `_types_mapper` for null-typed columns, falling back to pandas' default conversion. The Arrow round-trip is unchanged: `PandasBlockAccessor.to_arrow()` already coerces all-null columns back to `pa.null()`. The one behavioral consequence, noted in a comment: a column that is all-null in *every* block stays null-typed instead of being promoted — same as before this change. ## Related issue number Follow-up to ray-project#64765. Two tests added: - `test_arrow_block_to_pandas_null_type_is_not_arrow_backed` — a `pa.null()` column is not `pd.ArrowDtype`, still round-trips back to `pa.null()` untouched, and `fillna` now works and adopts the fill value's type. - `test_pandas_udf_can_fill_per_block_null_columns` — end-to-end regression test for the `map_batches` + `fillna` failure above. Signed-off-by: Goutam <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: 400Ping <[email protected]>
…ray-project#65187) ## Why are these changes needed? Follow-up to ray-project#64765 / ray-project#63017. `ArrowBlockAccessor.to_pandas()` maps Arrow types to `pd.ArrowDtype` via a `_types_mapper`, with carve-outs for extension and dictionary types. Arrow's `null` type needs the same carve-out. `null[pyarrow]` is unusable from pandas: the type carries no type information, so pandas cannot box a non-null value into such a column. `fillna` and masked assignment raise `ArrowInvalid`, and in some pyarrow/pandas combinations the failure comes out of Arrow C++ and aborts the worker process rather than raising. This is easy to hit in practice because the type is assigned per block, not per dataset. A column that has values overall can still be entirely null within one block — which is common with small blocks — so a pandas UDF like `map_batches(lambda df: df.fillna(...), batch_format="pandas")` fails on whichever block happens to hold only nulls: ```python ds = ray.data.from_items( [{"a": 1.0, "b": 2.0}, {"a": 3.0, "b": None}, {"a": None, "b": 4.0}], override_num_blocks=3, ) ds.map_batches(lambda df: df.fillna({"a": 0.0, "b": 0.0}), batch_format="pandas").take_all() # ArrowInvalid on the block where "a" (or "b") is all-null ``` The fix returns `None` from `_types_mapper` for null-typed columns, falling back to pandas' default conversion. The Arrow round-trip is unchanged: `PandasBlockAccessor.to_arrow()` already coerces all-null columns back to `pa.null()`. The one behavioral consequence, noted in a comment: a column that is all-null in *every* block stays null-typed instead of being promoted — same as before this change. ## Related issue number Follow-up to ray-project#64765. Two tests added: - `test_arrow_block_to_pandas_null_type_is_not_arrow_backed` — a `pa.null()` column is not `pd.ArrowDtype`, still round-trips back to `pa.null()` untouched, and `fillna` now works and adopts the fill value's type. - `test_pandas_udf_can_fill_per_block_null_columns` — end-to-end regression test for the `map_batches` + `fillna` failure above. Signed-off-by: Goutam <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Why are these changes needed?
Follow-up to #64765 / #63017.
ArrowBlockAccessor.to_pandas()maps Arrow types topd.ArrowDtypevia a_types_mapper, with carve-outs for extension and dictionary types. Arrow'snulltype needs the same carve-out.null[pyarrow]is unusable from pandas: the type carries no type information, so pandas cannot box a non-null value into such a column.fillnaand masked assignment raiseArrowInvalid, and in some pyarrow/pandas combinations the failure comes out of Arrow C++ and aborts the worker process rather than raising.This is easy to hit in practice because the type is assigned per block, not per dataset. A column that has values overall can still be entirely null within one block — which is common with small blocks — so a pandas UDF like
map_batches(lambda df: df.fillna(...), batch_format="pandas")fails on whichever block happens to hold only nulls:The fix returns
Nonefrom_types_mapperfor null-typed columns, falling back to pandas' default conversion. The Arrow round-trip is unchanged:PandasBlockAccessor.to_arrow()already coerces all-null columns back topa.null(). The one behavioral consequence, noted in a comment: a column that is all-null in every block stays null-typed instead of being promoted — same as before this change.Related issue number
Follow-up to #64765.
Two tests added:
test_arrow_block_to_pandas_null_type_is_not_arrow_backed— apa.null()column is notpd.ArrowDtype, still round-trips back topa.null()untouched, andfillnanow works and adopts the fill value's type.test_pandas_udf_can_fill_per_block_null_columns— end-to-end regression test for themap_batches+fillnafailure above.