|
2 | 2 |
|
3 | 3 | from typing import TYPE_CHECKING |
4 | 4 | from typing import Any |
| 5 | +from typing import Callable |
| 6 | +from typing import Iterable |
5 | 7 | from typing import Iterator |
6 | 8 | from typing import Literal |
7 | 9 | from typing import Mapping |
|
17 | 19 | from narwhals._pandas_like.utils import align_series_full_broadcast |
18 | 20 | from narwhals._pandas_like.utils import check_column_names_are_unique |
19 | 21 | from narwhals._pandas_like.utils import convert_str_slice_to_int_slice |
| 22 | +from narwhals._pandas_like.utils import get_dtype_backend |
20 | 23 | from narwhals._pandas_like.utils import horizontal_concat |
21 | 24 | from narwhals._pandas_like.utils import native_to_narwhals_dtype |
22 | 25 | from narwhals._pandas_like.utils import object_native_to_narwhals_dtype |
|
45 | 48 | import pandas as pd |
46 | 49 | import polars as pl |
47 | 50 | from typing_extensions import Self |
| 51 | + from typing_extensions import TypeAlias |
48 | 52 |
|
49 | 53 | from narwhals._pandas_like.expr import PandasLikeExpr |
50 | 54 | from narwhals._pandas_like.group_by import PandasLikeGroupBy |
51 | 55 | from narwhals._pandas_like.namespace import PandasLikeNamespace |
52 | 56 | from narwhals.dtypes import DType |
| 57 | + from narwhals.schema import Schema |
53 | 58 | from narwhals.typing import CompliantDataFrame |
54 | 59 | from narwhals.typing import CompliantLazyFrame |
| 60 | + from narwhals.typing import DTypeBackend |
55 | 61 | from narwhals.typing import SizeUnit |
56 | 62 | from narwhals.typing import _1DArray |
57 | 63 | from narwhals.typing import _2DArray |
58 | 64 | from narwhals.utils import Version |
| 65 | + from narwhals.utils import _FullContext |
| 66 | + |
| 67 | + Constructor: TypeAlias = Callable[..., pd.DataFrame] |
59 | 68 |
|
60 | 69 |
|
61 | 70 | CLASSICAL_NUMPY_DTYPES: frozenset[np.dtype[Any]] = frozenset( |
@@ -103,6 +112,48 @@ def __init__( |
103 | 112 | if validate_column_names: |
104 | 113 | check_column_names_are_unique(native_dataframe.columns) |
105 | 114 |
|
| 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 | + |
106 | 157 | def __narwhals_dataframe__(self: Self) -> Self: |
107 | 158 | return self |
108 | 159 |
|
|
0 commit comments