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

fix(python): Don't consume c_stream as iterable #20899

Merged
merged 1 commit into from
Jan 26, 2025
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
6 changes: 5 additions & 1 deletion py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,11 @@ def __init__(
data, schema=schema, schema_overrides=schema_overrides, strict=strict
)

elif not isinstance(data, Sized) and isinstance(data, (Generator, Iterable)):
elif (
not hasattr(data, "__arrow_c_stream__")
and not isinstance(data, Sized)
and isinstance(data, (Generator, Iterable))
):
self._df = iterable_to_pydf(
data,
schema=schema,
Expand Down
2 changes: 1 addition & 1 deletion py-polars/polars/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def __init__(
):
self._s = pandas_to_pyseries(name, values, dtype=dtype, strict=strict)

elif _is_generator(values):
elif not hasattr(values, "__arrow_c_stream__") and _is_generator(values):
self._s = iterable_to_pyseries(name, values, dtype=dtype, strict=strict)

elif isinstance(values, Series):
Expand Down
8 changes: 7 additions & 1 deletion py-polars/tests/unit/constructors/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1703,7 +1703,7 @@ class PyCapsuleStreamHolder:
"""
Hold the Arrow C Stream pycapsule.

A class that exposes _only_ the Arrow C Stream interface via Arrow PyCapsules. This
A class that exposes the Arrow C Stream interface via Arrow PyCapsules. This
ensures that the consumer is seeing _only_ the `__arrow_c_stream__` dunder, and that
nothing else (e.g. the dataframe or array interface) is actually being used.
"""
Expand All @@ -1716,6 +1716,12 @@ def __init__(self, arrow_obj: object) -> None:
def __arrow_c_stream__(self, requested_schema: object = None) -> object:
return self.arrow_obj.__arrow_c_stream__(requested_schema)

def __iter__(self) -> None:
return

def __next__(self) -> None:
return


class PyCapsuleArrayHolder:
"""
Expand Down
Loading