HOME


sh-3ll 1.0
DIR:/usr/local/lib64/python3.6/site-packages/pandas/core/__pycache__/
Upload File :
Current File : //usr/local/lib64/python3.6/site-packages/pandas/core/__pycache__/indexers.cpython-36.pyc
3

���h�5�@s�dZddlZddlZddlmZmZddlmZm	Z	m
Z
mZmZm
Z
ddlmZmZeed�dd�Zed	�d
d�Zeed�d
d�Zejed�dd�Zdd	�dd�Zejedd�dd�Zed�dd�Zd$ed	�dd�Zd%dd�Zdd �Zeeed!�d"d#�ZdS)&z$
Low-dependency indexing utilities.
�N)�Any�AnyArrayLike)�
is_array_like�
is_bool_dtype�is_extension_array_dtype�
is_integer�is_integer_dtype�is_list_like)�
ABCIndexClass�	ABCSeries)�slc�returncCs&dd�}||j�o$||j�o$||j�S)a(
    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.
    cSs|dkpt|�S)N)r)�val�r�6/tmp/pip-build-5_djhm0z/pandas/pandas/core/indexers.py�is_int_or_none*sz1is_valid_positional_slice.<locals>.is_int_or_none)�start�stop�step)rrrrr�is_valid_positional_slices

r)r
cCs t|�ot|t�ot|�tk	S)z�
    Check if we have a list-like indexer that is *not* a NamedTuple.

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

    Returns
    -------
    bool
    )r	�
isinstance�tuple�type)�keyrrr�is_list_like_indexer4s
r)�ndimr
cCs,t|t�r(t|�|kr(tdd�|D��SdS)z�
    Return True if we are all scalar indexers.

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

    Returns
    -------
    bool
    css@|]8}t|�p6t|tj�o6|jt|�ko2dknVqdS)�N)rr�np�ndarrayr�len)�.0�xrrr�	<genexpr>Usz$is_scalar_indexer.<locals>.<genexpr>F)rrr�all)�indexerrrrr�is_scalar_indexerDs

r%)�	arr_valuer
cCsJt|�rt|�rdS|jdkrFt|t�s4t|g�}tdd�|D��SdS)z�
    Check if we have an empty indexer.

    Parameters
    ----------
    indexer : object
    arr_value : np.ndarray

    Returns
    -------
    bool
    Trcss&|]}t|tj�ot|�dkVqdS)rN)rrrr)r �idxrrrr"msz#is_empty_indexer.<locals>.<genexpr>F)r	rrrr�any)r$r&rrr�is_empty_indexer[s



r)cCs�t|tjtf�r^t|�r�t|�t|�kr�t|tj�oR|jtjkoRt||�t|�ks�td��n4t|t	�r�t|�r�t|�r�t|�t
||�kr�td��dS)aF
    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
    -------
    None

    Raises
    ------
    ValueError
        When the indexer is an ndarray or list and the lengths don't match.
    zKcannot set using a list-like indexer with a different length than the valuezGcannot set using a slice indexer with a different length than the valueN)rrr�listr	r�dtypeZbool_�
ValueError�slice�length_of_indexer)r$�value�valuesrrr�check_setitem_lengthsus
r1)�indices�nr
cCsHt|�rD|j�}|dkr,d|�d�}t|��|j�}||krDtd��dS)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([1, 2], 3)
    # OK
    >>> validate_indices([1, -2], 3)
    ValueError
    >>> validate_indices([1, 2, 3], 3)
    IndexError
    >>> validate_indices([-1, -1], 0)
    # OK
    >>> validate_indices([0, 1], 0)
    IndexError
    rz-'indices' contains values less than allowed (z < -1)zindices are out-of-boundsN���)r�minr,�max�
IndexError)r2r3�min_idx�msgZmax_idxrrr�validate_indices�sr:)r3cCs|t|t�r0tj|�}t|�dkr0tjdtjd�S|dk}|j�rX|j�}|||7<||k|dkB}|j�rxt	d��|S)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.

    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)r+zindices are out-of-bounds)
rr*r�arrayr�empty�intpr(�copyr7)r2r3�maskrrr�maybe_convert_indices�s

r@cCs|dk	r�t|t�r�t|�}|j}|j}|j}|dkr:d}n|dkrJ||7}|dksZ||kr`|}n|dkrp||7}|dkr~d}n |dkr�|d|d}}|}|||d|St|tttj	t
f�r�t|t
�r�tj|�}|jt
kr�|j�St|�St|��sdStd��dS)zX
    Return the expected length of target[indexer]

    Returns
    -------
    int
    Nrrz%cannot find the length of the indexer)rr-rrrrrr
rrr*r;r+�bool�sumr�AssertionError)r$�targetZ
target_lenrrrrrrr.s8



r.�cCs"tj|�dkrtjdt|d�dS)a#
    Helper function to raise the deprecation warning for 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 currently return ndarray, which is deprecated
    (Deprecation GH#30588).
    rz�Support for multi-dimensional indexing (e.g. `obj[:, None]`) is deprecated and will be removed in a future version.  Convert to a numpy array before indexing instead.)�
stacklevelN)rr�warnings�warn�
FutureWarning)�resultrFrrr�deprecate_ndim_indexing*s
	rKcCs@t|�dkr<t|dt�r<t|t�r4tjdtdd�|dS|S)z�
    If we have a length-1 tuple/list that contains a slice, unpack to just
    the slice.

    Notes
    -----
    The list case is deprecated.
    rrz|Indexing with a single-item list containing a slice is deprecated and will raise in a future version.  Pass a tuple instead.rE)rF)rrr-r*rGrHrI)�tuprrr�
unpack_1tuple=s	
rM)r;r$r
cCsddlm}t|�r$t|t�r(|Sn|St|�sT||�}t|�dkrTtjgtjd�}|j	}t
|�r�t|�rz|jt
dd�}ntj|t
d�}t|�t|�kr�tdt|��dt|�����nXt|��rytj|tjd�}Wn.tk
�r}ztd�|�WYd	d	}~XnXntd
��|S)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.

    .. versionadded:: 1.0.0

    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)r;)r+F)r+Zna_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)Zpandas.core.constructionr;r	rrrrrr=r+rrZto_numpyrAZasarrayr7rr,)r;r$Zpd_arrayr+�errrrr�check_array_indexer\s4^

rO)N)rE) �__doc__rGZnumpyrZpandas._typingrrZpandas.core.dtypes.commonrrrrrr	Zpandas.core.dtypes.genericr
rr-rArr�intr%rr)r1r:r@r.rKrMrOrrrr�<module>s  1,0(