L ixeddlmZddlZddlmZddlmZddlm Z m Z m Z m Z m Z mZddlmZddlmZmZddlmZmZmZmZdd lmZmZmZmZmZmZdd l m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-e r$dd l.m/Z/m0Z0dd l1m2Z2dd l3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;me?e@eAeBeCefZDejejejfZHedd d;dZIedd ddZIdd d?dZIed@dZJedAdZJedBdZJedd dCdZJedddd dDdZJedddd dEdZJeddd dFd ZJeddd dGd!ZJeddd" dHd#ZJeddd$ dId%ZJeddddd& dJd'ZJeddddd& dKd(ZJedddd) dLd*ZJedddd+ dMd,ZJedddd- dNd.ZJe dOd/ZJddddd& dPd0ZJddd1 dQd2ZKddd1 dRd3ZLdSd4ZM dSd5ZN dTd6ddd6d& dUd7ZOdVd8ZPdWd9ZQgd:ZRy)X) annotationsN)Decimalwraps) TYPE_CHECKINGAnyCallableLiteralTypeVaroverload)plugins)EPOCH MS_PER_SECOND)is_native_arrowis_native_pandas_likeis_native_polarsis_native_spark_like)ImplementationVersionhas_native_namespaceis_compliant_dataframeis_compliant_lazyframeis_compliant_series) get_dask_expr get_numpy get_pandasis_cupy_scalaris_dask_dataframeis_duckdb_relation is_ibis_tableis_numpy_scalaris_pandas_like_dataframeis_polars_lazyframeis_polars_seriesis_pyarrow_scalaris_pyarrow_table DataFrame LazyFrameSeries) DataFrameTFrameIntoDataFrameT IntoFrameIntoLazyFrameT IntoSeries IntoSeriesT LazyFrameTSeriesTT. pass_throughcyNnarwhals_objectr7s X/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/narwhals/translate.py to_nativer>=cyr9r:r;s r=r>r>Ar?r@cyr9r:r;s r=r>r>Esr@cyr9r:r;s r=r>r>IsCFr@Fcddlm}ddlm}t ||r|j j St ||r|jjS|sdt|d}t||S)a]Convert Narwhals object to native one. Arguments: narwhals_object: Narwhals object. pass_through: Determine what happens if `narwhals_object` isn't a Narwhals class - `False` (default): raise an error - `True`: pass object through as-is Returns: Object of class that user started with. r) BaseFramer*zExpected Narwhals object, got .) narwhals.dataframerEnarwhals.seriesr+ isinstance_compliant_frame _native_frame_compliant_seriesnativetype TypeError)r<r7rEr+msgs r=r>r>Msi&-&/9-//===/6*00777 .tO/D.EQGn r@c yr9r: native_objectkwdss r= from_nativerUnsADr@c yr9r:rRs r=rUrUrGJr@c yr9r:rRs r=rUrUvrWr@) series_onlycyr9r:rSr7 eager_onlyrY allow_seriess r=rUrUzs7:r@)r\rYr]cyr9r:r[s r=rUrU!$r@cyr9r:r[s r=rUrU r@)rYr]cyr9r:r[s r=rUrUr_r@cyr9r:r[s r=rUrUrar@)r\rYcyr9r:r[s r=rUrUs SVr@)r\r]cyr9r:r[s r=rUrUr@r7r\rYr]cyr9r:r[s r=rUrUr_r@cyr9r:r[s r=rUrUr_r@)r7rYr]cyr9r:r[s r=rUrUr_r@)r7r\rYcyr9r:r[s r=rUrUs58r@)r7r\r]cyr9r:r[s r=rUrUrfr@cyr9r:r[s r=rUrUs r@c |r"dtt|}t|t|||d||tj S)aConvert `native_object` to Narwhals Dataframe, Lazyframe, or Series. Arguments: native_object: Raw object from user. Depending on the other arguments, input object can be - a Dataframe / Lazyframe / Series supported by Narwhals (pandas, Polars, PyArrow, ...) - an object which implements `__narwhals_dataframe__`, `__narwhals_lazyframe__`, or `__narwhals_series__` pass_through: Determine what happens if the object can't be converted to Narwhals - `False` (default): raise an error - `True`: pass object through as-is eager_only: Whether to only allow eager objects - `False` (default): don't require `native_object` to be eager - `True`: only convert to Narwhals if `native_object` is eager series_only: Whether to only allow Series - `False` (default): don't require `native_object` to be a Series - `True`: only convert to Narwhals if `native_object` is a Series allow_series: Whether to allow Series (default is only Dataframe / Lazyframe) - `False` or `None` (default): don't convert to Narwhals if `native_object` is a Series - `True`: allow `native_object` to be a Series Returns: DataFrame, LazyFrame, Series, or original object, depending on which combination of parameters was passed. z1from_native() got an unexpected keyword argument Fr7r\eager_or_interchange_onlyrYr]version)nextiterrO_from_native_implrMAIN)rSr7r\rYr]rTrPs r=rUrU sNX A$tDzBRAUVn !"'!  r@)r7r\ct|rC|r|s d}t||S|j|jj |dSt |rX|r|s d}t||S|s|r|s d}t||S|j |jj |dSt|rC|s|s d}t||S|j|jj |dSy)Nz,Cannot only use `series_only` with dataframefulllevelz,Cannot only use `series_only` with lazyframezJCannot only use `eager_only` or `eager_or_interchange_only` with lazyframe4Please set `allow_series=True` or `series_only=True`) rrO dataframe__narwhals_dataframe__ _with_versionr lazyframe__narwhals_lazyframe__rseries__narwhals_series__)compliant_objectr7r\rprYr]rqrPs r=_translate_if_compliantrEs+./ Dn$# #   3 3 5 C CG LTZ!  ./ Dn$# # 2bn$# #   3 3 5 C CG LTZ!  +,Ln$# #~~  0 0 2 @ @ IQW   r@c X ddlm}ddlm}m} ddlm} t||| fr|s|St|| r|s|r|S|r|dur d} t| d}|r|r d} t| t||||||| x} | St|r|r2t|s'|s#d t|j} t| |S|s|rt|r|s d } t| |S|st|r|s d } t| |S|j j#|j$j'|j)St+|rt-|r)|r:|s#d t|j} t| |S|s|s d } t| |S|j j#|j$j'|j)St/|rt1|r)|r:|s#d t|j} t| |S|s|s d } t| |S|j j#|j$j'|j)St3|r|r|s d } t| |S|s|r|s d} t| |St4j6j9dkrt; d} t=| |j j?t4j6j$j'|j)StA|rW|s|r|s d} t| |S|j j#|j$j'|j)StC|rW|s|r|s d} t| |S|j j#|j$j'|j)StE|ri|j j#|} |s|s|r|sd| jFd} t| |S| j$j'|j)S||reddlm$}|s|r|s d} t| |S|tJjLur|r|Sd} t| tJjLjO||dStQj&||}|t||||||| S|sAdt|} tQjRt|x}r| d|zz } t| |S)Nr)supports_dataframe_interchanger'r*FzJInvalid parameter combination: `series_only=True` and `allow_series=False`TzUInvalid parameter combination: `eager_only=True` and `eager_or_interchange_only=True`roz#Cannot only use `series_only` with zQCannot only use `eager_only` or `eager_or_interchange_only` with polars.LazyFramerzz1Cannot only use `series_only` with dask DataFramezOCannot only use `eager_only` or `eager_or_interchange_only` with dask DataFrame)i zPlease install dask-exprzNCannot only use `series_only=True` or `eager_only=False` with DuckDBPyRelationzHCannot only use `series_only=True` or `eager_only=False` with ibis.TablezPCannot only use `series_only`, `eager_only` or `eager_or_interchange_only` with z DataFrame)InterchangeFramezhCannot only use `series_only=True` or `eager_only=False` with object which only implements __dataframe__a+The Dataframe Interchange Protocol is no longer supported in the main `narwhals` namespace. You may want to: - Use `narwhals.stable.v1`, where it is still supported. - See https://narwhals-dev.github.io/narwhals/backcompat - Use `pass_through=True` to pass the object through without raising. interchangerxz!Unsupported dataframe type, got: z )*narwhals._interchange.dataframerrGr(r)rHr+rI ValueErrorrrr$rN __qualname__rOr# namespacefrom_native_object compliantrU to_narwhalsrr"rr&rrDASK_backend_versionr ImportError from_backendrr rimplementationrrV1r{r _show_suggestions)rSr7r\rprYr]rqrr(r)r+rP translatedns_sparkrrhints r=rtrttsIO7&-)Y!78-(k\ 5 ^CS/ ! /eo. %!&?#%    & / >;D+>+> ? Y{{=1 [] -( fn$    0 0 ? Y{{=1 [] ]# `n$    0 0 ? Y{{=1 [] M*$$77 F *(A$334J@ n$ !!--m<HHJJ&m4D F n$ '** $$$Y C. zz##$4]$C=#YY**='B#& %!&?#%   1$}2E1FG,,T--@A A4 A 6D= Cn r@c|s d}t||Dchc] }t|}}t|dk7rd|d}t||jScc}w)aGet native namespace from object. Arguments: obj: Dataframe, Lazyframe, or Series. Multiple objects can be passed positionally, in which case they must all have the same native namespace (else an error is raised). Returns: Native module. Examples: >>> import polars as pl >>> import pandas as pd >>> import narwhals as nw >>> df = nw.from_native(pd.DataFrame({"a": [1, 2, 3]})) >>> nw.get_native_namespace(df) >>> df = nw.from_native(pl.DataFrame({"a": [1, 2, 3]})) >>> nw.get_native_namespace(df) z=At least one object must be passed to `get_native_namespace`.rz0Found objects with different native namespaces: rF)r _get_native_namespace_single_objlenpop)objrPxresults r=get_native_namespacerKse, Mo;> ?a.q1 ?F ? 6{a@Jo ::< @sAct|r|jStjjj |j jSr9)r__native_namespace__rrurrrto_native_namespace)rs r=rrksIC '')) << ! ! 4 4  n((*+r@Tc4dfd }||S||S)aDecorate function so it becomes dataframe-agnostic. This will try to convert any dataframe/series-like object into the Narwhals respective DataFrame/Series, while leaving the other parameters as they are. Similarly, if the output of the function is a Narwhals DataFrame or Series, it will be converted back to the original dataframe/series type, while if the output is another type it will be left as is. By setting `pass_through=False`, then every input and every output will be required to be a dataframe/series-like object. Arguments: func: Function to wrap in a `from_native`-`to_native` block. pass_through: Determine what happens if the object can't be converted to Narwhals - `False`: raise an error - `True` (default): pass object through as-is eager_only: Whether to only allow eager objects - `False` (default): don't require `native_object` to be eager - `True`: only convert to Narwhals if `native_object` is eager series_only: Whether to only allow Series - `False` (default): don't require `native_object` to be a Series - `True`: only convert to Narwhals if `native_object` is a Series allow_series: Whether to allow Series (default is only Dataframe / Lazyframe) - `False` or `None`: don't convert to Narwhals if `native_object` is a Series - `True` (default): allow `native_object` to be a Series Returns: Decorated function. Examples: Instead of writing >>> import narwhals as nw >>> def agnostic_group_by_sum(df): ... df = nw.from_native(df, pass_through=True) ... df = df.group_by("a").agg(nw.col("b").sum()) ... return nw.to_native(df) you can just write >>> @nw.narwhalify ... def agnostic_group_by_sum(df): ... return df.group_by("a").agg(nw.col("b").sum()) c:tdfd }|S)Nc |Dcgc]}t|  }}|jDcic]\}}|t|  }}}g||jDchc]}t|ddx}r|}}t |dkDr d}t | |i|} t | Scc}wcc}}wcc}w)Nrgrrz_Found multiple backends. Make sure that all dataframe/series inputs come from the same backend.r6)rUitemsvaluesgetattrrrr>)argskwargsargnamevaluevbbackendsrPrr]r\funcr7rYs r=wrapperz.narwhalify..decorator..wrappers   !-) +!-  D &$*<<>  D%k!-) +!-  F 342&--/2 $:DAAAAH 8}q w o%4*6*FV,? ?E  sB6B;'C)rrrrreturnrr)rrr]r\r7rYs` r= decoratorznarwhalify..decorators) t# @# @ # @Jr@)rCallable[..., Any]rrr:)rr7r\rYr]rs ```` r= narwhalifyrus&p''R | T?r@ct}|t|tr|}|Stx}r[t||jrE|j dk(r6|j tz}ttj|z}|St|s t|r|j }|S|r(t||jr|j}|S|r(t||jr|j!}|St|t"r|}|St%|rd}|St'|r|j)}|Sdt+|d|}t-|)aPIf a scalar is not Python native, converts it to Python native. Arguments: scalar_like: Scalar-like value. Raises: ValueError: If the object is not convertible to a scalar. Examples: >>> import narwhals as nw >>> import pandas as pd >>> df = nw.from_native(pd.DataFrame({"a": [1, 2, 3]})) >>> nw.to_py_scalar(df["a"].item(0)) 1 >>> import pyarrow as pa >>> df = nw.from_native(pa.table({"a": [1, 2, 3]})) >>> nw.to_py_scalar(df["a"].item(0)) 1 >>> nw.to_py_scalar(1) 1 Nzdatetime64[ns]) microsecondsz/Expected object convertible to a scalar, found z. )rrINON_TEMPORAL_SCALAR_TYPESr datetime64dtypeitemrrdt timedeltar!r Timestamp to_pydatetime Timedeltato_pytimedeltaTEMPORAL_SCALAR_TYPES _is_pandas_nar%as_pyrNr) scalar_likepdscalarnpmsrPs r= to_py_scalarrso. Bj6OP8 M5{   {BMM 2   !1 1    = 0266* M)  % )D!!#& M% ; 5**," M! ; 5++- M K!6 7 M { # M ; '""$ M >d;>O=PPSo  or@cttx}xr8|jjj |xr|j |Sr9)boolrapitypes is_scalarisna)rrs r=rrs: z|#U)?)?)DUQT VVr@)rrr>r)r<DataFrame[IntoDataFrameT]r7Literal[False]rr.)r<LazyFrame[IntoLazyFrameT]r7rrr0)r<Series[IntoSeriesT]r7rrr2)r<rr7rrr)r<KDataFrame[IntoDataFrameT] | LazyFrame[IntoLazyFrameT] | Series[IntoSeriesT]r7rrz3IntoDataFrameT | IntoLazyFrameT | IntoSeriesT | Any)rSr4rTrrr4)rSr,rTrrr,)rSr3rTrrr3) rSzIntoDataFrameT | IntoSeriesTr7 Literal[True]r\rrYrr]rrz/DataFrame[IntoDataFrameT] | Series[IntoSeriesT]) rSr.r7rr\rrYrr]Nonerr) rSr5r7rr\rrYrr]rrr5) rSr.r7rr\rrYrr]rrr) rSr5r7rr\rrYrr]rrr5) rSz-IntoDataFrameT | IntoLazyFrameT | IntoSeriesTr7rr\rrYrr]rrr) rSr2r7rr\rrYrr]rrr) rSr0r7rr\rrYrr]rrr) rSr.r7rr\rrYrr]rrr) rSr.r7rr\rrYrr]rrr) rSzIntoFrame | IntoSeriesr7rr\rrYrr]rrz-DataFrame[Any] | LazyFrame[Any] | Series[Any]) rSr2r7rr\rrYrr]rrr) rSrr7rr\rrYrr] bool | Nonerr)rSzJIntoLazyFrameT | IntoDataFrameT | IntoSeriesT | IntoFrame | IntoSeries | Tr7rr\rrYrr]rrTrrzOLazyFrame[IntoLazyFrameT] | DataFrame[IntoDataFrameT] | Series[IntoSeriesT] | T)rrr7rr\rrprrYrr]rrqrrr)rSrr7rr\rrprrYrr]rrqrrr)rz,Frame | Series[Any] | IntoFrame | IntoSeriesrrr9) rzCallable[..., Any] | Noner7rr\rrYrr]rrr)rrrr)rrrr)S __future__rdatetimerdecimalr functoolsrtypingrrr r r r narwhalsr narwhals._constantsrrnarwhals._nativerrrrnarwhals._utilsrrrrrrnarwhals.dependenciesrrrrrrr r!r"r#r$r%r&rGr(r)rHr+narwhals.typingr,r-r.r/r0r1r2r3r4r5rbytesstrintfloatcomplexrdatertimerr>rUrrtrrrrr__all__r:r@r=rs"KK4  7&    CL!5#sE7GL",,8 RU.AO  RU.AO  LO(;I  F F    9 B D D J J J J #& :/: : :  :  :5: : "%"% $!$ $ $  $  $$ $ "%"%               #& $!$ $ $  $  $$ $ #&               "%"% V@V V V  V  VQV V "%         $'!$"% $!$!$ $  $  $$ $ $'!$"% $!$!$ $  $  $$ $ $'"% $!$!$ $  $  $$ $ $'!$"% 8)8!8 8  8  838 8 $'!$ !              $ $888888 8U8| ,,, , $ ,,,, ,d TTT T $ TTTT Tn@+ 5++'+d $ d #dd d  d  ddN5pW Nr@