[Python] `pa.array(pa.array([float("nan")]), from_pandas=True)` does not convert nan to null
Describe the bug, including details regarding any error messages, version, and platform.
In [1]: import pyarrow as pa
In [4]: pa.array(pa.array([float("nan"), None]), from_pandas=True)
Out[4]:
<pyarrow.lib.DoubleArray object at 0x7f91bbd90f40>
[
nan,
null
]
In [5]: pa.__version__
Out[5]: '14.0.1'
If an arrow array is considered an ndarray-like data according to the docs, I would assume the nan would be inferred as null
Component(s)
Python
Hi, I am working on this issue.
Hmm, I am not fully sure what the expected behaviour should be here. We could also document that this keyword is ignored if passing arrow-compatible data.
The issue is that it is not just a pyarrow Array, and not just the from_pandas keyword (eg also mask as @LucasG0 opened another issue: https://github.com/apache/arrow/issues/39445, and in addition also size is being ignore in some cases).
There are various code paths for "Arrow compatible data" (not a generic ndarray-like) that have been added more recently and that ignore keywords:
- pyarrow.Array objects (support added in https://github.com/apache/arrow/pull/34275): ignore all of
mask,sizeandfrom_pandaskeywords, only casts totypeif specified - objects supporting
__arrow_array__: here we explicitly raise an error ifmaskorsizeis specified (but don't raise such error forfrom_pandas) - objects supporting
__arrow_c_array__: same as pyarrow.Array objects, ignoring all three keywords, only casting if needed.
So if we would add support for from_pandas (or mask) for pyarrow.Array, we should probably do it for the others as well to be consistent. But on the other hand, I don't feel comfortable doing that for the protocols that return Arrow data (it seems a responsibility of the data producer to know whether NaNs should be converted or not).
Thanks for the thoughts. I don't have a strong opinion whether these keywords should be respected or not, but yes documenting or raising in constructors would be helpful on how these keywords apply when passing arrow data
Thanks for your answer @jorisvandenbossche, I do not have a strong opinion either. I understand that protocols returning Arrow data should not support mask / size / from_pandas. Should a pyarrow.Array be allowed to behave differently than objects supporting __arrow_array__ / __arrow_c_array__, in which case it would make sense to support these parameters for it? Otherwise, passing an Array to pa.array is just a wrapper around cast.
If not, would it be better to raise when any of these parameters is filled instead of documenting that they are ignored for a pyarrow.Array input? In order to keep consistent with objects supporting __arrow_array__ / __arrow_c_array__ that raise for these parameters and avoid confusion.
In any case, I think objects supporting __arrow_array__ / __arrow_c_array__ should raise on from_pandas as they already raise on mask / size.
Should a
pyarrow.Arraybe allowed to behave differently than objects supporting__arrow_array__/__arrow_c_array__
Give that pyarrow.Array itself is also an object that supports __arrow_c_aray__, the easier explanation would be that it behaves the same, then we don't need to mention that exception in the docstring.
Otherwise, passing an Array to pa.array is just a wrapper around
cast.
Yes, that's essentially it at the moment. We originally added it mostly for convenience (so you could just use pa.array(obj) to ensure to have a pyarrow.Array, without first having to check if obj already was one), and not to have those additional capabilities of pa.array(..). But we should have discussed that more when adding this support.
If not, would it be better to raise when any of these parameters is filled instead of documenting that they are ignored for a
pyarrow.Arrayinput?
Yes, if the outcome is to not support those parameters, we indeed should raise an error, in addition to documenting this.
I think I would personally lean to not support those keywords for now, and start with properly raising an error and documenting this (it's also easier to add support later if there are good motivating use cases, than to remove support).
We could maybe also document how you can achieve the same effect (convert NaNs to null, or apply a mask) with a pyarrow.Array, using the existing (compute) functionalities (or potentially add methods to make this easier, if it is complicated right now).