o
    $&]iQ                     @  sp  U d dl mZ d dlmZ d dlmZmZmZ d dlZ	d dl
mZ d dlmZ d dlmZmZmZ erd dlmZmZmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& d dl'm(Z( d dl)m*Z* d dl+m,Z,m-Z- e"Z.de/d< dddZ0dddZ1dddZ2dddZ3dd!d"Z4dd$d%Z5ddd&dd-d.Z6nd d/l
m6Z6 d d0l7m5Z5m1Z1m4Z4m3Z3m2Z2m0Z0 ej89 Z:	 d1d2d3d4d5d6d7d8d9d:d;
Z;d<e/d=< e	j<Z=	 dd?d@Z>ddCdDZ?	dddIdJZ@ddPdQZA	dddSdTZBddVdWZCdd\d]ZDed^d_dd`daZEejFjGZGi eGjHe	I eGjJe	K eGjLe	M eGjNe	O eGjPe	Q eGjRe	Se	T e	O eGjUe	V eGjWe	XdbeGjYe	Z eGj[e	\ eGj]e	^ eGj_e	` eGjae	b eGjce	d eGjee	T eGjfe	g Zhdce/dd< eGjieGjjfZkddfdgZlddldmZmddqdrZnddxdyZodzZpd{Zqd|Zrd}Zsd~ZtdZudZvep eq der dev dZwdZxdZydZzdZ{e{dfexdfeydfezdffZ|esdfetdfeudffZ}ddd&dddZ~dddZdddZdddZdddZdddZe:dkr
ddiddiddidZde/d< nddiddiddidZ	ddddZG dd ded ZdddZdddZdddńZdS )    )annotations)	lru_cache)TYPE_CHECKINGAnycastN)EagerSeriesNamespace)ImplementationVersionisinstance_or_issubclass)IterableIteratorMapping)Literal)	TypeAliasTypeIsArrowSeries)	ArrayAnyArrayOrScalarArrayOrScalarT1ArrayOrScalarT2ChunkedArrayAny
IncompleteNativeIntervalUnitPromoteOptions	ScalarAny)IntervalUnit)DType)	IntoDTypePythonLiteralr   ChunkedArrayStructArraytr   return"TypeIs[pa.TimestampType[Any, Any]]c                 C     d S N r!   r&   r&   S/var/www/html/IGF-ODF-V3/venv/lib/python3.10/site-packages/narwhals/_arrow/utils.pyis_timestamp&       r)   TypeIs[pa.DurationType[Any]]c                 C  r$   r%   r&   r'   r&   r&   r(   is_duration'   r*   r,   TypeIs[pa.ListType[Any]]c                 C  r$   r%   r&   r'   r&   r&   r(   is_list(   r*   r.   TypeIs[pa.LargeListType[Any]]c                 C  r$   r%   r&   r'   r&   r&   r(   is_large_list)   r*   r0   &TypeIs[pa.FixedSizeListType[Any, Any]]c                 C  r$   r%   r&   r'   r&   r&   r(   is_fixed_size_list*   r*   r2   (TypeIs[pa.DictionaryType[Any, Any, Any]]c                 C  r$   r%   r&   r'   r&   r&   r(   is_dictionary+   r*   r4   optionsmemory_poolpatternstrstringsr   r6   r7   c               C  r$   r%   r&   )r:   r8   r6   r7   r&   r&   r(   extract_regex,   s   r;   )r;   )r4   r,   r2   r0   r.   r)   yearquartermonthdayhourminutesecondmillisecondmicrosecond
nanosecond)
yqmodhmsmsusnsz)Mapping[IntervalUnit, NativeIntervalUnit]
UNITS_DICTvaluec                C  s   ddl m} || ddS )Nr   )maybe_extract_py_scalarT)return_py_scalar)narwhals._arrow.seriesrR   )rQ   rR   r&   r&   r(   extract_py_scalarS   s   rU   objTypeIs[ArrayOrScalar]c                 C  s   t | tjtjtjfS )z-Return True for any base `pyarrow` container.)
isinstancepaChunkedArrayArrayScalar)rV   r&   r&   r(   is_array_or_scalarY   s   r]   arr#ArrayOrScalar | list[Iterable[Any]]dtypepa.DataType | Nonec                C  s4   t | tjr| S t | trt| |S t| g|S r%   )rX   rY   rZ   listchunked_array)r^   r`   r&   r&   r(   rc   ^   s
   
rc   nintseriesr   r   c                 C  s   t | |jjS )z|Create a strongly-typed Array instance with all elements null.

    Uses the type of `series`, without upseting `mypy`.
    )rY   nullsnativetype)rd   rf   r&   r&   r(   
nulls_likeh   s   rj   PythonLiteral | ScalarAnyc                C  s   t }t|| |d|S )zCreate an Array instance whose slots are the given scalar.

    *Optionally*, casting to `dtype` **before** repeating `n` times.
    ri   )litrY   repeat)rQ   rd   r`   lit_r&   r&   r(   rn   p   s   rn   pa.Int64Arrayc                C  s   t d| S )Nr   )rY   rn   )rd   r&   r&   r(   zeros{   s   rq   pa.DataTypeversionr	   r   c                 C  s    t | tjr|j S t| |S r%   )rX   rY   ExtensionTypedtypesUnknown&native_non_extension_to_narwhals_dtyper`   rs   r&   r&   r(   native_to_narwhals_dtype   s   

ry      )maxsizec                   s4  j tj r S tj r S tj r! S tj	 r+
 S tj r5 S tj r? S tj rI S tj rS S tj r] S tj rg S tj rq S tj stj sttjddd  r S tj r S t rj  j! j"dS t# rj$ j!dS tj% r& S tj' rψ( fddt) j*D S t+ st, r-t. j/S t0 r1t. j/ j2S tj3 r4 S tj5 stj6 r7 S tj8 r9 S : S )Nis_string_viewc                 S  s   dS )NFr&   )_r&   r&   r(   <lambda>   r*   z8native_non_extension_to_narwhals_dtype.<locals>.<lambda>)	time_unit	time_zone)r   c              	     s.   g | ]}  |jt |jqS r&   )Fieldfieldnamery   ri   ).0ir`   ru   rs   r&   r(   
<listcomp>   s    
z:native_non_extension_to_narwhals_dtype.<locals>.<listcomp>);ru   rY   typesis_int64Int64is_int32Int32is_int16Int16is_int8Int8	is_uint64UInt64	is_uint32UInt32	is_uint16UInt16is_uint8UInt8
is_booleanBoolean
is_float64Float64
is_float32Float32	is_stringis_large_stringgetattrString	is_date32Dater)   Datetimeunittzr,   Durationr4   Categorical	is_structStructrange
num_fieldsr.   r0   Listry   
value_typer2   r[   	list_size
is_decimalDecimal	is_time32	is_time64Time	is_binaryBinaryrv   rx   r&   r   r(   rw      sr   

	rw   rO   z!Mapping[type[DType], pa.DataType]NW_TO_PA_DTYPESr   c           
        s   j }|  }t| }r|S t| |jr)| j}| j }r$t	||S t	|S t| |j
r5t| jS t| |jrFtjt| j ddS t| |jrYt fdd| jD S t| |jrpt| j d}| j}tj||dS t|trd|j d}	t|	d|  }	t|	)	Nrs   )r   c                   s    g | ]}|j t|j d fqS )r   )r   narwhals_to_native_dtyper`   )r   r   r   r&   r(   r      s    z,narwhals_to_native_dtype.<locals>.<listcomp>)r   zConverting to z$ dtype is not supported for PyArrow.zUnknown dtype: )ru   	base_typer   getr
   r   r   r   rY   	timestampr   durationr   list_r   innerr   structfieldsr[   size
issubclassUNSUPPORTED_DTYPES__name__NotImplementedErrorAssertionError)
r`   rs   ru   r   pa_typer   r   r   r   msgr&   r   r(   r      s4    


r   lhsrhs'ArrowSeries | PythonLiteral | ScalarAny3tuple[ChunkedArrayAny, ChunkedArrayAny | ScalarAny]c                 C  s   ddl m} |du r| jtd| jdfS t||r8| jr'|js'| jd |jfS |jr2| j|jd fS | j|jfS t|trCd}t|| jt|t	j
rN|fS t|fS )aJ  Extract native objects in binary  operation.

    If the comparison isn't supported, return `NotImplemented` so that the
    "right-hand-side" operation (e.g. `__radd__`) can be tried.

    If one of the two sides has a `_broadcast` flag, then extract the scalar
    underneath it so that PyArrow can do its own broadcasting.
    r   r   Nrl   z$Expected Series or scalar, got list.)rT   r   rh   rm   _typerX   
_broadcastrb   	TypeErrorrY   r\   )r   r   r   r   r&   r&   r(   extract_native   s   

 r   leftr   rightc          
   	   C  s  t |td|jd}t ||td|jd}td | jd}tj| jrxtj|jrxt |t | ||}tj	|jrnt
d|j}t t ||| }t t | |td|}t t ||t |td||}	n|}	|	
| j}	|	S t |t | ||}t |}	|	S )Nr   rl      zpa._lib.Int64Type)pc	not_equalrm   ri   if_elserY   r   
is_integerdivide_checkedis_signed_integerr   multiplylessbit_wise_xorand_subtractdividefloor)
r   r   	safe_mask
safe_rightnon_safe_resultdivideddiv_typehas_remainderhas_one_negative_operandresultr&   r&   r(   floordiv_compat  s0   

r   arrow_arrayr   	pa_objectr   'tuple[ArrayOrScalarT1, ArrayOrScalarT2]c                 C  sH   t j| jr t j|jr | jt  dd|jt  ddfS | |fS )NF)safe)rY   r   r   ri   r   float64)r   r   r&   r&   r(   cast_for_truediv<  s
   r   z/(?P<date>\d{1,4}[-/.]\d{1,2}[-/.]\d{1,4}|\d{8})z(?P<sep>\s|T)z'(?P<time>\d{2}:\d{2}(?::\d{2})?|\d{6}?)z^(?P<hms>\d{2}:\d{2}:\d{2})$z^(?P<hm>\d{2}:\d{2})$z^(?P<hms_no_sep>\d{6})$z(?P<tz>Z|[+-]\d{2}:?\d{2})?z?$zw^(?P<year>(?:[12][0-9])?[0-9]{2})(?P<sep1>[-/.])(?P<month>0[1-9]|1[0-2])(?P<sep2>[-/.])(?P<day>0[1-9]|[12][0-9]|3[01])$zw^(?P<day>0[1-9]|[12][0-9]|3[01])(?P<sep1>[-/.])(?P<month>0[1-9]|1[0-2])(?P<sep2>[-/.])(?P<year>(?:[12][0-9])?[0-9]{2})$zw^(?P<month>0[1-9]|1[0-2])(?P<sep1>[-/.])(?P<day>0[1-9]|[12][0-9]|3[01])(?P<sep2>[-/.])(?P<year>(?:[12][0-9])?[0-9]{2})$zY^(?P<year>(?:[12][0-9])?[0-9]{2})(?P<month>0[1-9]|1[0-2])(?P<day>0[1-9]|[12][0-9]|3[01])$%Y%m%dz%Y-%m-%dz%d-%m-%Yz%m-%d-%Yz%H:%M:%Sz%H:%Mz%H%M%Spa.StructArrayc               C  s"   t t| |||dj}td|S )Nr5   r   )rY   concat_arraysr;   chunksr   )r:   r8   r6   r7   rr&   r&   r(   _extract_regex_concat_arraysf  s   
r   c           	      C  s   t |  ddtd}t|  sd}t||	d}|	d}t
t| dkr7d}t|t
t| dkrId	}t|ttd
|	d}ttd
|	d}|d  }|d  rkdnd}| | | | S )z.Try to infer datetime format from StringArray.r   
   r8   zUnable to infer datetime format, provided format is not supported. Please report a bug to https://github.com/narwhals-dev/narwhals/issuessepr   r   z@Found multiple separator values while inferring datetime format.z?Found multiple timezone values while inferring datetime format.pc.StringArraydatetimez%z )r   	drop_nullsliceFULL_REr   allis_validas_pyr   r   countunique
ValueError_parse_date_formatr   _parse_time_format)	r^   matchesr   
separatorsr   
date_value
time_value	sep_valuetz_valuer&   r&   r(   parse_datetime_formatt  s$   

r  r   c                 C  s   t D ]]\}}tj| |d}|dkrt|  r|  S t|  r_tt|d } dkr_tt|d } dkr_|d   }|d  kr_|	d|  S qd}t
|)	Nr   r   sep1r   sep2r   -zgUnable to infer datetime format. Please report a bug to https://github.com/narwhals-dev/narwhals/issues)DATE_FORMATSr   r;   r  r  r  r  r  r   replacer  )r^   date_rgxdate_fmtr  r  r  date_sep_valuer   r&   r&   r(   r	    s   ""r	  c                 C  s:   t D ]\}}tj| |d}t|  r|  S qdS )Nr   r   )TIME_FORMATSr   r;   r  r  r  )r^   time_rgxtime_fmtr  r&   r&   r(   r
    s   r
  window_sizecenterbooltuple[ArrowSeries, int]c                C  s`   |s| dfS |d }||d dk }| j j}t|| g|t|| R }| t||| fS )au  Pad series with None values on the left and/or right side, depending on the specified parameters.

    Arguments:
        series: The input ArrowSeries to be padded.
        window_size: The desired size of the window.
        center: Specifies whether to center the padding or not.

    Returns:
        A tuple containing the padded ArrowSeries and the offset value.
    r      )rh   r   rj   _with_nativerY   r   )rf   r  r  offset_leftoffset_rightr   arraysr&   r&   r(   
pad_series  s   r&  chunked_arraysChunkedArrayAny | ScalarAny	separator7tuple[Iterator[ChunkedArrayAny | ScalarAny], ScalarAny]c                   s>   t dd |D st nt   fdd|D t|  fS )Nc                 s  s    | ]
}t j|jV  qd S r%   )rY   r   r   ri   r   car&   r&   r(   	<genexpr>  s    z2cast_to_comparable_string_types.<locals>.<genexpr>c                 3  s    | ]}|  V  qd S r%   )r   r+  r`   r&   r(   r-    s    )anyrY   stringlarge_stringrm   )r)  r'  r&   r.  r(   cast_to_comparable_string_types  s
   
r2  )   promote_optionsdefault
permissivenone)r5  r6  r7  z*Mapping[PromoteOptions, Mapping[str, Any]]_PROMOTEpromoteTFtablesIterable[pa.Table]r   pa.Tablec                 C  s   t j| fi t| S r%   )rY   concat_tablesr8  )r:  r4  r&   r&   r(   r=    s   r=  c                   @  s   e Zd ZdS )ArrowSeriesNamespaceN)r   
__module____qualname__r&   r&   r&   r(   r>    s    r>  )r   r   startendstepc                 C  s2   t dk rdd l}t|| ||S t| ||S )N)   r   )BACKEND_VERSIONnumpyrY   arrayarange)rA  rB  rC  npr&   r&   r(   rH    s   rH  rG  func:Literal['min', 'max', 'mean', 'approximate_median', 'sum']c                 C  s   t }|dkrd|tjddfnd|f}ttjjt| t| gddgd	d
|gdd| }ttt| t d}|dkrTt| d d}nt|d |jdt| }tt||d	|gS )
Nsumvaluesr   )	min_countoffsetsnamesvalues_rl   F)rm   r   ScalarAggregateOptionsrY   rG  Tablefrom_arrayslist_flattenlist_parent_indicesgroup_by	aggregatesort_bycolumnr   list_value_lengthr   is_nullrn   ri   lenrc   replace_with_mask	fill_null)rG  rJ  ro   aggregationaggnon_empty_mask
base_arrayr&   r&   r(   list_agg  s6   
re  
descending
nulls_lastc                C  s*  |rdnd}|r
dnd}d\}}t t | td}tjjtdt| dd| g||gd	}|	|}	|	t 
t |td
}
tjjt | t | g||gd	}t j||df||fg|d}|	| j}tj|t|||}tjj|	||g||gd	}t||
g||S )Nrf  	ascendingat_endat_start)idxrM  r   r   )rA  rB  rC  rP  T)	sort_keysnull_placement)r   greaterr\  rm   rY   rT  rU  rH  r^  filterr`  invertrV  rW  sort_indicesr[  combine_chunksrO  	ListArrayrG  taker=  rZ  )rG  rf  rg  sort_directionnulls_positionrk  vis_not_sortedindexednot_sorted_partpass_throughexplodedsorted_indicesrO  sorted_implodedimploded_by_idxr&   r&   r(   	list_sort  s4   

r  )r!   r   r"   r#   )r!   r   r"   r+   )r!   r   r"   r-   )r!   r   r"   r/   )r!   r   r"   r1   )r!   r   r"   r3   )
r8   r9   r:   r   r6   r   r7   r   r"   r    )rQ   r   r"   r   )rV   r   r"   rW   r%   )r^   r_   r`   ra   r"   r   )rd   re   rf   r   r"   r   )r`   ra   rQ   rk   rd   re   r"   r   )rd   re   r"   rp   )r`   rr   rs   r	   r"   r   )r`   r   rs   r	   r"   rr   )r   r   r   r   r"   r   )r   r   r   r   r"   r   )r   r   r   r   r"   r   )
r8   r9   r:   r   r6   r   r7   r   r"   r   )r^   r   r"   r9   )r^   r   r"   r9   )rf   r   r  re   r  r  r"   r   )r'  r(  r)  r9   r"   r*  )r7  )r:  r;  r4  r   r"   r<  )rA  re   rB  re   rC  re   r"   r   )rG  r   rJ  rK  r"   r   )rG  r   rf  r  rg  r  r"   r   )
__future__r   	functoolsr   typingr   r   r   pyarrowrY   pyarrow.computecomputer   narwhals._compliantr   narwhals._utilsr   r	   r
   collections.abcr   r   r   r   typing_extensionsr   r   rT   r   narwhals._arrow.typingr   r   r   r   r   r   r   r   r   narwhals._durationr   narwhals.dtypesr   narwhals.typingr   r   r    __annotations__r)   r,   r.   r0   r2   r4   r;   pyarrow.typesPYARROW_backend_versionrE  rP   scalarrm   rU   r]   rc   rj   rn   rq   ry   rw   MAINru   r   r   r   float32r   binaryr   r0  r   bool_r   
dictionaryuint32r   date32r   time64r   int8r   int16r   int32r   int64r   uint8r   uint16r   r   uint64r   r   Objectr   r   r   r   r   DATE_RESEP_RETIME_REHMS_REHM_REHMS_RE_NO_SEPTZ_REr  YMD_REDMY_REMDY_REYMD_RE_NO_SEPr  r  r   r  r	  r
  r&  r2  r8  r=  r>  rH  re  r  r&   r&   r&   r(   <module>   s   ,





	 
	



	

A






	











#







	%