Skip to content

Conversation

@shashbha14
Copy link
Contributor

@shashbha14 shashbha14 commented Jan 26, 2026

Rationale for this change

Currently, pyarrow.compute.cast() raises an exception when encountering values that cannot be cast to the target type. This PR adds an errors parameter (similar to pandas.to_numeric(errors='coerce')) to allow gracefully handling cast failures by converting them to null values.

What changes are included in this PR?

  • Added errors parameter to cast() function with options 'raise' (default) and 'coerce'
  • When errors='coerce', sets CastOptions.null_on_error = True to produce nulls for invalid casts
  • Added is_castable() meta-function to check element-wise castability
  • Comprehensive test suite in test_coerce_cast.py

Are these changes tested?

Yes, added extensive tests covering:

  • Basic coercion behavior
  • Edge cases (empty strings, "nan", None values)
  • Temporal type casting
  • Integration with existing safe parameter
  • Instance methods (array.cast(), chunked_array.cast())
  • Error validation

Are there any user-facing changes?

Yes - new errors parameter for pyarrow.compute.cast() and new is_castable() function.

Shashwati added 2 commits January 19, 2026 17:32
…able function

- Add errors parameter to cast() function with 'raise' (default) and 'coerce' options
- errors='coerce' converts invalid values to null instead of raising errors
- Add errors parameter to Array.cast(), Scalar.cast(), and ChunkedArray.cast() instance methods
- Verify is_castable() function is properly exposed and working
- Add comprehensive tests including the exact example from issue apache#48972
- Update documentation with examples showing errors='coerce' usage

This addresses issue apache#48972 by providing pandas.to_numeric(errors='coerce')
equivalent functionality in PyArrow.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant