Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add IntegerArray.__arrow_array__ for custom conversion to Arrow #28368

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,14 @@ def __array__(self, dtype=None):
"""
return self._coerce_to_ndarray()

def __arrow_array__(self, type=None):
"""
Convert myself into a pyarrow Array.
"""
import pyarrow as pa

return pa.array(self._data, mask=self._mask, type=type)

_HANDLED_TYPES = (np.ndarray, numbers.Number)

def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/arrays/test_integer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
import pytest

import pandas.util._test_decorators as td

from pandas.core.dtypes.generic import ABCIndexClass

import pandas as pd
Expand Down Expand Up @@ -817,6 +819,16 @@ def test_ufunc_reduce_raises(values):
np.add.reduce(a)


@td.skip_if_no("pyarrow", min_version="0.14.1.dev")
def test_arrow_array(data):
# protocol added in 0.15.0
import pyarrow as pa

arr = pa.array(data)
expected = pa.array(list(data), type=data.dtype.name.lower(), from_pandas=True)
assert arr.equals(expected)


# TODO(jreback) - these need testing / are broken

# shift
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/io/test_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,18 @@ def test_empty_dataframe(self, pa):
df = pd.DataFrame()
check_round_trip(df, pa)

@td.skip_if_no("pyarrow", min_version="0.14.1.dev")
def test_nullable_integer(self, pa):
df = pd.DataFrame({"a": pd.Series([1, 2, 3], dtype="Int64")})
# currently de-serialized as plain int
expected = df.assign(a=df.a.astype("int64"))
check_round_trip(df, pa, expected=expected)

df = pd.DataFrame({"a": pd.Series([1, 2, 3, None], dtype="Int64")})
# if missing values currently de-serialized as float
expected = df.assign(a=df.a.astype("float64"))
check_round_trip(df, pa, expected=expected)


class TestParquetFastParquet(Base):
@td.skip_if_no("fastparquet", min_version="0.2.1")
Expand Down