
    hhn+                    @   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r1d 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d3d"Z!d4d$Z"d5d&Z#d6d'Z$d7d(Z%d8d/Z&d9d1Z'd:d2Z(dS );    )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          	     X   d}	t          d | |fD                       rLdt          t          |                      dt          t          |                     d}
t          |
          t	          | |||           |sR| j                                        rd}
t          |
          |                                 |                                }} t          | |          \  }}|s| j        
                                st          |||||||           d
S t          ||||	           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              3  6   K   | ]}t          |           V  d S )Nr
   ).0objs     V/var/www/histauto/venv/lib/python3.11/site-packages/narwhals/testing/asserts/series.py	<genexpr>z&assert_series_equal.<locals>.<genexpr>T   s.      
@
@3!#&&&
@
@
@
@
@
@    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_valss                r,   assert_series_equalr?      sk   z 

@
@4-
@
@
@@@ 5*4::665 5+DKK885 5 5 	 nnD%lTTTT 0:!! 	+UC%c***iikk5::<<e.tU;;Iz [$*--// [%#1	
 	
 	
 	
 	
 	
 	")ZRYZZZZZZr.   r   c               ^   | j         |j         }}||k    rt          d||           t          |           t          |          }}||k    rt          d||           | j        |j        }	}|r||	k    rt          d||	           | j        |j        }}
|r|
|k    rt          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   lenr3   name)r!   r#   r   r   	left_impl
right_implleft_len	right_len
left_dtyperight_dtype	left_name
right_names               r,   r2   r2   v   s     !/1EzIJ$%>	:VVVd))SZZiH9$%6)LLL"j%+J P
k11$%5z;OOO IuzzI MyJ..$_iLLLLLM M..r.   tuple[SeriesT, SeriesT]c                X   |                                  |                                 }}|                                 |                                }}||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_masks         r,   r7   r7      s    (,(9(95;K;K;M;M%O&*llnnemmooON***~/P.U.U.W.W*$!?4D	
 	
 	
 ;;''6F)G)GGGr.   c          
        | j         }| j        |j        }	}|                                r|                     |ddd           }
nmt	          |t
          t          f          rst	          |	t
          t          f          rW||	k    rQt          t          |dd||||          }t          | |||	|           t          ddgt                      |          }
nt	          |t                    rft	          |	t                    rQt          t          ddd||||          }t          | |||	|           t          ddgt                      |          }
nct	          |t                    rHt	          |	t                    r3t          | ||	          }t          d|gt                      |          }
n| |k    }
|
                                rt#          d
| |           dS dS )z2Check exact value equality for various data types.r   Tr   r   
nans_equalFr   )check_fn )r3   backend)r    exact value mismatchN)rA   r3   
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    rD   rH   rI   is_not_equal_maskrX   
_not_equals                r,   r9   r9      s+    #I"j%+J  ,* "]]5!QSW]XXX:t}--(*2<[5RV-2X2X(*

#
#%#1	
 	
 	
 	uj+QQQQ&rE7'))YWWW	J	'	' *J{F,K,K *#1	
 	
 	
 	dE:{XNNNN&rE7'))YWWW	J	,	, 
*K1U1U 
* (%,>
 
 

 'GIIy
 
 
 !EM J$%;T5IIIIIJ Jr.   c                   |                      |||d           }|                                r9t          d|                     |          |                    |                     dS dS )z0Check approximate value equality with tolerance.TrV   zvalues not within toleranceN)r]   r/   r   rP   )r!   r#   r   r   is_not_close_masks        r,   r:   r:      s     wD '     
$)KK)**LL*++	
 	
 	
 	
 	

 
r.   r=   r>   rH   List | ArrayrI   rX   c           
         | j         }	 t          | |          D ]=\  }} |t          d||j        |          t          d||j        |                     >d S # t          $ r t          d| |           Y d S w xY w)NrY   )valuesr3   rZ   znested value mismatch)rA   r	   r   innerAssertionErrorr   )r=   r>   rH   rI   rX   implleft_val	right_vals           r,   r_   r_      s     #DU#-i#D#D 	 	HiH2hj6FPTUUU2i{7HRVWWW   	 	
  U U U$%<iTTTTTTUs   AA A87A8r   c                   	 t          |j        |j                  D ]K\  }} || j                            |j                  |j                            |j                             Ld S # t
          $ r t          d| |           Y d S w xY w)Nr[   )r	   fieldsstructfieldrC   rj   r   )r=   r>   rH   rI   rX   
left_fieldright_fields          r,   r`   r`      s    T'1*2C[EW'X'X 	 	#JH &&z77!''(899   	 	
  T T T$%;Y
SSSSSSTs   A&A* *B	B	c                  |rB|                      t                                |                     t                                }} 	 | |k                                    S # t          $ r}d}t	          |          |d}~ww xY 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/   	Exceptionrj   )r=   r>   r    excr<   s        r,   ra   ra     s      T )vxx 8 8*//&((:S:S:	+Z',,... + + +JS!!s*+s   A 
A?(A::A?)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&   rL   )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   rf   rI   rf   rX   r   r&   r'   )r=   r   r>   r   rH   r   rI   r   rX   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?   r2   r7   r9   r:   r_   r`   ra    r.   r,   <module>r      s"   " " " " " " "       / / / / / / / / / / ; ; ; ; ; ; ; ; 4 4 4 4 4 4 M M M M M M M M M M M M M M M M ) ) ) ) ) ) G G G G G G D++++++&&&&&&44444444!6#;s"<d"BCGCCCC $^[ ^[ ^[ ^[ ^[ ^[BM M M M*
H 
H 
H 
H>J >J >J >JB
 
 
 
 U U U U*T T T T,+ + + + + +r.   