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

[BUG]: accept "iterable[pa.Table]" for from_arrow #2583

Merged
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
4 changes: 2 additions & 2 deletions daft/convert.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# isort: dont-add-import: from __future__ import annotations

from typing import TYPE_CHECKING, Any, Dict, List, Union
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Union

from daft.api_annotations import PublicAPI

Expand Down Expand Up @@ -55,7 +55,7 @@ def from_pydict(data: Dict[str, InputListType]) -> "DataFrame":


@PublicAPI
def from_arrow(data: Union["pa.Table", List["pa.Table"]]) -> "DataFrame":
def from_arrow(data: Union["pa.Table", List["pa.Table"], Iterable["pa.Table"]]) -> "DataFrame":
"""Creates a DataFrame from a pyarrow Table.

Example:
Expand Down
4 changes: 3 additions & 1 deletion daft/dataframe/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,10 @@ def _from_pydict(cls, data: Dict[str, InputListType]) -> "DataFrame":
return cls._from_tables(data_vpartition)

@classmethod
def _from_arrow(cls, data: Union["pyarrow.Table", List["pyarrow.Table"]]) -> "DataFrame":
def _from_arrow(cls, data: Union["pyarrow.Table", List["pyarrow.Table"], Iterable["pyarrow.Table"]]) -> "DataFrame":
"""Creates a DataFrame from a `pyarrow Table <https://arrow.apache.org/docs/python/generated/pyarrow.Table.html>`__."""
if isinstance(data, Iterable):
data = list(data)
if not isinstance(data, list):
data = [data]
data_vpartitions = [MicroPartition.from_arrow(table) for table in data]
Expand Down
15 changes: 15 additions & 0 deletions tests/table/test_from_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pyarrow.compute as pac
import pytest

import daft
from daft import DataType, TimeUnit
from daft.context import get_context
from daft.series import Series
Expand Down Expand Up @@ -649,3 +650,17 @@ def test_nested_struct_dates(levels: int) -> None:

assert back_again.to_arrow().type == expected_arrow_type
assert back_again.to_pylist() == data


def test_from_arrow_iterable() -> None:
class CustomIterable:
def __iter__(self):
yield pa.table({"text": ["foo1", "bar2"]})
yield pa.table({"text": ["foo2", "bar2"]})
yield pa.table({"text": ["foo3", "bar3"]})

my_iter = CustomIterable()

table = daft.from_arrow(my_iter)
tbl = table.to_pydict()
assert tbl == {"text": ["foo1", "bar2", "foo2", "bar2", "foo3", "bar3"]}
Loading