L iQ0 dZddlmZddlmZddlmZddlmZddl m Z m Z ddl m Z mZmZmZddlmZmZmZmZmZmZmZe r.dd lmZdd l mZmZdd lZdd lZdd l m!Z!dd l"m#Z#ddl$m%Z%m&Z&m'Z'm(Z(dgZ)Gddee*dfZ+y )zxSchema. Adapted from Polars implementation at: https://github.com/pola-rs/polars/blob/main/py-polars/polars/schema.py. ) annotations) OrderedDict)Mapping)partial) TYPE_CHECKINGcast)ImplementationVersionqualified_type_name zip_strict)get_cudf is_cudf_dtypeis_pandas_like_dtypeis_polars_data_typeis_polars_schemais_pyarrow_data_typeis_pyarrow_schema)Iterable)AnyClassVarN)Self)DType) DTypeBackendIntoArrowSchemaIntoPandasSchemaIntoPolarsSchemaSchemaceZdZUdZej Zded< d dfd ZddZ ddZ ddZ e ddZ e dd Ze dd Ze dd Zdd Z d dd ZddZe ddZe ddZxZS)ra<Ordered mapping of column names to their data type. Arguments: schema: The schema definition given by column names and their associated *instantiated* Narwhals data type. Accepts a mapping or an iterable of tuples. Examples: Define a schema by passing *instantiated* data types. >>> import narwhals as nw >>> schema = nw.Schema({"foo": nw.Int8(), "bar": nw.String()}) >>> schema Schema({'foo': Int8, 'bar': String}) Access the data type associated with a specific column name. >>> schema["foo"] Int8 Access various schema properties using the `names`, `dtypes`, and `len` methods. >>> schema.names() ['foo', 'bar'] >>> schema.dtypes() [Int8, String] >>> schema.len() 2 zClassVar[Version]_versionc0|xsi}t||yN)super__init__)selfschema __class__s U/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/narwhals/schema.pyr#zSchema.__init__Ms2  c4t|jS)z#Get the column names of the schema.)listkeysr$s r'namesz Schema.namesSsDIIK  r(c4t|jS)z!Get the data types of the schema.)r*valuesr,s r'dtypesz Schema.dtypesWsDKKM""r(ct|S)z(Get the number of columns in the schema.)lenr,s r'r2z Schema.len[s 4yr(ct|tr|sSddl}|j|}ddlmfd|DS)aConstruct a Schema from a pyarrow Schema. Arguments: schema: A pyarrow Schema or mapping of column names to pyarrow data types. Examples: >>> import pyarrow as pa >>> import narwhals as nw >>> >>> mapping = { ... "a": pa.timestamp("us", "UTC"), ... "b": pa.date32(), ... "c": pa.string(), ... "d": pa.uint8(), ... } >>> native = pa.schema(mapping) >>> >>> nw.Schema.from_arrow(native) Schema({'a': Datetime(time_unit='us', time_zone='UTC'), 'b': Date, 'c': String, 'd': UInt8}) >>> nw.Schema.from_arrow(mapping) == nw.Schema.from_arrow(native) True rNnative_to_narwhals_dtypec3pK|]-}|j|jjf/ywr!)nametyper).0fieldclsr5s r' z$Schema.from_arrow..s2 ZZ1%**cllK L s36) isinstancerpyarrowr%narwhals._arrow.utilsr5)r;r%par5s` @r' from_arrowzSchema.from_arrow_sI2 fg &u RYYv&FB    r(c|s|Str0td|jDrtjntj }|j ||S)a3Construct a Schema from a pandas-like schema representation. Arguments: schema: A mapping of column names to pandas-like data types. Examples: >>> import numpy as np >>> import pandas as pd >>> import pyarrow as pa >>> import narwhals as nw >>> >>> data = {"a": [1], "b": ["a"], "c": [False], "d": [9.2]} >>> native = pd.DataFrame(data).convert_dtypes().dtypes.to_dict() >>> >>> nw.Schema.from_pandas_like(native) Schema({'a': Int64, 'b': String, 'c': Boolean, 'd': Float64}) >>> >>> mapping = { ... "a": pd.DatetimeTZDtype("us", "UTC"), ... "b": pd.ArrowDtype(pa.date32()), ... "c": pd.StringDtype("python"), ... "d": np.dtype("uint8"), ... } >>> >>> nw.Schema.from_pandas_like(mapping) Schema({'a': Datetime(time_unit='us', time_zone='UTC'), 'b': Date, 'c': String, 'd': UInt8}) c32K|]}t|ywr!)r)r9dtypes r'r<z*Schema.from_pandas_like..s!T5-"6!Ts)r anyr/r CUDFPANDAS_from_pandas_like)r;r%impls r'from_pandas_likezSchema.from_pandas_likesV:5Lzc!TFMMO!TT   && $$VT22r(ct|r|j|St|r|j|St |t r|r|j |S|Sdt|d|}t|)aoConstruct a Schema from a native schema representation. Arguments: schema: A native schema object, or mapping of column names to *instantiated* native data types. Examples: >>> import datetime as dt >>> import pyarrow as pa >>> import narwhals as nw >>> >>> data = {"a": [1], "b": ["a"], "c": [dt.time(1, 2, 3)], "d": [[2]]} >>> native = pa.table(data).schema >>> >>> nw.Schema.from_native(native) Schema({'a': Int64, 'b': String, 'c': Time, 'd': List(Int64)}) z5Expected an arrow, polars, or pandas schema, but got z ) rrAr from_polarsr=r_from_native_mappingr TypeError)r;r%msgs r' from_nativezSchema.from_natives* V $>>&) ) F #??6* * fg &7=3++F3 H35 HC"6*-T& = nr(cd|sSddlmfd|jDS)a/Construct a Schema from a polars Schema. Arguments: schema: A polars Schema or mapping of column names to *instantiated* polars data types. Examples: >>> import polars as pl >>> import narwhals as nw >>> >>> mapping = { ... "a": pl.Datetime(time_zone="UTC"), ... "b": pl.Date(), ... "c": pl.String(), ... "d": pl.UInt8(), ... } >>> native = pl.Schema(mapping) >>> >>> nw.Schema.from_polars(native) Schema({'a': Datetime(time_unit='us', time_zone='UTC'), 'b': Date, 'c': String, 'd': UInt8}) >>> nw.Schema.from_polars(mapping) == nw.Schema.from_polars(native) True rr4c3NK|]\}}||jfywr!r)r9r7rDr;r5s r'r<z%Schema.from_polars..s. e+E3<<@ A "%)narwhals._polars.utilsr5items)r;r%r5s` @r'rLzSchema.from_polarss245LC %||~   r(cnddl}ddlm|jfdj DS)aConvert Schema to a pyarrow Schema. Examples: >>> import narwhals as nw >>> schema = nw.Schema({"a": nw.Int64(), "b": nw.Datetime("ns")}) >>> schema.to_arrow() a: int64 b: timestamp[ns] rNnarwhals_to_native_dtypec3NK|]\}}||jfywr!rSr9r7rDrYr$s r'r<z"Schema.to_arrow..s. e+E4==A B rT)r>r?rYr%rV)r$r@rYs` @r'to_arrowzSchema.to_arrows1 Bryy #zz|   r(c ddlm}t|tj|j }|t |tr,|jDcic]\}}||||c}}St|}t|t|k7rnddl m }m }m} t|t|} } t||j|| || | } d| d| d|d |dd | d } t!| t#|j%|j'|Dcic]\}}}||||c}}}Scc}}wcc}}}w) amConvert Schema to an ordered mapping of column names to their pandas data type. Arguments: dtype_backend: Backend(s) used for the native types. When providing more than one, the length of the iterable must be equal to the length of the schema. Examples: >>> import narwhals as nw >>> schema = nw.Schema({"a": nw.Int64(), "b": nw.Datetime("ns")}) >>> schema.to_pandas() {'a': 'int64', 'b': 'datetime64[ns]'} >>> schema.to_pandas("pyarrow") {'a': 'Int64[pyarrow]', 'b': 'timestamp[ns][pyarrow]'} rrX)implementationversion)rD dtype_backend)chainislicerepeatz Provided z) `dtype_backend`(s), but schema contains z1 field(s). Hint: instead of schema.to_pandas(z+) you may want to use schema.to_pandas(z) or schema.to_pandas())narwhals._pandas_like.utilsrYrr rGrr=strrVtupler2 itertoolsrarbrc from_iterable ValueErrorr r+r/)r$r`rYto_native_dtyper7rDbackendsrarbrcn_usern_actual suggestionrObackends r' to_pandaszSchema.to_pandassf$ I! $)00MM   J}c$B$(::<D%oEOO ' x=CI % 7 7"8}c$iHFu**6&2BH+MNPXYJF:%Nxl[((0z2((0 }5((2|1 6 S/ !)3499; x(X  $eW /WE E  -, s E(E cddl}ddlmtjj }fdj D}|dk\r|j|Stdt|S)a%Convert Schema to a polars Schema. Examples: >>> import narwhals as nw >>> schema = nw.Schema({"a": nw.Int64(), "b": nw.Datetime("ns")}) >>> schema.to_polars() Schema({'a': Int64, 'b': Datetime(time_unit='ns', time_zone=None)}) rNrXc3NK|]\}}||jfywr!rSr[s r'r<z#Schema.to_polars..Es. e+E4==A B rT)rr pl.Schema) polarsrUrYr POLARS_backend_versionrVrrdict)r$pl pl_versionr%rYs` @r' to_polarszSchema.to_polars7sh C#**;;=  #zz|  Y& BIIf  k4<0 r(crtt|j}|\}}t|r|j t d|St |r|jt d|St|r|jt d|Sd|dt|d|}t|)Nrrrz7Expected an arrow, polars, or pandas dtype, but found `z: z` ) nextiterrVrrLrrrJrrAr rN)r;native first_item first_key first_dtyperOs r'rMzSchema._from_native_mappingOs $v||~./ !+ ; { +??4(:F#CD D  ,''-?(HI I  ,>>$'8&"AB B{"0=>eF: O nr(cZddlm|fd|jDS)Nrr4c3TK|]\}}||jdf!yw)T) allow_objectNrS)r9r7rDr;rIr5s r'r<z+Schema._from_pandas_like..js3 e+E3<<TXY Z s%()rer5rV)r;r%r^rIr5s` @@r'rHzSchema._from_pandas_likecs. I %||~   r(r!)r%z8Mapping[str, DType] | Iterable[tuple[str, DType]] | NonereturnNone)rz list[str])rz list[DType])rint)r%rrr)r%rrr)r%z5IntoArrowSchema | IntoPolarsSchema | IntoPandasSchemarr)r%rrr)rz pa.Schema)r`z%DTypeBackend | Iterable[DTypeBackend]rzdict[str, Any])rru)rzHMapping[str, pa.DataType] | Mapping[str, pl.DataType] | IntoPandasSchemarr)r%rr^r rr)__name__ __module__ __qualname____doc__r MAINr__annotations__r#r-r0r2 classmethodrArJrPrLr\rqr|rMrH __classcell__)r&s@r'rr-s!:#*,,H.RV!N! ! !## # J#3#3JJ @    D (FJ3 B3 3 j 0X  &  %  7E      r(r),r __future__r collectionsrcollections.abcr functoolsrtypingrrnarwhals._utilsr r r r narwhals.dependenciesr rrrrrrrrrrvrzr>r@typing_extensionsrnarwhals.dtypesrnarwhals.typingrrrr__all__rfrr(r'rsq ###&TT($&% *@ [g &@ r(