|
2 | 2 |
|
3 | 3 | from abc import abstractmethod |
4 | 4 | from collections.abc import Mapping |
5 | | -from typing import TYPE_CHECKING, Literal, Optional, Union |
| 5 | +from typing import TYPE_CHECKING, Literal, Optional, TypedDict, Union |
6 | 6 |
|
7 | 7 | import narwhals.stable.v1 as nw |
8 | 8 | from narwhals.stable.v1.dtypes import DType as NarwhalsDType |
@@ -30,6 +30,14 @@ class _BoundedDType(_DType): |
30 | 30 | _max: int | float |
31 | 31 |
|
32 | 32 |
|
| 33 | +class _ColumnKwargs(TypedDict): |
| 34 | + name: Optional[str] |
| 35 | + nullable: bool |
| 36 | + required: bool |
| 37 | + cast: bool |
| 38 | + checks: Optional[list[Check]] |
| 39 | + |
| 40 | + |
33 | 41 | class _Column: |
34 | 42 | """Represents a column in a DataFrame. |
35 | 43 |
|
@@ -945,11 +953,15 @@ def __to_narwhals(self): |
945 | 953 | return nw.Datetime(time_unit=self.time_unit, time_zone=self.time_zone) |
946 | 954 |
|
947 | 955 | @staticmethod |
948 | | - def from_narwhals(nw_dtype: nw.Datetime) -> Datetime: |
| 956 | + def from_narwhals(nw_dtype: nw.Datetime, **column_kwargs) -> Datetime: |
949 | 957 | if hasattr(nw_dtype, "time_unit"): |
950 | | - return Datetime(time_unit=nw_dtype.time_unit, time_zone=nw_dtype.time_zone) |
| 958 | + return Datetime( |
| 959 | + time_unit=nw_dtype.time_unit, |
| 960 | + time_zone=nw_dtype.time_zone, |
| 961 | + **column_kwargs, |
| 962 | + ) |
951 | 963 |
|
952 | | - return Datetime() |
| 964 | + return Datetime(**column_kwargs) |
953 | 965 |
|
954 | 966 | @staticmethod |
955 | 967 | def _safe_cast(s: nw.Series, to_dtype: _DType) -> nw.Series: |
@@ -987,11 +999,11 @@ def __to_narwhals(self): |
987 | 999 | return nw.Duration(time_unit=self.time_unit) |
988 | 1000 |
|
989 | 1001 | @staticmethod |
990 | | - def from_narwhals(nw_dtype: nw.Duration) -> Duration: |
| 1002 | + def from_narwhals(nw_dtype: nw.Duration, **column_kwargs) -> Duration: |
991 | 1003 | if hasattr(nw_dtype, "time_unit"): |
992 | | - return Duration(nw_dtype.time_unit) |
| 1004 | + return Duration(nw_dtype.time_unit, **column_kwargs) |
993 | 1005 |
|
994 | | - return Duration() |
| 1006 | + return Duration(**column_kwargs) |
995 | 1007 |
|
996 | 1008 | @staticmethod |
997 | 1009 | def _safe_cast(s: nw.Series, to_dtype: _DType) -> nw.Series: |
@@ -1113,8 +1125,10 @@ def to_narwhals(self): # type: ignore |
1113 | 1125 | return nw.Array(self.inner.to_narwhals(), self.shape) |
1114 | 1126 |
|
1115 | 1127 | @staticmethod |
1116 | | - def from_narwhals(nw_dtype: nw.Array) -> Array: |
1117 | | - return Array(_nw_type_to_cf_type(nw_dtype.inner), shape=nw_dtype.shape) |
| 1128 | + def from_narwhals(nw_dtype: nw.Array, **column_kwargs) -> Array: |
| 1129 | + return Array( |
| 1130 | + _nw_type_to_cf_type(nw_dtype.inner), shape=nw_dtype.shape, **column_kwargs |
| 1131 | + ) |
1118 | 1132 |
|
1119 | 1133 | @staticmethod |
1120 | 1134 | def _safe_cast(s: nw.Series, to_dtype: _DType) -> nw.Series: |
@@ -1148,8 +1162,8 @@ def to_narwhals(self): # type: ignore[override] |
1148 | 1162 | return nw.List(self.inner.to_narwhals()) |
1149 | 1163 |
|
1150 | 1164 | @staticmethod |
1151 | | - def from_narwhals(nw_dtype: nw.List) -> List: |
1152 | | - return List(_nw_type_to_cf_type(nw_dtype.inner)) |
| 1165 | + def from_narwhals(nw_dtype: nw.List, **column_kwargs) -> List: |
| 1166 | + return List(_nw_type_to_cf_type(nw_dtype.inner), **column_kwargs) |
1153 | 1167 |
|
1154 | 1168 | @staticmethod |
1155 | 1169 | def _safe_cast(s: nw.Series, to_dtype: _DType) -> nw.Series: |
@@ -1196,56 +1210,58 @@ def to_narwhals(self) -> nw.Struct: # type: ignore |
1196 | 1210 | return nw.Struct(dct) |
1197 | 1211 |
|
1198 | 1212 | @staticmethod |
1199 | | - def from_narwhals(nw_dtype: nw.Struct) -> Struct: |
| 1213 | + def from_narwhals(nw_dtype: nw.Struct, **column_kwargs) -> Struct: |
1200 | 1214 | dct = {} |
1201 | 1215 | for field in nw_dtype.fields: |
1202 | 1216 | dct[field.name] = _nw_type_to_cf_type(field.dtype) |
1203 | 1217 |
|
1204 | | - return Struct(dct) |
| 1218 | + return Struct(dct, **column_kwargs) |
1205 | 1219 |
|
1206 | 1220 | @staticmethod |
1207 | 1221 | def _safe_cast(s: nw.Series, to_dtype: _DType) -> nw.Series: |
1208 | 1222 | return _checked_cast(s, to_dtype) |
1209 | 1223 |
|
1210 | 1224 |
|
1211 | | -_NARWHALS_DTYPE_TO_CHECKEDFRAME_DTYPE_MAPPER: dict[type[NarwhalsDType], _DType] = { |
1212 | | - nw.Binary: Binary(), |
1213 | | - nw.Boolean: Boolean(), |
1214 | | - nw.Categorical: Categorical(), |
1215 | | - nw.Date: Date(), |
1216 | | - nw.Datetime: Datetime(), |
1217 | | - nw.Decimal: Decimal(), |
1218 | | - nw.Enum: Enum(), |
1219 | | - nw.Float32: Float32(), |
1220 | | - nw.Float64: Float64(), |
1221 | | - nw.Int8: Int8(), |
1222 | | - nw.Int16: Int16(), |
1223 | | - nw.Int32: Int32(), |
1224 | | - nw.Int64: Int64(), |
1225 | | - nw.Int128: Int128(), |
1226 | | - nw.Object: Object(), |
1227 | | - nw.String: String(), |
1228 | | - nw.UInt8: UInt8(), |
1229 | | - nw.UInt16: UInt16(), |
1230 | | - nw.UInt32: UInt32(), |
1231 | | - nw.UInt64: UInt64(), |
1232 | | - nw.UInt128: UInt128(), |
1233 | | - nw.Unknown: Unknown(), |
| 1225 | +_NARWHALS_DTYPE_TO_CHECKEDFRAME_DTYPE_MAPPER: dict[ |
| 1226 | + type[NarwhalsDType], type[_DType] |
| 1227 | +] = { |
| 1228 | + nw.Binary: Binary, |
| 1229 | + nw.Boolean: Boolean, |
| 1230 | + nw.Categorical: Categorical, |
| 1231 | + nw.Date: Date, |
| 1232 | + nw.Datetime: Datetime, |
| 1233 | + nw.Decimal: Decimal, |
| 1234 | + nw.Enum: Enum, |
| 1235 | + nw.Float32: Float32, |
| 1236 | + nw.Float64: Float64, |
| 1237 | + nw.Int8: Int8, |
| 1238 | + nw.Int16: Int16, |
| 1239 | + nw.Int32: Int32, |
| 1240 | + nw.Int64: Int64, |
| 1241 | + nw.Int128: Int128, |
| 1242 | + nw.Object: Object, |
| 1243 | + nw.String: String, |
| 1244 | + nw.UInt8: UInt8, |
| 1245 | + nw.UInt16: UInt16, |
| 1246 | + nw.UInt32: UInt32, |
| 1247 | + nw.UInt64: UInt64, |
| 1248 | + nw.UInt128: UInt128, |
| 1249 | + nw.Unknown: Unknown, |
1234 | 1250 | } |
1235 | 1251 |
|
1236 | 1252 |
|
1237 | 1253 | def _nw_type_to_cf_type( |
1238 | | - nw_dtype: Union[NarwhalsDType, type[NarwhalsDType]], |
| 1254 | + nw_dtype: Union[NarwhalsDType, type[NarwhalsDType]], **column_kwargs |
1239 | 1255 | ) -> _DType: |
1240 | 1256 | if isinstance(nw_dtype, nw.Array): |
1241 | | - return Array.from_narwhals(nw_dtype) |
| 1257 | + return Array.from_narwhals(nw_dtype, **column_kwargs) |
1242 | 1258 | elif isinstance(nw_dtype, nw.List): |
1243 | | - return List.from_narwhals(nw_dtype) |
| 1259 | + return List.from_narwhals(nw_dtype, **column_kwargs) |
1244 | 1260 | elif isinstance(nw_dtype, nw.Struct): |
1245 | | - return Struct.from_narwhals(nw_dtype) |
| 1261 | + return Struct.from_narwhals(nw_dtype, **column_kwargs) |
1246 | 1262 | elif isinstance(nw_dtype, nw.Datetime): |
1247 | | - return Datetime.from_narwhals(nw_dtype) |
| 1263 | + return Datetime.from_narwhals(nw_dtype, **column_kwargs) |
1248 | 1264 | elif isinstance(nw_dtype, nw.Duration): |
1249 | | - return Duration.from_narwhals(nw_dtype) |
| 1265 | + return Duration.from_narwhals(nw_dtype, **column_kwargs) |
1250 | 1266 |
|
1251 | | - return _NARWHALS_DTYPE_TO_CHECKEDFRAME_DTYPE_MAPPER[nw_dtype] # type: ignore |
| 1267 | + return _NARWHALS_DTYPE_TO_CHECKEDFRAME_DTYPE_MAPPER[nw_dtype](**column_kwargs) # type: ignore |
0 commit comments