o
    $&]ij                     @  s  U d dl mZ d dlZd dlZd dlZd dlmZmZmZm	Z	m
Z
mZ d dlZd dlmZ d dlmZmZmZmZmZmZ d dlmZ d dlmZmZmZmZmZmZ d dl m!Z! erd d	l"m#Z#m$Z$m%Z% d d
l&m'Z' d dl(m)Z* d dl+m,Z, d dl-m.Z.m/Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl6m7Z7m8Z8m9Z9 d dl:m;Z; d dl<m=Z=m>Z>m?Z?m@Z@ e
de3dZAe?ZBdeCd< e?ZDdeCd< eeeEgef ZFdeCd< eEZGdeCd< ejHejIejJhZKdZLeMeLejNZOdZPeMePejNZQdZReMeRejNZSdZTeMeTejNZUe	d ZVdeCd < d!d"d#ZWd$d%d&d'd(d)d*d+d,d-d.
ZXd/eCd0< ejHY ZZ	 dd5d6Z[dd<d=Z\ddBdCZ]ddFdGZ^ej_dHdIddNdOZ`ddRdSZa	dddVdWZbddYdZZcd[d\dd^d_ZdejHY d`krgddcddZenddeddZeddgdhZfddldmZgej_dHdIddodpZhejijjZjejjkdqejjldriZmdseCdt< ejjndudvdwdxejjodydzd{dxejjpd|d}d~dxejjqddddxejjrddddxejjsddddxejjtddddxejjuddddxejjvddddxejjwddddxejjxddd4dxiZydeCd< ejjzfZ{dddZ|dddZ}dddZ~ejdfejdfejefejdfejefejdfejefejefejefd	ZdeCd< dddZeeeddZdeCd< dddZdddZdddZdddZG dd ded8ef ZdddƄZdS )    )annotationsN)TYPE_CHECKINGAnyCallableLiteralTypeVarcast)EagerSeriesNamespace)MS_PER_SECONDNS_PER_MICROSECONDNS_PER_MILLISECONDNS_PER_SECONDSECONDS_PER_DAYUS_PER_SECOND)issue_warning)ImplementationVersion_DeferredIterablecheck_columns_existisinstance_or_issubclassrequires)
ShapeError)IterableIteratorMapping)
ModuleType)DtypeBaseMaskedDtype)	TypeAliasTypeIs)IntervalUnit)PandasLikeExprPandasLikeSeries)NativeDataFrameTNativeNDFrameTNativeSeriesT)DType)DTypeBackend	IntoDTypeTimeUnit_1DArrayExprT)boundr   UnitCurrent
UnitTargetBinOpBroadcastIntoRhsa  ^
    datetime64\[
        (?P<time_unit>s|ms|us|ns)                 # Match time unit: s, ms, us, or ns
        (?:,                                      # Begin non-capturing group for optional timezone
            \s*                                   # Optional whitespace after comma
            (?P<time_zone>                        # Start named group for timezone
                [a-zA-Z\/]+                       # Match timezone name, e.g., UTC, America/New_York
                (?:[+-]\d{2}:\d{2})?              # Optional offset in format +HH:MM or -HH:MM
                |                                 # OR
                pytz\.FixedOffset\(\d+\)          # Match pytz.FixedOffset with integer offset in parentheses
            )                                     # End time_zone group
        )?                                        # End optional timezone group
    \]                                            # Closing bracket for datetime64
$a  ^
    timestamp\[
        (?P<time_unit>s|ms|us|ns)                 # Match time unit: s, ms, us, or ns
        (?:,                                      # Begin non-capturing group for optional timezone
            \s?tz=                                # Match "tz=" prefix
            (?P<time_zone>                        # Start named group for timezone
                [a-zA-Z\/]*                       # Match timezone name (e.g., UTC, America/New_York)
                (?:                               # Begin optional non-capturing group for offset
                    [+-]\d{2}:\d{2}               # Match offset in format +HH:MM or -HH:MM
                )?                                # End optional offset group
            )                                     # End time_zone group
        )?                                        # End optional timezone group
    \]                                            # Closing bracket for timestamp
    \[pyarrow\]                                   # Literal string "[pyarrow]"
$z^
    timedelta64\[
        (?P<time_unit>s|ms|us|ns)                 # Match time unit: s, ms, us, or ns
    \]                                            # Closing bracket for timedelta64
$a  ^
    duration\[
        (?P<time_unit>s|ms|us|ns)                 # Match time unit: s, ms, us, or ns
    \]                                            # Closing bracket for duration
    \[pyarrow\]                                   # Literal string "[pyarrow]"
$)yearquartermonthweekdayhourminutesecondmillisecondmicrosecond
nanosecondNativeIntervalUnitDmin)dmr3   r4   r5   r7   r8   r9   r:   r;   r<   r=   )
yqmorA   hrB   smsusnsz)Mapping[IntervalUnit, NativeIntervalUnit]
UNITS_DICTimplementationr   returnboolc                 C  s   | t jt jhv S N)r   PANDASMODINrL    rS   Y/var/www/html/IGF-ODF-V3/venv/lib/python3.10/site-packages/narwhals/_pandas_like/utils.pyis_pandas_or_modin      rU   lhsr$   rhsPandasLikeSeries | object.tuple[pd.Series[Any], pd.Series[Any] | object]c                 C  s   ddl m} | jj}| jrt||r|js| jjd |jfS t||rG|jr/| j|jjd fS |jj|urA| jt|j||jdfS | j|jfS t|t	rRd}t
|| j|fS )zValidate RHS of binary operation.

    If the comparison isn't supported, return `NotImplemented` so that the
    "right-hand-side" operation (e.g. `__radd__`) can be tried.
    r   r#   rR   z$Expected Series or scalar, got list.)narwhals._pandas_like.seriesr$   nativeindex
_broadcast
isinstanceiloc	set_index_implementationlist	TypeError)rW   rX   r$   	lhs_indexmsgrS   rS   rT   align_and_extract_native   s    


rg   objr&   r]   r   c                C  s   t || jr t| }t|  }kr d| d| }t||tju r0| jdd} || _| S |tj	u rKd|
   krAdk rKn n| j|dddS | j|dd	S )
z}Wrapper around pandas' set_axis to set object index.

    We can set `copy` / `inplace` based on implementation/version.
    zExpected object of length z, got length: F)deep         r   )axiscopy)ro   )r_   to_native_namespaceIndexlenr   r   CUDFrp   r]   rP   _backend_versionset_axis)rh   r]   rL   expected_len
actual_lenrf   rS   rS   rT   ra      s   



ra   argskwargsc                O  sT   |t ju r| dkr| j|i |ddi}n| j|i |ddd}td|S )zXWrapper around pandas' rename so that we can set `copy` based on implementation/version.rm   inplaceF)rp   r{   r&   )r   rP   ru   renamer   )rh   rL   ry   rz   resultrS   rS   rT   r|      s
   

r|      )maxsizenative_dtypeversionr   r(   c                 C  s  t | }|j}|dv r| S |dv r| S |dv r| S |dv r'| S |dv r/| S |dv r7| S |dv r?| S |dv rG|	 S |d	v rO|
 S |d
v rW| S |dv r_| S |dv rg| S |drp| S |dkryt| |S t| }st| }r|d}|d}|||S t| }st| }r|d}||S |dkr| S |dr|dr| S |dr|dr| S |dr|dr| S | S )N>   int64[pyarrow]Int64int64Int64[pyarrow]>   int32[pyarrow]Int32int32Int32[pyarrow]>   int16[pyarrow]Int16int16Int16[pyarrow]>   int8[pyarrow]Int8int8Int8[pyarrow]>   uint64[pyarrow]UInt64uint64UInt64[pyarrow]>   uint32[pyarrow]UInt32uint32UInt32[pyarrow]>   uint16[pyarrow]UInt16uint16UInt16[pyarrow]>   uint8[pyarrow]UInt8uint8UInt8[pyarrow]>   double[pyarrow]float64[pyarrow]Float64float64Float64[pyarrow]>   float[pyarrow]float32[pyarrow]Float32float32Float32[pyarrow]>   string[python]string[pyarrow]large_string[pyarrow]string[pyarrow_numpy]strstring>   bool[pyarrow]rN   booleanboolean[pyarrow]zdictionary<category	time_unit	time_zonezdate32[day][pyarrow]decimalz	[pyarrow]timebinary)r   dtypesr   r   r   r   r   r   r   r   r   r   StringBoolean
startswithCategorical$native_categorical_to_narwhals_dtypePATTERN_PD_DATETIMEmatchPATTERN_PA_DATETIMEgroupDatetimePATTERN_PD_DURATIONPATTERN_PA_DURATIONDurationDateendswithDecimalTimeBinaryUnknown)r   r   dtyper   match_dt_time_unitdt_time_zonedu_time_unitrS   rS   rT   #non_object_native_to_narwhals_dtype   sd   	





r   seriesPandasLikeSeries | Nonec                 C  s   |j }|tju r| S tjjj}| d u rdn|| ddd}|dkr(| S |dkr5|t	j
ur5| S |dkr=| S | S )Nemptyd   T)skipnar   )r   r   rt   r   pdapitypesinfer_dtypeheadr   V1Object)r   r   rL   r   inferinferred_dtyperS   rS   rT   object_native_to_narwhals_dtype!  s   

r   pd.CategoricalDtype#Literal[Implementation.CUDF] | Nonec                 C  sN   |j }|tju r| S | jr#|tju rt| n| jj	}|
t|S | S rO   )r   r   r   r   orderedr   rt   _cudf_categorical_to_list
categoriesto_listEnumr   )r   r   rL   r   	into_iterrS   rS   rT   r   8  s   

r   Callable[[], list[Any]]c                   s   d fdd}|S )NrM   	list[Any]c                     s    j   S rO   )r   to_arrow	to_pylistrS   r   rS   rT   fnN     z%_cudf_categorical_to_list.<locals>.fn)rM   r   rS   )r   r   rS   r   rT   r   J  s   r   F)allow_objectr   c                C  s   t | }|dr!ddlm} t| dr||  |S || j|S |dkr0| r0t| |t	j
S |dkr9t| |S |t	ju rC|j S |rKtd ||S d}t|)N)
large_listrc   structfixed_size_listr   )native_to_narwhals_dtyper   r   objectz;Unreachable code, object dtype should be handled separately)r   r   narwhals._arrow.utilsr   hasattrr   pyarrow_dtypeis_cudfr   r   rt   r   DASKr   r   r   AssertionError)r   r   rL   r   	str_dtypearrow_native_to_narwhals_dtyperf   rS   rS   rT   r   T  s&   




r   )rk      r   TypeIs[BaseMaskedDtype]c                 C  s&   t  }t| tjjjot| d|du S )z/Return `True` if `dtype` is `"numpy_nullable"`.baseN)r   r_   r   r   
extensionsExtensionDtypegetattr)r   sentinelrS   rS   rT   is_dtype_numpy_nullable|  s   r  c                 C  s*   t | tjjjrddlm} t | |S dS )Nr   r   F)r_   r   r   r  r  pandas.core.arrays.maskedr   )r   OldBaseMaskedDtyperS   rS   rT   r    s   
r)   c                 C  s*   |t ju rdS t| rdS t| rdS dS )zjGet dtype backend for pandas type.

    Matches pandas' `dtype_backend` argument in `convert_dtypes`.
    Npyarrownumpy_nullable)r   rt   is_dtype_pyarrowr  )r   rL   rS   rS   rT   get_dtype_backend  s
   
r  r   Iterable[Any]Iterator[DTypeBackend]c                   s    fdd| D S )ziYield a `DTypeBackend` per-dtype.

    Matches pandas' `dtype_backend` argument in `convert_dtypes`.
    c                 3  s    | ]}t | V  qd S rO   )r  ).0r   rR   rS   rT   	<genexpr>  s    z&iter_dtype_backends.<locals>.<genexpr>rS   )r   rL   rS   rR   rT   iter_dtype_backends  s   r  TypeIs[pd.ArrowDtype]c                 C  s   t tdo
t| tjS )N
ArrowDtype)r   r   r_   r  r   rS   rS   rT   r    s   r  r   r   zMapping[type[DType], str]NW_TO_PD_DTYPES_INVARIANTr   r   r   )r  r  Nr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   z<Mapping[type[DType], Mapping[DTypeBackend, str | type[Any]]]NW_TO_PD_DTYPES_BACKENDr*   dtype_backendstr | PandasDtypec              
   C  s  |dvrd| d}t ||j}|  }t| }r|S t| }r)|| S t||jrF|dkr>dd l}	t	
|	 S |dkrDdS tS t| |jrt|rtdk rt| |jr~| jd	kr~tt}
d
|
d}d}d| jd| d| d}t|t d	}n| j}|dkr| j }rd| nd}d| | dS | j }rd| nd}d| | dS t| |jrt|rtdk rd	}n| j}|dkrd| dS d| dS t| |jrzdd l}	W dS  ty } zd}t||d }~ww t| |jr!|tju rd}t|t| |jr| }|j | j!ddS d}t |t||j"|j#|j$|j%|j&fr7t'| ||S t|t(rKd |j) d!| d}t|d"|  }t*|)#N>   Nr  r  z;Expected one of {None, 'pyarrow', 'numpy_nullable'}, got: ''r  r   r  r   )r  rJ   z*available in 'pandas>=2.0', found version .zhttps://pandas.pydata.org/docs/dev/whatsnew/v2.0.0.html#construction-with-datetime64-or-timedelta64-dtype-with-unsupported-resolutionz`nw.Datetime(time_unit=z)` is only z
Narwhals has fallen back to using `time_unit='ns'` to avoid an error.

Hint: to avoid this warning, consider either:
- Upgrading pandas: zA
- Using a bare `nw.Datetime`, if this precision is not importantz, tz= z
timestamp[z
][pyarrow]z, zdatetime64[]z	duration[ztimedelta64[z/'pyarrow>=13.0.0' is required for `Date` dtype.zdate32[pyarrow]z9Converting to Enum is not supported in narwhals.stable.v1T)r   z9Can not cast / initialize Enum without categories presentConverting to z dtype is not supported for zUnknown dtype: )+
ValueErrorr   	base_typer  getr  
issubclassr   r  r   r  r   r   r   r   rU   PANDAS_VERSIONr_   r   r   _unparse_versionr   UserWarningr   r   r   ModuleNotFoundErrorr   r   r   NotImplementedErrorrq   CategoricalDtyper   StructArrayListr   r   narwhals_to_native_arrow_dtypeUNSUPPORTED_DTYPES__name__r   )r   r  rL   r   rf   r   r!  pd_typeinto_pd_typepafound	availablechangelog_urlr   tztz_partr   excrJ   rS   rS   rT   narwhals_to_native_dtype  s   






r9  pd.ArrowDtypec              
   C  s   t |r7tdkr7zdd l}W n ty( } zd|  d|j }t||d }~ww ddlm} t|| |S d|  d| d| d	}t	|)
N)r  r  r   zUnable to convert to z  to to the following exception: )r9  r  z+ dtype is not supported for implementation z and version r  )
rU   r$  r  ImportErrorrf   r   r9  r   r  r(  )r   rL   r   r2  r8  rf   _to_arrow_dtyperS   rS   rT   r-  E  s"   
r-  r   c                 C  s,   dt | v rdS t |  t | krdS dS )Nr  r   r   r   )r   lowerr  rS   rS   rT   int_dtype_mapperX  s
   r>  i  i@B )	)rJ   rI   )rJ   rH   )rI   rJ   )rI   rH   )rH   rJ   )rH   rI   )rG   rJ   )rG   rI   )rG   rH   zGMapping[tuple[UnitCurrent, UnitTarget], tuple[BinOpBroadcast, IntoRhs]]_TIMESTAMP_DATETIME_OP_FACTORrG   r'   currentr+   r   c                 C  sD   ||kr| S t ||f }r|\}}|| |S d| d}t|)Nzunexpected time unit zD, please report an issue at https://github.com/narwhals-dev/narwhals)r?  r"  r   )rG   r@  r   itemr   factorrf   rS   rS   rT   calculate_timestamp_datetimeo  s   

rC  rk   )rJ   rI   rH   rG   zMapping[TimeUnit, int]_TIMESTAMP_DATE_FACTORc                 C  s   | t  t|  S rO   )r   rD  )rG   r   rS   rS   rT   calculate_timestamp_date  rV   rE  dfr%   column_nameslist[str] | _1DArrayNativeDataFrameT | Anyc              
   C  s   t || jd kr| j|k r| S | jjjdks$|tju r:| dk r:t	|| j
 d }r1|| jdd|f S z| | W S  tyZ } zt	|| j
 d }rU|| d}~ww )zsSelect columns by name.

    Prefer this over `df.loc[:, column_names]` as it's
    generally more performant.
    rk   brj   )r4  N)rs   shapecolumnsallr   kindr   rP   ru   r   tolistlocKeyError)rF  rG  rL   errorerS   rS   rT   select_columns_by_name  s     


rT  c                 C  s"   | j tjtjtjhv o| jjdkS )NrN   )rb   r   rP   rQ   r   r\   r   )rG   rS   rS   rT   is_non_nullable_boolean  s
   
rU  r   c                C  sD   | t jt jhv rddl}|S | t ju rddl}|S d|  }t|)zCReturns numpy or cupy module depending on the given implementation.r   Nz!Expected pandas/modin/cudf, got: )r   rP   rQ   numpyrt   cupyr   )rL   npcprf   rS   rS   rT   import_array_module  s   

rZ  c                   @  s   e Zd ZdS )PandasLikeSeriesNamespaceN)r/  
__module____qualname__rS   rS   rS   rT   r[    s    r[  drop_null_keysdict[str, bool]c                 C  s   dd| ddS )NFT)sortas_indexdropnaobservedrS   )r^  rS   rS   rT   make_group_by_kwargs  r   rd  )rL   r   rM   rN   )rW   r$   rX   rY   rM   rZ   )rh   r&   r]   r   rL   r   rM   r&   )
rh   r&   ry   r   rL   r   rz   r   rM   r&   )r   r   r   r   rM   r(   )r   r   r   r   rL   r   rM   r(   rO   )r   r   r   r   rL   r   rM   r(   )r   r   rM   r   )
r   r   r   r   rL   r   r   rN   rM   r(   )r   r   rM   r  )r   r   rL   r   rM   r)   )r   r  rL   r   rM   r  )r   r   rM   r  )
r   r*   r  r)   rL   r   r   r   rM   r  )r   r*   rL   r   r   r   rM   r:  )r   r   rM   r   )rG   r'   r@  r+   r   r+   rM   r'   )rG   r'   r   r+   rM   r'   )rF  r%   rG  rH  rL   r   rM   rI  )rG   r$   rM   rN   )rL   r   rM   r   )r^  rN   rM   r_  )
__future__r   	functoolsoperatorretypingr   r   r   r   r   r   pandasr   narwhals._compliantr	   narwhals._constantsr
   r   r   r   r   r   narwhals._exceptionsr   narwhals._utilsr   r   r   r   r   r   narwhals.exceptionsr   collections.abcr   r   r   r   r   pandas._typingr   PandasDtypepandas.core.dtypes.dtypesr   typing_extensionsr   r    narwhals._durationr!   narwhals._pandas_like.exprr"   r[   r$   narwhals._pandas_like.typingr%   r&   r'   narwhals.dtypesr(   narwhals.typingr)   r*   r+   r,   r-   r/   __annotations__r0   intr1   r2   rP   rt   rQ   PANDAS_LIKE_IMPLEMENTATIONPD_DATETIME_RGXcompileVERBOSEr   PA_DATETIME_RGXr   PD_DURATION_RGXr   PA_DURATION_RGXr   r>   
ALIAS_DICTrK   ru   r$  rU   rg   ra   r|   	lru_cacher   r   r   r   r   r  r  r  r  MAINr   r   r   r  r   r   r   r   r   r   r   r   r   r   r   r  r   r.  r9  r-  r>  floordivmulr?  rC  rD  rE  rT  rU  rZ  r[  rd  rS   rS   rS   rT   <module>   s      




!


J
&




%

c






	