You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@arrow.apache.org by "randolf-scholz (via GitHub)" <gi...@apache.org> on 2026/01/23 17:04:07 UTC

[I] EHN: Add a `is_castable` function and/or `errors=coerce` option to `cast` [arrow]

randolf-scholz opened a new issue, #48972:
URL: https://github.com/apache/arrow/issues/48972

   ### Describe the enhancement requested
   
   I'd like to cast a `string` array to `float`, but it can contain bad entried.
   
   ```python
   import pyarrow as pa
   import pyarrow.compute as pc
   
   arr = pa.array(["1.2", "3", "10-20", None, "nan", ""])
   
   out = pc.cast(arr, pa.float64(), safe=False)  # raises ArrowInvalid
   
   print(out)  # E: [1.2, 3, null, null, nan, null]
   ```
   
   My current workaround is to export to `pandas` and use [`pandas.to_numeric(errors="coerce")`](https://pandas.pydata.org/docs/reference/api/pandas.to_numeric.html#pandas-to-numeric).
   However, it would be nice if `pyarrow` had some built-in machinery to deal with this situation:
   
   1. A `errors={"raise", "coerce"}` option like `pandas.to_numeric` that catches conversion errors other than overflow and truncation.
   
   2. Add a function that yields a boolean mask of all values that are castable.
   
       ```python
       def is_castable(arr, target_type, options=None) -> Array[bool]:
           """Returns boolean mask of values that can be cast to target_type,
           under the chosen options."""
       ```
       
       Such a function would also be useful for extracting the set of all values that cannot be cast.
       
   
   
   
   ### Component(s)
   
   C++, Python


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@arrow.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] EHN: Add a `is_castable` function and/or `errors=coerce` option to `cast` [arrow]

Posted by "shashbha14 (via GitHub)" <gi...@apache.org>.
shashbha14 commented on issue #48972:
URL: https://github.com/apache/arrow/issues/48972#issuecomment-3798099827

   take


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] EHN: Add a `is_castable` function and/or `errors=coerce` option to `cast` [arrow]

Posted by "rok (via GitHub)" <gi...@apache.org>.
rok commented on issue #48972:
URL: https://github.com/apache/arrow/issues/48972#issuecomment-3880272786

   Hey @randolf-scholz, did you consider something like:
   
   ```python
   arr = pa.array(["1.2", "abc", "ba", "c", None])
   mask = pc.match_substring_regex(arr, r"^\d+[.,]\d+")
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org