Skip to content

Commit

Permalink
Array constructor correctly convert nans from an array with from_pand…
Browse files Browse the repository at this point in the history
…as=True
  • Loading branch information
LucasG0 committed Jan 4, 2024
1 parent 6c39726 commit 36def07
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python/pyarrow/array.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,13 @@ def array(object obj, type=None, mask=None, size=None, from_pandas=None,
c_from_pandas = from_pandas

if isinstance(obj, Array):
from pyarrow.types import is_floating
if type is not None and not obj.type.equals(type):
obj = obj.cast(type, safe=safe, memory_pool=memory_pool)
if is_floating(obj.type):
mask_nulls_and_nans = obj.is_null(nan_is_null=True)
obj = _pc().replace_with_mask(obj, mask_nulls_and_nans, nulls(
mask_nulls_and_nans.sum().as_py(), type=obj.type))
return obj

if hasattr(obj, '__arrow_array__'):
Expand Down
6 changes: 6 additions & 0 deletions python/pyarrow/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2183,6 +2183,12 @@ def test_pandas_null_sentinels_index():
assert result.equals(expected)


def test_array_from_array_with_pandas_nans():
result = pa.array(pa.array([np.nan, None]), from_pandas=True)
expected = pa.nulls(2, type=pa.float64())
assert result.equals(expected)


def test_array_roundtrip_from_numpy_datetimeD():
arr = np.array([None, datetime.date(2017, 4, 4)], dtype='datetime64[D]')

Expand Down

0 comments on commit 36def07

Please sign in to comment.