Skip to content

[Data] Don't convert Arrow null columns to null[pyarrow] in to_pandas - #65187

Merged
goutamvenkat-anyscale merged 2 commits into
ray-project:masterfrom
goutamvenkat-anyscale:data-null-type-not-arrow-backed
Aug 3, 2026
Merged

goutamvenkat-anyscale merged 2 commits into
ray-project:masterfrom
goutamvenkat-anyscale:data-null-type-not-arrow-backed

Conversation

@goutamvenkat-anyscale

@goutamvenkat-anyscale goutamvenkat-anyscale commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Why are these changes needed?

Follow-up to #64765 / #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:

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 #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.

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]>
@goutamvenkat-anyscale
goutamvenkat-anyscale requested a review from a team as a code owner August 3, 2026 22:33
@goutamvenkat-anyscale goutamvenkat-anyscale added data Ray Data-related issues go add ONLY when ready to merge, run all tests labels Aug 3, 2026

@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 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.

@ayushk7102

Copy link
Copy Markdown
Contributor

Nit:

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.

Can we also add tests for the masked assignment case?

@ayushk7102 ayushk7102 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.

LGTM, can take another pass if we choose to add more tests to catch these pandas/pyarrow errors

@goutamvenkat-anyscale
goutamvenkat-anyscale merged commit 850f0be into ray-project:master Aug 3, 2026
6 checks passed
@goutamvenkat-anyscale
goutamvenkat-anyscale deleted the data-null-type-not-arrow-backed branch August 3, 2026 23:56
AarryaSaraf added a commit to AarryaSaraf/ray that referenced this pull request Aug 6, 2026
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]>
AarryaSaraf added a commit to AarryaSaraf/ray that referenced this pull request Aug 6, 2026
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]>
AarryaSaraf added a commit to AarryaSaraf/ray that referenced this pull request Aug 7, 2026
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]>
Artimislyy pushed a commit to Artimislyy/ray that referenced this pull request Aug 11, 2026
…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]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…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]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
…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]>
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.

2 participants