Skip to content

Commit b63fa86

Browse files
committed
feat: Add PandasLikeDataFrame.from_numpy
1 parent c689161 commit b63fa86

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

narwhals/_pandas_like/dataframe.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from typing import TYPE_CHECKING
44
from typing import Any
5+
from typing import Callable
6+
from typing import Iterable
57
from typing import Iterator
68
from typing import Literal
79
from typing import Mapping
@@ -17,6 +19,7 @@
1719
from narwhals._pandas_like.utils import align_series_full_broadcast
1820
from narwhals._pandas_like.utils import check_column_names_are_unique
1921
from narwhals._pandas_like.utils import convert_str_slice_to_int_slice
22+
from narwhals._pandas_like.utils import get_dtype_backend
2023
from narwhals._pandas_like.utils import horizontal_concat
2124
from narwhals._pandas_like.utils import native_to_narwhals_dtype
2225
from narwhals._pandas_like.utils import object_native_to_narwhals_dtype
@@ -45,17 +48,23 @@
4548
import pandas as pd
4649
import polars as pl
4750
from typing_extensions import Self
51+
from typing_extensions import TypeAlias
4852

4953
from narwhals._pandas_like.expr import PandasLikeExpr
5054
from narwhals._pandas_like.group_by import PandasLikeGroupBy
5155
from narwhals._pandas_like.namespace import PandasLikeNamespace
5256
from narwhals.dtypes import DType
57+
from narwhals.schema import Schema
5358
from narwhals.typing import CompliantDataFrame
5459
from narwhals.typing import CompliantLazyFrame
60+
from narwhals.typing import DTypeBackend
5561
from narwhals.typing import SizeUnit
5662
from narwhals.typing import _1DArray
5763
from narwhals.typing import _2DArray
5864
from narwhals.utils import Version
65+
from narwhals.utils import _FullContext
66+
67+
Constructor: TypeAlias = Callable[..., pd.DataFrame]
5968

6069

6170
CLASSICAL_NUMPY_DTYPES: frozenset[np.dtype[Any]] = frozenset(
@@ -103,6 +112,48 @@ def __init__(
103112
if validate_column_names:
104113
check_column_names_are_unique(native_dataframe.columns)
105114

115+
@classmethod
116+
def from_numpy(
117+
cls,
118+
data: _2DArray,
119+
/,
120+
*,
121+
context: _FullContext,
122+
schema: Mapping[str, DType] | Schema | Sequence[str] | None,
123+
) -> Self:
124+
from narwhals.schema import Schema
125+
126+
implementation = context._implementation
127+
DataFrame: Constructor = implementation.to_native_namespace().DataFrame # noqa: N806
128+
if isinstance(schema, (Mapping, Schema)):
129+
it: Iterable[DTypeBackend] = (
130+
get_dtype_backend(native_type, implementation)
131+
for native_type in schema.values()
132+
)
133+
native = DataFrame(data, columns=schema.keys()).astype(
134+
Schema(schema).to_pandas(it)
135+
)
136+
elif is_sequence_but_not_str(schema):
137+
native = DataFrame(data, columns=list(schema))
138+
elif schema is None:
139+
native = DataFrame(
140+
data, columns=[f"column_{x}" for x in range(data.shape[1])]
141+
)
142+
else:
143+
msg = (
144+
"`schema` is expected to be one of the following types: "
145+
"Mapping[str, DType] | Schema | Sequence[str]. "
146+
f"Got {type(schema)}."
147+
)
148+
raise TypeError(msg)
149+
return cls(
150+
native,
151+
implementation=implementation,
152+
backend_version=context._backend_version,
153+
version=context._version,
154+
validate_column_names=True,
155+
)
156+
106157
def __narwhals_dataframe__(self: Self) -> Self:
107158
return self
108159

0 commit comments

Comments
 (0)