-
Notifications
You must be signed in to change notification settings - Fork 185
refactor: Add CompliantDataFrame.from_arrow
#2306
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
Changes from all commits
12330d5
83b2752
7c306dc
038c8da
51bef83
79f080a
1812d28
1f92a58
41e4034
e52edee
8d95cb2
299be9c
976cffb
7a31e65
f5c79cc
e84a035
392a611
cb6c86f
883b927
655da1d
adb6bf1
bdeaaac
7bf8b27
3843bc2
ced6d07
5802361
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
| from narwhals.exceptions import InvalidOperationError | ||
| from narwhals.exceptions import ShapeError | ||
| from narwhals.utils import Implementation | ||
| from narwhals.utils import _into_arrow_table | ||
| from narwhals.utils import _remap_full_join_keys | ||
| from narwhals.utils import check_column_exists | ||
| from narwhals.utils import generate_temporary_column_name | ||
|
|
@@ -55,6 +56,7 @@ | |
| from narwhals._pandas_like.expr import PandasLikeExpr | ||
| from narwhals._pandas_like.group_by import PandasLikeGroupBy | ||
| from narwhals._pandas_like.namespace import PandasLikeNamespace | ||
| from narwhals._translate import IntoArrowTable | ||
| from narwhals.dtypes import DType | ||
| from narwhals.schema import Schema | ||
| from narwhals.typing import CompliantDataFrame | ||
|
|
@@ -114,6 +116,29 @@ def __init__( | |
| if validate_column_names: | ||
| check_column_names_are_unique(native_dataframe.columns) | ||
|
|
||
| @classmethod | ||
| def from_arrow(cls, data: IntoArrowTable, /, *, context: _FullContext) -> Self: | ||
| implementation = context._implementation | ||
| tbl = _into_arrow_table(data, context) | ||
| if implementation.is_pandas(): | ||
| native = tbl.to_pandas() | ||
| elif implementation.is_modin(): # pragma: no cover | ||
| from modin.pandas.utils import from_arrow as mpd_from_arrow | ||
|
|
||
| native = mpd_from_arrow(tbl) | ||
| elif implementation.is_cudf(): # pragma: no cover | ||
| native = implementation.to_native_namespace().DataFrame.from_arrow(tbl) | ||
| else: # pragma: no cover | ||
| msg = "congratulations, you entered unreachable code - please report a bug" | ||
| raise AssertionError(msg) | ||
|
Comment on lines
+132
to
+133
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol, can't believe i wrote this, we may want to make a utility with a consistent message for these at some point
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Certainly made me smile when I copied it over π |
||
| return cls( | ||
| native, | ||
| implementation=implementation, | ||
| backend_version=context._backend_version, | ||
| version=context._version, | ||
| validate_column_names=True, | ||
| ) | ||
|
|
||
| @classmethod | ||
| def from_dict( | ||
| cls, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Supporting
Collectionhere might seem strange.My thinking is that were a
list | tuple | dictofpyarrowobjects passed -pa.tablewould still be capable of turning that into apa.Table:https://github.com/apache/arrow/blob/7c1b96e26910dd553ee0ccf0792e1854af6b0021/python/pyarrow/table.pxi#L4993-L5000