o
    �¨Êh¹%  ã                   @  s  d Z ddlmZ ddlZddlmZ ddl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 erVdd	lmZmZmZ dd
lmZmZ ddlmZmZmZmZ dZ d-dd„Z!dde dfd.d!d"„Z"de fd/d%d&„Z#de dfd0d(d)„Z$de dfd1d+d,„Z%dS )2z"
data hash pandas / numpy objects
é    )ÚannotationsN)ÚTYPE_CHECKING)Úhash_object_array)Úis_list_like)ÚCategoricalDtype)ÚABCDataFrameÚABCExtensionArrayÚABCIndexÚABCMultiIndexÚ	ABCSeries)ÚHashableÚIterableÚIterator)Ú	ArrayLikeÚnpt)Ú	DataFrameÚIndexÚ
MultiIndexÚSeriesÚ0123456789123456ÚarraysúIterator[np.ndarray]Ú	num_itemsÚintÚreturnúnpt.NDArray[np.uint64]c           	      C  sÊ   zt | ƒ}W n ty   tjg tjd� Y S w t |g| ¡} t d¡}t |¡t d¡ }d}t| ƒD ]\}}|| }||N }||9 }|t d| | ¡7 }|}q4|d |ks\J dƒ‚|t d¡7 }|S )	z¼
    Parameters
    ----------
    arrays : Iterator[np.ndarray]
    num_items : int

    Returns
    -------
    np.ndarray[uint64]

    Should be the same as CPython's tupleobject.c
    )ÚdtypeiCB ixV4 r   iXB é   zFed in wrong num_itemsiû| )	ÚnextÚStopIterationÚnpÚarrayÚuint64Ú	itertoolsÚchainÚ
zeros_likeÚ	enumerate)	r   r   ÚfirstÚmultÚoutÚlast_iÚiÚaÚ	inverse_i© r.   úJ/var/www/html/env/lib/python3.10/site-packages/pandas/core/util/hashing.pyÚcombine_hash_arrays/   s$   ÿ
r0   TÚutf8ÚobjúIndex | DataFrame | SeriesÚindexÚboolÚencodingÚstrÚhash_keyú
str | NoneÚ
categorizer   c                   s|  ddl m} ˆdu rt‰tˆtƒr|tˆˆˆƒddd�S tˆtƒr8tˆjˆˆˆ ƒj	ddd�}||ˆddd�}|S tˆt
ƒrotˆjˆˆˆ ƒj	ddd�}|rd‡ ‡‡‡fd	d
„dD ƒ}t |g|¡}	t|	dƒ}||ˆjddd�}|S tˆtƒrµ‡ ‡‡fdd
„ˆ ¡ D ƒ}
tˆjƒ}|r¥‡ ‡‡‡fdd
„dD ƒ}|d7 }t |
|¡}dd
„ |D ƒ}
t|
|ƒ}||ˆjddd�}|S tdtˆƒ› �ƒ‚)a>  
    Return a data hash of the Index/Series/DataFrame.

    Parameters
    ----------
    obj : Index, Series, or DataFrame
    index : bool, default True
        Include the index in the hash (if Series/DataFrame).
    encoding : str, default 'utf8'
        Encoding for data & key when strings.
    hash_key : str, default _default_hash_key
        Hash_key for string key to encode.
    categorize : bool, default True
        Whether to first categorize object arrays before hashing. This is more
        efficient when the array contains duplicate values.

    Returns
    -------
    Series of uint64, same length as the object

    Examples
    --------
    >>> pd.util.hash_pandas_object(pd.Series([1, 2, 3]))
    0    14639053686158035780
    1     3869563279212530728
    2      393322362522515241
    dtype: uint64
    r   )r   Nr"   F)r   Úcopy©r;   )r4   r   r;   c                 3  ó&   � | ]}t ˆjd ˆˆˆ d�jV  qdS ©F)r4   r6   r8   r:   N©Úhash_pandas_objectr4   Ú_values©Ú.0Ú_©r:   r6   r8   r2   r.   r/   Ú	<genexpr>‰   ó   € ùû
ùz%hash_pandas_object.<locals>.<genexpr>©Né   c                 3  s$   � | ]\}}t |jˆˆˆ ƒV  qd S rH   )Ú
hash_arrayrA   )rC   rD   Úseries)r:   r6   r8   r.   r/   rF   ™   s
   € ÿ
ÿc                 3  r=   r>   r?   rB   rE   r.   r/   rF   Ÿ   rG   r   c                 s  s   � | ]}|V  qd S rH   r.   )rC   Úxr.   r.   r/   rF   ­   s   € zUnexpected type for hashing )Úpandasr   Ú_default_hash_keyÚ
isinstancer
   Úhash_tuplesr	   rJ   rA   Úastyper   r#   r$   r0   r4   r   ÚitemsÚlenÚcolumnsÚ	TypeErrorÚtype)r2   r4   r6   r8   r:   r   ÚhÚserÚ
index_iterr   Úhashesr   Úindex_hash_generatorÚ_hashesr.   rE   r/   r@   S   sN   #

ÿ2
Ðÿø


äþ
ø

þr@   Úvalsú+MultiIndex | Iterable[tuple[Hashable, ...]]c                   sz   t | ƒstdƒ‚ddlm‰ m} t| tƒs| | ¡‰n| ‰‡ ‡fdd„tˆj	ƒD ƒ}‡‡fdd„|D ƒ}t
|t|ƒƒ}|S )a  
    Hash an MultiIndex / listlike-of-tuples efficiently.

    Parameters
    ----------
    vals : MultiIndex or listlike-of-tuples
    encoding : str, default 'utf8'
    hash_key : str, default _default_hash_key

    Returns
    -------
    ndarray[np.uint64] of hashed values
    z'must be convertible to a list-of-tuplesr   )ÚCategoricalr   c              	     s,   g | ]}ˆ   ˆj| tˆj| d d�¡‘qS )F©Ú
categoriesÚordered)Ú_simple_newÚcodesr   Úlevels)rC   Úlevel)r_   Úmir.   r/   Ú
<listcomp>×   s    üþÿzhash_tuples.<locals>.<listcomp>c                 3  s    � | ]}|j ˆ ˆd d�V  qdS )F©r6   r8   r:   N)Ú_hash_pandas_object)rC   Úcat)r6   r8   r.   r/   rF   à   s
   € ÿ
ÿzhash_tuples.<locals>.<genexpr>)r   rU   rM   r_   r   rO   r
   Úfrom_tuplesÚrangeÚnlevelsr0   rS   )r]   r6   r8   r   Úcat_valsrZ   rW   r.   )r_   r6   r8   rg   r/   rP   ·   s   
û	þrP   r   c                 C  s\   t | dƒs	tdƒ‚t| tƒr| j|||d�S t| tjƒs'tdt| ƒj› d�ƒ‚t	| |||ƒS )aø  
    Given a 1d array, return an array of deterministic integers.

    Parameters
    ----------
    vals : ndarray or ExtensionArray
    encoding : str, default 'utf8'
        Encoding for data & key when strings.
    hash_key : str, default _default_hash_key
        Hash_key for string key to encode.
    categorize : bool, default True
        Whether to first categorize object arrays before hashing. This is more
        efficient when the array contains duplicate values.

    Returns
    -------
    ndarray[np.uint64, ndim=1]
        Hashed values, same length as the vals.

    Examples
    --------
    >>> pd.util.hash_array(np.array([1, 2, 3]))
    array([ 6238072747940578789, 15839785061582574730,  2185194620014831856],
      dtype=uint64)
    r   zmust pass a ndarray-likeri   z6hash_array requires np.ndarray or ExtensionArray, not z!. Use hash_pandas_object instead.)
ÚhasattrrU   rO   r   rj   r    ÚndarrayrV   Ú__name__Ú_hash_ndarray)r]   r6   r8   r:   r.   r.   r/   rJ   é   s   

ÿÿÿrJ   ú
np.ndarrayc                 C  s†  | j }t |tj¡r t| j|||ƒ}t| j|||ƒ}|d|  S |tkr*|  d¡} nwt	|j
tjtjfƒr?|  d¡jddd�} nbt	|j
tjƒrY|jdkrY|  d| j j› �¡ d¡} nH|rƒdd	lm}m}m}	 |	| dd
�\}
}t||ƒdd�}| |
|¡}|j||dd�S zt| ||ƒ} W n ty    t|  t¡ t¡||ƒ} Y nw | | d? N } | t d¡9 } | | d? N } | t d¡9 } | | d? N } | S )z!
    See hash_array.__doc__.
    é   Úu8Úi8Fr<   é   Úur   )r_   r   Ú	factorize)Úsortr`   ri   é   l   ¹eÉ9´Âz é   l   ëb&ì&‚&	 é   )r   r    Ú
issubdtypeÚ
complex128rs   ÚrealÚimagr5   rQ   Ú
issubclassrV   Ú
datetime64Útimedelta64ÚviewÚnumberÚitemsizerM   r_   r   rz   r   rc   rj   r   rU   r7   Úobjectr"   )r]   r6   r8   r:   r   Ú	hash_realÚ	hash_imagr_   r   rz   rd   ra   rk   r.   r.   r/   rs     s@   	ÿÿþrs   )r   r   r   r   r   r   )r2   r3   r4   r5   r6   r7   r8   r9   r:   r5   r   r   )r]   r^   r6   r7   r8   r7   r   r   )
r]   r   r6   r7   r8   r7   r:   r5   r   r   )
r]   rt   r6   r7   r8   r7   r:   r5   r   r   )&Ú__doc__Ú
__future__r   r#   Útypingr   Únumpyr    Úpandas._libs.hashingr   Úpandas.core.dtypes.commonr   Úpandas.core.dtypes.dtypesr   Úpandas.core.dtypes.genericr   r   r	   r
   r   Úcollections.abcr   r   r   Úpandas._typingr   r   rM   r   r   r   r   rN   r0   r@   rP   rJ   rs   r.   r.   r.   r/   Ú<module>   s>    	
&ûfý4ü3ü