8000 BUG: Handle zero-chunked pyarrow.ChunkedArray in StringArray by xhochy · Pull Request #41052 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

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 1 commit
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
Prev Previous commit
Fix IntervalArray and PeriodArray
  • Loading branch information
xhochy committed Apr 21, 2021
commit 5ce19cf1b3c121a6c3f1e2cfe828627051241704
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
< C9EB td id="diff-e104cef1537d180aafff4c5c8aa8d3c895443b2c039eadfe60d6f6245802cd91R279" data-line-number="279" class="blob-num blob-num-addition js-linkable-line-number js-blob-rnum">
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
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
0