Skip to content

Commit

Permalink
reorder pycapsule interface checks
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron committed Jul 21, 2024
1 parent f4f1162 commit 763e153
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
29 changes: 15 additions & 14 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,20 +390,6 @@ def __init__(
nan_to_null=nan_to_null,
)

elif hasattr(data, "__arrow_c_array__"):
# This uses the fact that PySeries.from_arrow_c_array will create a
# struct-typed Series. Then we unpack that to a DataFrame.
tmp_col_name = ""
s = wrap_s(PySeries.from_arrow_c_array(data))
self._df = s.to_frame(tmp_col_name).unnest(tmp_col_name)._df

elif hasattr(data, "__arrow_c_stream__"):
# This uses the fact that PySeries.from_arrow_c_stream will create a
# struct-typed Series. Then we unpack that to a DataFrame.
tmp_col_name = ""
s = wrap_s(PySeries.from_arrow_c_stream(data))
self._df = s.to_frame(tmp_col_name).unnest(tmp_col_name)._df

elif _check_for_pyarrow(data) and isinstance(data, pa.Table):
self._df = arrow_to_pydf(
data, schema=schema, schema_overrides=schema_overrides, strict=strict
Expand All @@ -428,6 +414,21 @@ def __init__(
self._df = dataframe_to_pydf(
data, schema=schema, schema_overrides=schema_overrides, strict=strict
)

elif hasattr(data, "__arrow_c_array__"):
# This uses the fact that PySeries.from_arrow_c_array will create a
# struct-typed Series. Then we unpack that to a DataFrame.
tmp_col_name = ""
s = wrap_s(PySeries.from_arrow_c_array(data))
self._df = s.to_frame(tmp_col_name).unnest(tmp_col_name)._df

elif hasattr(data, "__arrow_c_stream__"):
# This uses the fact that PySeries.from_arrow_c_stream will create a
# struct-typed Series. Then we unpack that to a DataFrame.
tmp_col_name = ""
s = wrap_s(PySeries.from_arrow_c_stream(data))
self._df = s.to_frame(tmp_col_name).unnest(tmp_col_name)._df

else:
msg = (
f"DataFrame constructor called with unsupported type {type(data).__name__!r}"
Expand Down
12 changes: 6 additions & 6 deletions py-polars/polars/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,6 @@ def __init__(
if dtype is not None:
self._s = self.cast(dtype, strict=strict)._s

elif hasattr(values, "__arrow_c_array__"):
self._s = PySeries.from_arrow_c_array(values)

elif hasattr(values, "__arrow_c_stream__"):
self._s = PySeries.from_arrow_c_stream(values)

elif _check_for_pyarrow(values) and isinstance(
values, (pa.Array, pa.ChunkedArray)
):
Expand All @@ -347,6 +341,12 @@ def __init__(
original_name, values, dtype=dtype, strict=strict
)

elif hasattr(values, "__arrow_c_array__"):
self._s = PySeries.from_arrow_c_array(values)

elif hasattr(values, "__arrow_c_stream__"):
self._s = PySeries.from_arrow_c_stream(values)

else:
msg = (
f"Series constructor called with unsupported type {type(values).__name__!r}"
Expand Down

0 comments on commit 763e153

Please sign in to comment.