a
    DBìj¹%  ã                   @  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r¬d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dœdd„Z!dde dfdddddddœdd„Z"de fdddddœdd „Z#de dfd!ddddd"œd#d$„Z$de dfd%ddddd"œd&d'„Z%dS )(z"
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ÚSeriesZ0123456789123456zIterator[np.ndarray]Úintznpt.NDArray[np.uint64])ÚarraysÚ	num_itemsÚreturnc           	      C  sÊ   zt | ƒ}W n" ty.   tjg tjd� Y S 0 t |g| ¡} t d¡}t |¡t d¡ }d}t| ƒD ]:\}}|| }||N }||9 }|t d| | ¡7 }|}qh|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ÚchainZ
zeros_likeÚ	enumerate)	r   r   ÚfirstZmultÚoutZlast_iÚiÚaZ	inverse_i© r'   úW/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pandas/core/util/hashing.pyÚcombine_hash_arrays/   s"    
r)   TÚutf8zIndex | DataFrame | SeriesÚboolÚstrz
str | Noner   )ÚobjÚindexÚencodingÚhash_keyÚ
categorizer   c                   s€  ddl m} ˆdu rt‰tˆtƒr8|tˆˆˆƒddd�S tˆtƒrptˆjˆˆˆ ƒj	ddd�}||ˆddd�}�ntˆt
ƒrÜtˆjˆˆˆ ƒj	ddd�}|rÈ‡ ‡‡‡fd	d
„dD ƒ}t |g|¡}	t|	dƒ}||ˆjddd�}n tˆtƒ�rj‡ ‡‡fdd
„ˆ ¡ D ƒ}
tˆjƒ}|�rL‡ ‡‡‡fdd
„dD ƒ}|d7 }t |
|¡}dd
„ |D ƒ}
t|
|ƒ}||ˆjddd�}ntdtˆƒ› �ƒ‚|S )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©r2   )r.   r   r2   c                 3  s$   | ]}t ˆjd ˆˆˆ d�jV  qdS ©F)r.   r/   r0   r1   N©Úhash_pandas_objectr.   Ú_values©Ú.0Ú_©r1   r/   r0   r-   r'   r(   Ú	<genexpr>‰   s   ùûz%hash_pandas_object.<locals>.<genexpr>©Né   c                 3  s"   | ]\}}t |jˆˆˆ ƒV  qd S r=   )Ú
hash_arrayr7   )r9   r:   Zseries)r1   r/   r0   r'   r(   r<   ™   s   ÿc                 3  s$   | ]}t ˆjd ˆˆˆ d�jV  qdS r4   r5   r8   r;   r'   r(   r<   Ÿ   s   ùûr   c                 s  s   | ]
}|V  qd S r=   r'   )r9   Úxr'   r'   r(   r<   ­   ó    zUnexpected type for hashing )Úpandasr   Ú_default_hash_keyÚ
isinstancer
   Úhash_tuplesr	   r?   r7   Úastyper   r    r!   r)   r.   r   ÚitemsÚlenÚcolumnsÚ	TypeErrorÚtype)r-   r.   r/   r0   r1   r   ÚhZserZ
index_iterr   Úhashesr   Zindex_hash_generatorÚ_hashesr'   r;   r(   r6   S   sJ    #

ÿ
ÿø

þ
ø

r6   z+MultiIndex | Iterable[tuple[Hashable, ...]])Úvalsr/   r0   r   c                   sz   t | ƒstdƒ‚ddlm‰ m} t| tƒs6| | ¡‰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   Zlevels)r9   Úlevel)rP   Úmir'   r(   Ú
<listcomp>×   s
   üþzhash_tuples.<locals>.<listcomp>c                 3  s   | ]}|j ˆ ˆd d�V  qdS )F©r/   r0   r1   N)Ú_hash_pandas_object)r9   Úcat)r/   r0   r'   r(   r<   à   s   ÿzhash_tuples.<locals>.<genexpr>)r   rJ   rB   rP   r   rD   r
   Úfrom_tuplesÚrangeZnlevelsr)   rH   )rO   r/   r0   r   Zcat_valsrM   rL   r'   )rP   r/   r0   rW   r(   rE   ·   s    
û	þrE   r   )rO   r/   r0   r1   r   c                 C  s\   t | dƒstdƒ‚t| tƒr,| j|||d�S t| tjƒsNt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-likerY   z6hash_array requires np.ndarray or ExtensionArray, not z!. Use hash_pandas_object instead.)
ÚhasattrrJ   rD   r   rZ   r   ZndarrayrK   Ú__name__Ú_hash_ndarray)rO   r/   r0   r1   r'   r'   r(   r?   é   s    

ÿÿÿr?   z
np.ndarrayc                 C  sŠ  | j }t |tj¡r@t| j|||ƒ}t| j|||ƒ}|d|  S |tkrT|  d¡} nòt	|j
tjtjfƒr~|  d¡jddd�} nÈt	|j
tjƒr²|jdkr²|  d| j j› �¡ d¡} n”|�rdd	lm}m}m}	 |	| dd
�\}
}t||ƒdd�}| |
|¡}|j||dd�S zt| ||ƒ} W n, t�yD   t|  t¡ t¡||ƒ} Y n0 | | d? N } | t d¡9 } | | d? N } | t d¡9 } | | d? N } | S )z!
    See hash_array.__doc__.
    é   Úu8Úi8Fr3   é   Úur   )rP   r   Ú	factorize)ÚsortrQ   rY   é   l   ¹eÉ9´Âz é   l   ëb&ì&‚&	 é   )r   r   Z
issubdtypeZ
complex128r`   ÚrealÚimagr+   rF   Ú
issubclassrK   Z
datetime64Ztimedelta64ÚviewÚnumberÚitemsizerB   rP   r   rf   r   rT   rZ   r   rJ   r,   Úobjectr   )rO   r/   r0   r1   r   Z	hash_realZ	hash_imagrP   r   rf   rU   rR   r[   r'   r'   r(   r`     s>    	ÿÿ
r`   )&Ú__doc__Ú
__future__r   r    Útypingr   Únumpyr   Zpandas._libs.hashingr   Zpandas.core.dtypes.commonr   Zpandas.core.dtypes.dtypesr   Zpandas.core.dtypes.genericr   r   r	   r
   r   Úcollections.abcr   r   r   Zpandas._typingr   r   rB   r   r   r   r   rC   r)   r6   rE   r?   r`   r'   r'   r'   r(   Ú<module>   s<   	&ûfý4ü3ü