o
    $&]in+                     @  s4  U d dl mZ d dlmZ d dlmZmZ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 d dlmZ d dlmZ ergd d	lmZ d d
lmZ d dlmZmZ eee ee gdf Zde d< ddddddddd;d"d#Z!d<d%d&Z"d=d(d)Z#d>d*d+Z$d?d,d-Z%d@d4d5Z&dAd7d8Z'dBd9d:Z(dS )C    )annotations)partial)TYPE_CHECKINGAnyCallable)qualified_type_name
zip_strictis_narwhals_series)ArrayBooleanCategoricalListStringStruct)
new_series)raise_series_assertion_error)	TypeAlias)Series)IntoSeriesTSeriesTNr   CheckFnTFgh㈵>g:0yE>check_dtypescheck_namescheck_ordercheck_exactrel_tolabs_tolcategorical_as_strleftSeries[IntoSeriesT]rightr   boolr   r   r   r   floatr   r   returnNonec             	   C  s   d}	t dd | |fD r"dtt|  dtt| d}
t|
t| |||d |s@| j r7d}
t|
|  | } }t	| |\}}|sN| j
 s[t|||||||d	 dS t||||d
 dS )u"  Assert that the left and right Series are equal.

    Raises a detailed `AssertionError` if the Series differ.
    This function is intended for use in unit tests.

    Arguments:
        left: The first Series to compare.
        right: The second Series to compare.
        check_dtypes: Requires data types to match.
        check_names: Requires names to match.
        check_order: Requires elements to appear in the same order.
        check_exact: Requires float values to match exactly. If set to `False`, values are
            considered equal when within tolerance of each other (see `rel_tol` and
            `abs_tol`). Only affects columns with a Float data type.
        rel_tol: Relative tolerance for inexact checking, given as a fraction of the
            values in `right`.
        abs_tol: Absolute tolerance for inexact checking.
        categorical_as_str: Cast categorical columns to string before comparing.
            Enabling this helps compare columns that do not share the same string cache.

    Examples:
        >>> import pandas as pd
        >>> import narwhals as nw
        >>> from narwhals.testing import assert_series_equal
        >>> s1 = nw.from_native(pd.Series([1, 2, 3]), series_only=True)
        >>> s2 = nw.from_native(pd.Series([1, 5, 3]), series_only=True)
        >>> assert_series_equal(s1, s2)  # doctest: +ELLIPSIS
        Traceback (most recent call last):
        ...
        AssertionError: Series are different (exact value mismatch)
        [left]:
        ┌───────────────┐
        |Narwhals Series|
        |---------------|
        | 0    1        |
        | 1    2        |
        | 2    3        |
        | dtype: int64  |
        └───────────────┘
        [right]:
        ┌───────────────┐
        |Narwhals Series|
        |---------------|
        | 0    1        |
        | 1    5        |
        | 2    3        |
        | dtype: int64  |
        └───────────────┘
    Tc                 s  s    | ]}t | V  qd S )Nr	   ).0obj r)   ]/var/www/html/IGF-ODF-V3/venv/lib/python3.10/site-packages/narwhals/testing/asserts/series.py	<genexpr>T   s    z&assert_series_equal.<locals>.<genexpr>z4Expected `narwhals.Series` instance, found:
[left]: z

[right]: zp

Hint: Use `nw.from_native(obj, series_only=True) to convert each native object into a `narwhals.Series` first.)r   r   zA`check_order=False` is not supported (yet) with nested data type.)r   r   r   r   r   )r   r   N)anyr   type	TypeError_check_metadatadtype	is_nestedNotImplementedErrorsort_check_null_valuesis_float_check_exact_values_check_approximate_values)r    r"   r   r   r   r   r   r   r   __tracebackhide__msg	left_vals
right_valsr)   r)   r*   assert_series_equal   s6   =




r<   r   c                C  s   | j |j }}||krtd|| t| t|}}||kr$td|| | j|j}}	|r7||	kr7td||	 | j|j}
}|rL|
|krNtd|
| dS dS dS )zECheck metadata information: implementation, length, dtype, and names.zimplementation mismatchzlength mismatchzdtype mismatchzname mismatchN)implementationr   lenr0   name)r    r"   r   r   	left_impl
right_implleft_len	right_len
left_dtyperight_dtype	left_name
right_namer)   r)   r*   r/   v   s   r/   tuple[SeriesT, SeriesT]c                 C  s\   |   |  }}|  | }}||ks||k r"td|| | | || fS )z8Check null value consistency and return non-null values.znull value mismatch)
null_countis_nullr,   r   filter)r    r"   left_null_countright_null_countleft_null_maskright_null_maskr)   r)   r*   r4      s   r4   c             
   C  sF  | j }| j|j}}	| r| j|dddd }
n|t|ttfrKt|	ttfrK||	krKtt|dd||||d}t	| |||	|d t
ddgt |d}
nJt|trut|	truttddd||||d}t| |||	|d t
ddgt |d}
n t|trt|	trt| ||d	}t
d|gt |d}
n| |k}
|
 rtd
| | dS dS )z2Check exact value equality for various data types.r   Tr   r   
nans_equalFr   )check_fn )r0   backend)r   exact value mismatchN)r=   r0   
is_numericis_close
isinstancer   r   r   r<   _check_list_liker   r   r   _check_structr   _check_categoricalr,   r   )r    r"   r   r   r   r   r   r@   rD   rE   is_not_equal_maskrR   
_not_equalr)   r)   r*   r6      sZ   

r6   c                C  s<   | j |||dd }| rtd| ||| dS dS )z0Check approximate value equality with tolerance.TrP   zvalues not within toleranceN)rW   r,   r   rK   )r    r"   r   r   is_not_close_maskr)   r)   r*   r7      s   r7   r:   r;   rD   List | ArrayrE   rR   c              	   C  sh   | j }z t| |D ]\}}|td||j|dtd||j|d q	W d S  ty3   td| | Y d S w )NrS   )valuesr0   rT   znested value mismatch)r=   r   r   innerAssertionErrorr   )r:   r;   rD   rE   rR   implleft_val	right_valr)   r)   r*   rY      s   
rY   r   c                 C  s^   zt |j|jD ]\}}|| j|j|j|j qW d S  ty.   td| | Y d S w )NrU   )r   fieldsstructfieldr?   rb   r   )r:   r;   rD   rE   rR   
left_fieldright_fieldr)   r)   r*   rZ      s   rZ   c             
   C  sT   |r|  t | t } }z| |k W S  ty) } zd}t||d}~ww )zTry to compare if any element of categorical series' differ.

    Inability to compare means that the encoding is different, and an exception is raised.
    z:Cannot compare categoricals coming from different sources.N)castr   r,   	Exceptionrb   )r:   r;   r   excr9   r)   r)   r*   r[     s   
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   r#   r%   r&   )r    r   r"   r   r%   rH   )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   r;   r   rD   r_   rE   r_   rR   r   r%   r&   )r:   r   r;   r   rD   r   rE   r   rR   r   r%   r&   )r:   r   r;   r   r   r#   r%   r#   ))
__future__r   	functoolsr   typingr   r   r   narwhals._utilsr   r   narwhals.dependenciesr
   narwhals.dtypesr   r   r   r   r   r   narwhals.functionsr   narwhals.testing.asserts.utilsr   typing_extensionsr   narwhals.seriesr   narwhals.typingr   r   r   __annotations__r<   r/   r4   r6   r7   rY   rZ   r[   r)   r)   r)   r*   <module>   s8      
a


A

