Skip to content

BUG: Handle zero-chunked pyarrow.ChunkedArray in StringArray #41052

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

Merged
merged 5 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ Conversion
Strings
^^^^^^^

-
- Bug in the conversion from ``pyarrow.ChunkedArray`` to :class:`~arrays.StringArray` when the original had zero chunks (:issue:`41040`)
-

Interval
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ def __from_arrow__(
bool_arr = BooleanArray._from_sequence(np.array(arr))
results.append(bool_arr)

return BooleanArray._concat_same_type(results)
if not results:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not results:
if len(results) == 0:

? (I find that more explicit / readable)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can simply revert f4a2c8c (did do that commit as @phofl suggested)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, missed that comment. Sorry for the contradicting feedback ;)

(let's leave it for now then, something to discuss / agree on as pandas team)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saw this on a few prs in the past that either not was used or it was recommended to use not, hence my feedback, sorry if I misinterpreted this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry if I misinterpreted this

No, no, nothing to be sorry about! I know that others prefer that and give that feedback, I personally just don't agree with it being a better style ;)

return BooleanArray(
np.array([], dtype=np.bool_), np.array([], dtype=np.bool_)
)
else:
return BooleanArray._concat_same_type(results)


def coerce_to_array(
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/arrays/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ def __from_arrow__(
num_arr = array_class(data.copy(), ~mask, copy=False)
results.append(num_arr)

if len(results) == 1:
if not results:
return array_class(
np.array([], dtype=self.numpy_dtype), np.array([], dtype=np.bool_)
)
elif len(results) == 1:
# avoid additional copy in _concat_same_type
return results[0]
else:
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ def __from_arrow__(
str_arr = StringArray._from_sequence(np.array(arr))
results.append(str_arr)

return StringArray._concat_same_type(results)
if results:
return StringArray._concat_same_type(results)
else:
return StringArray(np.array([], dtype="object"))


class StringArray(PandasArray):
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,8 @@ def __from_arrow__(
parr[~mask] = NaT
results.append(parr)

if not results:
return PeriodArray(np.array([], dtype="int64"), freq=self.freq, copy=False)
return PeriodArray._concat_same_type(results)


Expand Down Expand Up @@ -1238,6 +1240,12 @@ def __from_arrow__(
iarr = IntervalArray.from_arrays(left, right, closed=array.type.closed)
results.append(iarr)

if not results:
return IntervalArray.from_arrays(
np.array([], dtype=self.subtype),
np.array([], dtype=self.subtype),
closed=array.type.closed,
)
return IntervalArray._concat_same_type(results)

def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None:
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/arrays/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,13 @@ def test_arrow_table_roundtrip(breaks):
expected = pd.concat([df, df], ignore_index=True)
tm.assert_frame_equal(result, expected)

# GH-41040
table = pa.table(
[pa.chunked_array([], type=table.column(0).type)], schema=table.schema
)
result = table.to_pandas()
tm.assert_frame_equal(result, expected[0:0])


@pyarrow_skip
@pytest.mark.parametrize(
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/arrays/masked/test_arrow_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ def test_arrow_roundtrip(data):
tm.assert_frame_equal(result, df)


@td.skip_if_no("pyarrow", min_version="0.15.1.dev")
def test_arrow_load_from_zero_chunks(data):
# GH-41040
import pyarrow as pa

df = pd.DataFrame({"a": data[0:0]})
table = pa.table(df)
assert table.field("a").type == str(data.dtype.numpy_dtype)
table = pa.table(
[pa.chunked_array([], type=table.field("a").type)], schema=table.schema
)
result = table.to_pandas()
assert result["a"].dtype == data.dtype
tm.assert_frame_equal(result, df)


@td.skip_if_no("pyarrow", min_version="0.16.0")
def test_arrow_from_arrow_uint():
# https://github.com/pandas-dev/pandas/issues/31896
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/arrays/period/test_arrow_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,26 @@ def test_arrow_table_roundtrip():
tm.assert_frame_equal(result, expected)


@pyarrow_skip
def test_arrow_load_from_zero_chunks():
# GH-41040
import pyarrow as pa

from pandas.core.arrays._arrow_utils import ArrowPeriodType

arr = PeriodArray([], freq="D")
df = pd.DataFrame({"a": arr})

table = pa.table(df)
assert isinstance(table.field("a").type, ArrowPeriodType)
table = pa.table(
[pa.chunked_array([], type=table.column(0).type)], schema=table.schema
)
result = table.to_pandas()
assert isinstance(result["a"].dtype, PeriodDtype)
tm.assert_frame_equal(result, df)


@pyarrow_skip
def test_arrow_table_roundtrip_without_metadata():
import pyarrow as pa
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/arrays/string_/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,22 @@ def test_arrow_roundtrip(dtype, dtype_object):
assert result.loc[2, "a"] is pd.NA


@td.skip_if_no("pyarrow", min_version="0.15.1.dev")
def test_arrow_load_from_zero_chunks(dtype, dtype_object):
# GH-41040
import pyarrow as pa

data = pd.array([], dtype=dtype)
df = pd.DataFrame({"a": data})
table = pa.table(df)
assert table.field("a").type == "string"
# Instantiate the same table with no chunks at all
table = pa.table([pa.chunked_array([], type=pa.string())], schema=table.schema)
result = table.to_pandas()
assert isinstance(result["a"].dtype, dtype_object)
tm.assert_frame_equal(result, df)


def test_value_counts_na(dtype):
arr = pd.array(["a", "b", "a", pd.NA], dtype=dtype)
result = arr.value_counts(dropna=False)
Expand Down