o
    h\9                  	   @  s:  U d dl mZ d dlZd dlZd dlZd dlZd dlmZmZ d dl	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 d dlm  mZ d dlmZ d dlmZ d	d
lmZ d	dl m!Z!m"Z"m#Z#m$Z$m%Z% d	dl&m'Z' ej(dkr|d dlm)Z)m*Z* nd dl+m)Z)m*Z* ej(dkrd dlm,Z,m-Z- nd dl+m,Z,m-Z- erej(dkrd dlm.Z. nd dl+m.Z. ej(dkrd dlm/Z/ nd dl+m/Z/ d dl0Z1d dl2Z3e*G dd de)Z4ee5eef ee4e!f Z6de7d< ede6dZ8ededZ9e5e:ee:e5eef e;e5eef  f f Z<de7d< e5e:ee5eef e;e5eef  f f Z=de7d< eee5e:ef df Z>dsd d!Z?e-d"Z@ed#e<eZAee,e6e@f eAf ZBG d$d% d%e'eBeAf ZCG d&d' d'eDZEedtdud.d/ZFedvdwd0d/ZF	1dxdyd4d/ZFe	(dzd{d8d9ZGe	(dtd|d:d9ZGe	(dtd}d<d9ZG			d~dd>d9ZGed? ZHG d@dA dAeZIG dBdC dCeZJe	(	(	(	(	(dddIdJZKe	(	(	(	(dddKdJZK		L	M	N	OdddQdJZKe	(	(	(	(	(dddRdSZLe	(	(	(	(dddUdSZL		L	V	N	OdddXdSZLddZd[ZMdd]d^ZNdd_d`ZOddadbZPddddeZQddhdiZRddjdkZSddldmZTddqdrZUdS )    )annotationsN)MutableMappingSequence)partial)Path)TYPE_CHECKINGAnyCallableLiteral	TypedDictTypeVarUnionoverload)is_pandas_dataframe)IntoDataFrame   )import_pyarrow_interchange)DataFrameLikesanitize_geo_interfacesanitize_narwhals_dataframesanitize_pandas_dataframeto_eager_narwhals_dataframe)PluginRegistry)      )Protocolruntime_checkable)r   
   )Concatenate	ParamSpec)TypeIs)	TypeAliasc                   @     e Zd ZU ded< dS )SupportsGeoInterfacer   __geo_interface__N__name__
__module____qualname____annotations__ r*   r*   E/var/www/vscode/kcb/lib/python3.10/site-packages/altair/utils/data.pyr#   :   s   
 r#   r!   DataType	TDataType)boundTIntoDataFrameVegaLiteDataDictToValuesReturnTypeobjr   returnTypeIs[DataType]c                 C  s&   t | ttfpt tj| dddtjS )NT)eager_or_interchange_onlypass_through)
isinstancedictr#   nwfrom_native	DataFrame)r2   r*   r*   r+   is_data_typeM   s   r<   PRc                   @  s2   e Zd ZddiZedddZejdd	dZd
S )DataTransformerRegistryconsolidate_datasetsTr3   boolc                 C  s
   | j d S Nr@   _global_settings)selfr*   r*   r+   r@   i   s   
z,DataTransformerRegistry.consolidate_datasetsvalueNonec                 C  s   || j d< d S rB   rC   )rE   rF   r*   r*   r+   r@   m   s   N)r3   rA   )rF   rA   r3   rG   )r&   r'   r(   rD   propertyr@   setterr*   r*   r*   r+   r?   f   s    r?   c                   @  s   e Zd ZdZdS )MaxRowsErrorz+Raised when a data model has too many rows.N)r&   r'   r(   __doc__r*   r*   r*   r+   rJ   s   s    rJ   .datarG   max_rows
int | Noner   c                 C     d S Nr*   rL   rM   r*   r*   r+   
limit_rowsw      rR   c                 C  rO   rP   r*   rQ   r*   r*   r+   rR   y   rS     DataType | Nonepartial | DataTypec                   s   | du r
t t dS t|   fdd}t| tr*| jd dkr&| jd }n| j}nt| tr:d| v r8| d }n| S t| } | } durMt| krM|  | S )	z
    Raise MaxRowsError if the data model has more than max_rows.

    If max_rows is None, then do not perform any check.
    NrM   c                    s   d  d} t | )NzHThe number of rows in your dataset is greater than the maximum allowed (aG  ).

Try enabling the VegaFusion data transformer which raises this limit by pre-evaluating data
transformations in Python.
    >> import altair as alt
    >> alt.data_transformers.enable("vegafusion")

Or, see https://altair-viz.github.io/user_guide/large_datasets.html for additional information
on how to plot large datasets.)rJ   )msgrW   r*   r+   raise_max_rows_error   s
   z(limit_rows.<locals>.raise_max_rows_errortypeFeatureCollectionfeaturesvalues)	r   rR   check_data_typer7   r#   r$   r8   r   len)rL   rM   rY   r]   r*   rW   r+   rR   {   s"   


nfracfloat | Nonec                 C  rO   rP   r*   rL   r`   ra   r*   r*   r+   sample      rd   c                 C  rO   rP   r*   rc   r*   r*   r+   rd      re   SampleReturnTypec                 C  rO   rP   r*   rc   r*   r*   r+   rd      re   partial | SampleReturnTypec                 C  s   | du rt t||dS t|  t| r| j||dS t| trGd| v rE| d }|s;|du r3d}t|t|t| }t	||}d|iS dS t
j| dd} |sb|du rZd}t|t|t|  }t	tt| |}| |  S )zBReduce the size of the data model by sampling without replacement.N)r`   ra   r]   z9frac cannot be None if n is None and data is a dictionaryT
eager_onlyz:frac cannot be None if n is None with this data input type)r   rd   r^   r   r7   r8   
ValueErrorintr_   randomr9   r:   range	to_native)rL   r`   ra   r]   rX   indicesr*   r*   r+   rd      s0   
)csvjsonc                   @  r"   )_FormatDict_FormatTyperZ   Nr%   r*   r*   r*   r+   rr      s   
 rr   c                   @  s   e Zd ZU ded< ded< dS )_ToFormatReturnUrlDictstrurlrr   formatNr%   r*   r*   r*   r+   rt      s   
 rt   prefixru   	extensionfilenameurlpathc                 C  rO   rP   r*   rL   rx   ry   rz   r{   r*   r*   r+   to_json      r}   c                 C  rO   rP   r*   r|   r*   r*   r+   r}      r~   altair-datarq   {prefix}-{hash}.{extension}  partial | _ToFormatReturnUrlDictc                 C  L   t ||||}| du rttfi |S t| }t|fi |dtddiS )zGWrite the data model to a .json file and return a url based data model.Nrw   rq   rZ   )_to_text_kwdsr   r}   _data_to_json_string_to_textrr   rL   rx   ry   rz   r{   kwdsdata_strr*   r*   r+   r}      
   c                 C  rO   rP   r*   r|   r*   r*   r+   to_csv  r~   r   #dict | pd.DataFrame | DataFrameLikec                 C  rO   rP   r*   r|   r*   r*   r+   r     r~   rp   *dict | pd.DataFrame | DataFrameLike | Nonec                 C  r   )zFWrite the data model to a .csv file and return a url based data model.Nrw   rp   r   )r   r   r   _data_to_csv_stringr   rr   r   r*   r*   r+   r     r   rw   c                 C  sF   t | }|j|||d}t|j| dd tt||}t||dS )N)rx   hashry   zutf-8)encoding)rv   rw   )_compute_data_hashrw   r   
write_textru   rt   )rL   rx   ry   rz   r{   rw   	data_hashrv   r*   r*   r+   r   ,  s
   r   dict[str, str]c                C  s   | |||dS )Nrx   ry   rz   r{   r*   r   r*   r*   r+   r   ;  s   r   c                 C  s   t |  tj| dd}t|trdt|iS t|r&t|}d|jddiS t|t	r7d|vr5d}t
||S t| tjrIt| } d| jddiS dt|  }t|)	z0Replace a DataFrame by a data model with values.T)r6   r]   records)orient.values expected in data dict, but not present.namedzUnrecognized data type: )r^   r9   rn   r7   r#   _from_geo_interfacer   r   to_dictr8   KeyErrorr;   r   rowsrZ   rj   )rL   data_nativerX   r*   r*   r+   	to_values?  s"   

r   c                 C  s"   t | sdt|  }t|d S )Nz@Expected dict, DataFrame or a __geo_interface__ attribute, got: )r<   rZ   	TypeError)rL   rX   r*   r*   r+   r^   W  s   r^   r   c                 C  s   t |   d d S )N    )hashlibsha256encode	hexdigest)r   r*   r*   r+   r   `  s   r   SupportsGeoInterface | Anydict[str, Any]c                 C  s   t | rt| } t| jS )z
    Santize a ``__geo_interface__`` w/ pre-santize step for ``pandas`` if needed.

    Notes
    -----
    Split out to resolve typing issues related to:
    - Intersection types
    - ``typing.TypeGuard``
    - ``pd.DataFrame.__getattr__``
    )r   r   r   r$   )rL   r*   r*   r+   r   d  s   
r   c              
   C  s   t |  t| trtt| S t| rt| } | jdddS t| t	r7d| vr.d}t
|tj| d ddS z	tj| dd}W n tyS } zd	}t||d
}~ww t|}t|jddS )z6Return a JSON string representation of the input data.r      )r   double_precisionr]   r   T)	sort_keysrh   zBto_json only works with data expressed as a DataFrame or as a dictNr   )r^   r7   r#   rq   dumpsr   r   r   r}   r8   r   r9   r:   r   NotImplementedErrorr   r   )rL   rX   data_nwexcr*   r*   r+   r   t  s(   


r   c              
   C  s   t |  t| trdttjd}t|t| r$t| } | jddS t| t	rYd| vr3d}t
|zddl}W n tyL } zd	}t||d}~ww |j| d jddS ztj| d
d}W | S  tyx } zd}t||d}~ww )z5Return a CSV string representation of the input data.z3to_csv does not yet work with data that is of type z0.
See https://github.com/vega/altair/issues/3441F)indexr]   z-values expected in data dict, but not presentr   Nz4pandas is required to convert a dict to a CSV stringTrh   zAto_csv only works with data expressed as a DataFrame or as a dict)r^   r7   r#   rZ   r&   r   r   r   r   r8   r   pandasImportErrorr;   	from_dictr9   r:   r   	write_csv)rL   rX   pdr   r   r*   r*   r+   r     s<   



r   dfi_dfr   pa.Tablec                 C  sP   ddl }dD ]}t| |d}t|r| }t||jr|  S qt }|| S )zMConvert a DataFrame Interchange Protocol compatible object to an Arrow Table.r   N)arrowto_arrowto_arrow_table
to_pyarrow)pyarrowgetattrcallabler7   Tabler   from_dataframe)r   paconvert_method_nameconvert_methodresultpir*   r*   r+   arrow_table_from_dfi_dataframe  s   
r   )r2   r   r3   r4   )..)rL   rG   rM   rN   r3   r   ).)rL   r,   rM   rN   r3   r,   )NrT   )rL   rU   rM   rN   r3   rV   )...)rL   rG   r`   rN   ra   rb   r3   r   )rL   r/   r`   rN   ra   rb   r3   r/   )rL   r,   r`   rN   ra   rb   r3   rf   )NNN)rL   rU   r`   rN   ra   rb   r3   rg   ).....)rL   rG   rx   ru   ry   ru   rz   ru   r{   ru   r3   r   )....)rL   r,   rx   ru   ry   ru   rz   ru   r{   ru   r3   rt   )Nr   rq   r   r   )rL   rU   rx   ru   ry   ru   rz   ru   r{   ru   r3   r   )rL   r   rx   ru   ry   ru   rz   ru   r{   ru   r3   rt   )Nr   rp   r   r   )rL   r   rx   ru   ry   ru   rz   ru   r{   ru   r3   r   )rL   ru   rx   ru   ry   ru   rz   ru   r{   ru   rw   rr   r3   rt   )
rx   ru   ry   ru   rz   ru   r{   ru   r3   r   )rL   r,   r3   r1   )rL   r,   r3   rG   )r   ru   r3   ru   )rL   r   r3   r   )rL   r,   r3   ru   )r   r   r3   r   )V
__future__r   r   rq   rl   syscollections.abcr   r   	functoolsr   pathlibr   typingr   r   r	   r
   r   r   r   r   narwhals.stable.v1stablev1r9   narwhals.stable.v1.dependenciesr   narwhals.stable.v1.typingr   
_importersr   corer   r   r   r   r   plugin_registryr   version_infor   r   typing_extensionsr   r   r    r!   r   r   r   r   r#   r8   r,   r)   r-   r/   ru   listr0   r1   rf   r<   r=   r>   DataTransformerTyper?   	ExceptionrJ   rR   rd   rs   rr   rt   r}   r   r   r   r   r^   r   r   r   r   r   r*   r*   r*   r+   <module>   s    (



$0
/"	
	





	


