
    Mh>                    (   S r SSKJr  SSKJrJr  SSKrSSKJ	r	  SSK
JrJrJrJrJr  SSKJr  SSKJrJr  \(       a  SS	KJr  SS
KJr  SSKJr  SS jrSS jrSS jrSS jrSS jrSS jr SSS jjr!SS S jjr"S!S jr#S r$S"S jr%S#S jr&S$S jr'g)%z$
Low-dependency indexing utilities.
    )annotations)TYPE_CHECKINGAnyN)lib)is_array_likeis_bool_dtype
is_integeris_integer_dtypeis_list_like)ExtensionDtype)ABCIndex	ABCSeries)AnyArrayLike)	DataFrame)Indexc                    [         R                  " U R                  5      =(       aG    [         R                  " U R                  5      =(       a     [         R                  " U R                  5      $ )z
Check if a slice object can be interpreted as a positional indexer.

Parameters
----------
slc : slice

Returns
-------
bool

Notes
-----
A valid positional slice may also be interpreted as a label-based slice
depending on the index being sliced.
)r   is_int_or_nonestartstopstep)slcs    L/var/www/html/env/lib/python3.13/site-packages/pandas/core/indexers/utils.pyis_valid_positional_slicer   &   sI    $ 	399% 	)sxx(	)sxx(    c                    [        U 5      =(       a-    [        U [        5      =(       a    [        U 5      [        L(       + $ )z|
Check if we have a list-like indexer that is *not* a NamedTuple.

Parameters
----------
key : object

Returns
-------
bool
)r   
isinstancetupletype)keys    r   is_list_like_indexerr    >   s-     Xje&<&WcRWAW!XXr   c                    US:X  a  [        U 5      (       a  g[        U [        5      (       a!  [        U 5      U:X  a  [	        S U  5       5      $ g)z
Return True if we are all scalar indexers.

Parameters
----------
indexer : object
ndim : int
    Number of dimensions in the object being indexed.

Returns
-------
bool
   Tc              3  8   #    U  H  n[        U5      v   M     g 7fN)r	   ).0xs     r   	<genexpr>$is_scalar_indexer.<locals>.<genexpr>`   s     2'Q:a=='s   F)r	   r   r   lenall)indexerndims     r   is_scalar_indexerr-   N   sD     qyZ(('5!!c'ld&:2'222r   c                    [        U 5      (       a  [        U 5      (       d  g[        U [        5      (       d  U 4n [	        S U  5       5      $ )zb
Check if we have an empty indexer.

Parameters
----------
indexer : object

Returns
-------
bool
Tc              3     #    U  H4  n[        U[        R                  5      =(       a    [        U5      S :H  v   M6     g7f)r   N)r   npndarrayr)   )r%   idxs     r   r'   #is_empty_indexer.<locals>.<genexpr>t   s+     Pz#rzz*<s3x1}<s   <>)r   r)   r   r   any)r+   s    r   is_empty_indexerr5   d   s?     GS\\gu%%*PPPPr   c                   Sn[        U [        R                  [        45      (       a  [	        U5      (       a  [        U 5      [        U5      :w  a  UR                  S:X  a  [        U [        5      (       a  [        R                  " U 5      n [        U [        R                  5      (       a;  U R                  [        R                  :X  a  U R                  5       [        U5      :X  d  [        S5      e[        U 5      (       d  SnU$ [        U [        5      (       aU  [	        U5      (       aE  [        U5      [        X5      :w  a  UR                  S:X  a  [        S5      e[        U5      (       d  SnU$ )a6  
Validate that value and indexer are the same length.

An special-case is allowed for when the indexer is a boolean array
and the number of true values equals the length of ``value``. In
this case, no exception is raised.

Parameters
----------
indexer : sequence
    Key for the setitem.
value : array-like
    Value for the setitem.
values : array-like
    Values being set into.

Returns
-------
bool
    Whether this is an empty listlike setting which is a no-op.

Raises
------
ValueError
    When the indexer is an ndarray or list and the lengths don't match.
Fr"   zKcannot set using a list-like indexer with a different length than the valueTzGcannot set using a slice indexer with a different length than the value)r   r0   r1   listr   r)   r,   arraydtypebool_sum
ValueErrorslicelength_of_indexer)r+   valuevaluesno_ops       r   check_setitem_lengthsrB   {   s   6 E'BJJ-.. 7|s5z)fkkQ.>gt,, hhw/Gw

331U3$A  w<< L 
GU	#	#5z.w??FKKSTDT 6  u::Lr   c                    [        U 5      (       aH  U R                  5       nUS:  a  SU S3n[        U5      eU R                  5       nXA:  a  [	        S5      egg)a  
Perform bounds-checking for an indexer.

-1 is allowed for indicating missing values.

Parameters
----------
indices : ndarray
n : int
    Length of the array being indexed.

Raises
------
ValueError

Examples
--------
>>> validate_indices(np.array([1, 2]), 3) # OK

>>> validate_indices(np.array([1, -2]), 3)
Traceback (most recent call last):
    ...
ValueError: negative dimensions are not allowed

>>> validate_indices(np.array([1, 2, 3]), 3)
Traceback (most recent call last):
    ...
IndexError: indices are out-of-bounds

>>> validate_indices(np.array([-1, -1]), 0) # OK

>>> validate_indices(np.array([0, 1]), 0)
Traceback (most recent call last):
    ...
IndexError: indices are out-of-bounds
z-'indices' contains values less than allowed (z < -1)indices are out-of-boundsN)r)   minr<   max
IndexError)indicesnmin_idxmsgmax_idxs        r   validate_indicesrN      s^    J 7||++-R<A'&QCS/!++-<899  r   c                   [        U [        5      (       aH  [        R                  " U 5      n [	        U 5      S:X  a#  [        R
                  " S[        R                  S9$ U S:  nUR                  5       (       a  U R                  5       n X==   U-  ss'   U(       a*  X:  U S:  -  nUR                  5       (       a  [        S5      eU $ )a  
Attempt to convert indices into valid, positive indices.

If we have negative indices, translate to positive here.
If we have indices that are out-of-bounds, raise an IndexError.

Parameters
----------
indices : array-like
    Array of indices that we are to convert.
n : int
    Number of elements in the array that we are indexing.
verify : bool, default True
    Check that all entries are between 0 and n - 1, inclusive.

Returns
-------
array-like
    An array-like of positive indices that correspond to the ones
    that were passed in initially to this function.

Raises
------
IndexError
    One of the converted indices either exceeded the number of,
    elements (specified by `n`), or was still negative.
r   r9   rE   )
r   r7   r0   r8   r)   emptyintpr4   copyrH   )rI   rJ   verifymasks       r   maybe_convert_indicesrV      s    8 '4  ((7#w<1 88ARWW--Q;Dxxzz,,.1-88::899Nr   c                   Ub  [        U [        5      (       ay  [        U5      nU R                  nU R                  nU R
                  nUc  SnO
US:  a  X2-  nUb  XB:  a  UnO
US:  a  XB-  nUc  SnOUS:  a  US-   US-   pCU* nXC-
  U-   S-
  U-  $ [        U [        [        [        R                  [        45      (       aZ  [        U [        5      (       a  [        R                  " U 5      n U R                  [        :X  a  U R                  5       $ [        U 5      $ [        U [        5      (       a&  U R                  U R                  -
  U R
                  -  $ [!        U 5      (       d  g[#        S5      e)zD
Return the expected length of target[indexer]

Returns
-------
int
r   r"   z%cannot find the length of the indexer)r   r=   r)   r   r   r   r   r   r0   r1   r7   r8   r9   boolr;   ranger    AssertionError)r+   target
target_lenr   r   r   s         r   r>   r>   "  sI    j%88[
||||=EQYE<4,DAXD<DAX(EAI45Dt#a'D00	Gi2::tD	E	Egt$$hhw'G==D ;;= 7|	GU	#	#w}},==!'**
@
AAr   c                N    [         R                  " U 5      S:  a  [        S5      eg)z
Helper function to disallow multi-dimensional indexing on 1D Series/Index.

GH#27125 indexer like idx[:, None] expands dim, but we cannot do that
and keep an index, so we used to return ndarray, which was deprecated
in GH#30588.
r"   zzMulti-dimensional indexing (e.g. `obj[:, None]`) is no longer supported. Convert to a numpy array before indexing instead.N)r0   r,   r<   )results    r   disallow_ndim_indexingr_   L  s,     
wwvK
 	
 r   c                    [        U 5      S:X  a=  [        U S   [        5      (       a%  [        U [        5      (       a  [	        S5      eU S   $ U $ )z}
If we have a length-1 tuple/list that contains a slice, unpack to just
the slice.

Notes
-----
The list case is deprecated.
r"   r   zYIndexing with a single-item list containing a slice is not allowed. Pass a tuple instead.)r)   r   r=   r7   r<   )tups    r   unpack_1tuplerb   [  sP     3x1}CFE22 c4  > 
 1vJr   c                    U R                   (       a.  [        UR                  5      [        U5      :w  a  [        S5      eg[        U R	                  U5      S   5      [        UR                  5      :w  a  [        S5      eg)a  
Checks if a key used as indexer has the same length as the columns it is
associated with.

Parameters
----------
columns : Index The columns of the DataFrame to index.
key : A list-like of keys to index with.
value : DataFrame The value to set for the keys.

Raises
------
ValueError: If the length of key is not equal to the number of columns in value
            or if the number of columns referenced by key is not equal to number
            of columns.
z"Columns must be same length as keyr   N)	is_uniquer)   columnsr<   get_indexer_non_unique)re   r   r?   s      r   check_key_lengthrg   s  sm    " u}}S)ABB * w--c2156#emm:LLABB Mr   c                    [        U 5      S:  a#  U S   [        L a  U SS n OU S   [        L a  U SS n [        U 5      S:  a  [        S5      eU S   n U $ )z'
Possibly unpack arr[..., n] to arr[n]
r"   r   NrD   ztoo many indices for array.)r)   EllipsisrH   )items    r   unpack_tuple_and_ellipsesrk     sb     4y1}7h8D"X!9D
4y1}6777DKr   c                   SSK Jn  [        U5      (       a  [        U[        5      (       a  U$ OU$ [        U5      (       d:  U" U5      n[        U5      S:X  a#  [        R                  " / [        R                  S9nUR                  n[        U5      (       a  [        U[        5      (       a  UR                  [        SS9nO[        R                  " U[        S9n[        U5      [        U 5      :w  a#  [        S[        U5       S[        U 5       35      e U$ [!        U5      (       a&   [        R                  " U[        R                  S9nU$ [        S
5      e! ["         a  n[#        S5      UeS	nAff = f)a
  
Check if `indexer` is a valid array indexer for `array`.

For a boolean mask, `array` and `indexer` are checked to have the same
length. The dtype is validated, and if it is an integer or boolean
ExtensionArray, it is checked if there are missing values present, and
it is converted to the appropriate numpy array. Other dtypes will raise
an error.

Non-array indexers (integer, slice, Ellipsis, tuples, ..) are passed
through as is.

Parameters
----------
array : array-like
    The array that is being indexed (only used for the length).
indexer : array-like or list-like
    The array-like that's used to index. List-like input that is not yet
    a numpy array or an ExtensionArray is converted to one. Other input
    types are passed through as is.

Returns
-------
numpy.ndarray
    The validated indexer as a numpy array that can be used to index.

Raises
------
IndexError
    When the lengths don't match.
ValueError
    When `indexer` cannot be converted to a numpy ndarray to index
    (e.g. presence of missing values).

See Also
--------
api.types.is_bool_dtype : Check if `key` is of boolean dtype.

Examples
--------
When checking a boolean mask, a boolean ndarray is returned when the
arguments are all valid.

>>> mask = pd.array([True, False])
>>> arr = pd.array([1, 2])
>>> pd.api.indexers.check_array_indexer(arr, mask)
array([ True, False])

An IndexError is raised when the lengths don't match.

>>> mask = pd.array([True, False, True])
>>> pd.api.indexers.check_array_indexer(arr, mask)
Traceback (most recent call last):
...
IndexError: Boolean index has wrong length: 3 instead of 2.

NA values in a boolean array are treated as False.

>>> mask = pd.array([True, pd.NA])
>>> pd.api.indexers.check_array_indexer(arr, mask)
array([ True, False])

A numpy boolean mask will get passed through (if the length is correct):

>>> mask = np.array([True, False])
>>> pd.api.indexers.check_array_indexer(arr, mask)
array([ True, False])

Similarly for integer indexers, an integer ndarray is returned when it is
a valid indexer, otherwise an error is  (for integer indexers, a matching
length is not required):

>>> indexer = pd.array([0, 2], dtype="Int64")
>>> arr = pd.array([1, 2, 3])
>>> pd.api.indexers.check_array_indexer(arr, indexer)
array([0, 2])

>>> indexer = pd.array([0, pd.NA], dtype="Int64")
>>> pd.api.indexers.check_array_indexer(arr, indexer)
Traceback (most recent call last):
...
ValueError: Cannot index with an integer indexer containing NA values

For non-integer/boolean dtypes, an appropriate error is raised:

>>> indexer = np.array([0., 2.], dtype="float64")
>>> pd.api.indexers.check_array_indexer(arr, indexer)
Traceback (most recent call last):
...
IndexError: arrays used as indices must be of integer or boolean type
r   )r8   rP   F)r9   na_valuez Boolean index has wrong length: z instead of z9Cannot index with an integer indexer containing NA valuesNz9arrays used as indices must be of integer or boolean type)pandas.core.constructionr8   r   r   r   r   r)   r0   rR   r9   r   r   to_numpyrX   asarrayrH   r
   r<   )r8   r+   pd_arrayr9   errs        r   check_array_indexerrs     sV   x ; Ggu%%N &  !!7#w<1hhr1GMMEUe^,,&&TE&BGjj5G w<3u:%2w<.SZL:  & N 
%	 	 	jj8G N TUU  	K	s   )#E 
E4#E//E4)r   r=   returnrX   )rt   rX   )r,   intrt   rX   )rI   
np.ndarrayrJ   ru   rt   None)T)rJ   ru   rT   rX   rt   rv   r$   )rt   ru   )rt   rw   )re   r   r?   r   rt   rw   )rj   r   )r8   r   r+   r   rt   r   )(__doc__
__future__r   typingr   r   numpyr0   pandas._libsr   pandas.core.dtypes.commonr   r   r	   r
   r   pandas.core.dtypes.dtypesr   pandas.core.dtypes.genericr   r   pandas._typingr   pandas.core.framer   pandas.core.indexes.baser   r   r    r-   r5   rB   rN   rV   r>   r_   rb   rg   rk   rs    r   r   <module>r      s    #
    5
 ++.0Y ,Q.=@-:h,f'BT
0C4,Fr   