HOME


sh-3ll 1.0
DIR:/usr/local/lib/python3.6/site-packages/xarray/core/__pycache__/
Upload File :
Current File : //usr/local/lib/python3.6/site-packages/xarray/core/__pycache__/indexing.cpython-36.pyc
3

���hh=�@sddlmZddlZddlZddlmZddlm	Z	m
Z
mZmZddlm
Z
dd�Zd	d
�Zdd�Zd
d�Zdd�Zdd�Zd(dd�Zd)dd�Zdd�Zdd�ZGdd�dej�ZGdd�dej�Zd d!�ZGd"d#�d#ej�ZGd$d%�d%ej�ZGd&d'�d'ej�ZdS)*�)�	timedeltaN�)�utils)�	iteritems�range�dask_array_type�suppress)�
is_full_slicecCs�t|t�s|f}g}d}xX|D]P}|tkrd|sT|j|dt|�td�g�d}qn|jtd��q|j|�qWt|�|kr�td��|j|t|�td�g�t|�S)aMGiven a key for indexing an ndarray, return an equivalent key which is a
    tuple with length equal to the number of dimensions.

    The expansion is done by replacing all `Ellipsis` items with the right
    number of full slices and then padding the key with full slices so that it
    reaches the appropriate dimensionality.
    FrNTztoo many indices)�
isinstance�tuple�Ellipsis�extend�len�slice�append�
IndexError)�key�ndim�new_keyZfound_ellipsis�k�r�4/tmp/pip-build-5_djhm0z/xray/xarray/core/indexing.py�expanded_indexer
s

 rcs$dd��t�fdd�t||�D��S)z�Given an indexer for orthogonal array indexing, return an indexer that
    is a tuple composed entirely of slices, integer ndarrays and native python
    ints.
    cSsvt|t�srtj|�}|jdkr.ttj|��}nD|jdkr@td��|jj	dkrZtj
|�\}n|jj	dkrrtd|��|S)Nrrz1orthogonal array indexing only supports 1d arrays�b�iz}invalid subkey %r for integer based array indexing; all subkeys must be slices, integers or sequences of integers or Booleans)r
r�np�asarrayr�intZasscalar�
ValueError�dtype�kind�nonzero)�indexerrrr�canonicalize-s



z*canonicalize_indexer.<locals>.canonicalizec3s|]}�|�VqdS)Nr)�.0r)r#rr�	<genexpr>?sz'canonicalize_indexer.<locals>.<genexpr>)rr)rrr)r#r�canonicalize_indexer(sr&cCstj|j|��S)N)r�arange�indices)Zslice_�sizerrr�
_expand_sliceBsr*cs�tt�t�����dd�t��D�}��fdd���|�}dd��tj���fdd�|D��}x t|�D]\}}||�|<qhWt��S)	z�Given a key for orthogonal array indexing, returns an equivalent key
    suitable for indexing a numpy.ndarray with fancy indexing.
    cSs$g|]\}}t|ttjf�s|�qSr)r
rr�integer)r$�nrrrr�
<listcomp>Psz&orthogonal_indexer.<locals>.<listcomp>csf�fdd�}|s|S|t|dd��r8�|dd��S|t|dt����r^�|dd��S|SdS)Ncst�fdd�|D��S)Nc3s|]}t�|�VqdS)N)r	)r$r,)rrrr%Tsz^orthogonal_indexer.<locals>.full_slices_unselected.<locals>.all_full_slices.<locals>.<genexpr>)�all)Z	key_index)rrr�all_full_slicesSszKorthogonal_indexer.<locals>.full_slices_unselected.<locals>.all_full_slicesrr���r0)rr)Zn_listr/)�full_slices_unselectedrrrr1Rsz2orthogonal_indexer.<locals>.full_slices_unselectedcSst|t�rt||�S|S)N)r
rr*)r�lengthrrr�maybe_expand_slicegsz.orthogonal_indexer.<locals>.maybe_expand_slicec3s |]}��|�|�VqdS)Nr)r$r,)rr3�shaperrr%jsz%orthogonal_indexer.<locals>.<genexpr>)�listr&r�	enumeraterZix_r)rr4Znon_int_keysZ
array_keysZarray_indexersrr,r)r1rr3r4r�orthogonal_indexerFs
r7cCs"y|j�Stk
r|SXdS)N)�item�AttributeError)�xrrr�
_try_get_itemqsr;cCsLt|t�rtj|�}n2tj|�}|jdkrHtjt|�t	d�}||dd�<|S)z�
    Convert values into a numpy array of at most 1-dimension, while preserving
    tuples.

    Adapted from pandas.core.common._asarray_tuplesafe
    �)rN)
r
rrZtuple_to_0darrayrrr�emptyr�object)�values�resultrrr�_asarray_tuplesafexs


rA�cCs�i}|dk	r||d<|dk	r6tjdkr.td��||d<t|t�r�|dk	sP|dk	rXtd��|jt|j�t|j�t|j	��}t|t�s�t
d��nht|�}|jdkr�|j
|j�f|�}nB|jjd	kr�tj|�\}n(|j|f|�}tj|dk�r�t
d
|��|S)z�Given a pandas.Index and labels (e.g., from __getitem__) for one
    dimension, return an indexer suitable for indexing an ndarray along that
    dimension
    N�methodz0.17z5the tolerance argument requires pandas v0.17 or newer�	tolerancez@cannot use ``method`` argument if any indexers are slice objectsz�cannot represent labeled-based slice indexer for dimension %r with a slice over integer positions; the index is unsorted or non-uniquerrz not all values found in index %r)�pd�__version__�NotImplementedErrorr
rZ
slice_indexerr;�start�stop�step�KeyErrorrArZget_locr8rr rr!Zget_indexer�any)�index�labelZ
index_namerCrD�kwargsr"rrr�convert_label_indexer�s6




rPcs:�dk	rt�t�rtd��t���fdd�t|�D��S)zvGiven an xarray data object and label based indexers, return a mapping
    of equivalent location based indexers.
    Nz``method`` must be a stringc3s.|]&\}}|t�|j�||���fVqdS)N)rPZto_index)r$ZdimrN)�data_objrCrDrrr%�sz'remap_label_indexers.<locals>.<genexpr>)r
�str�	TypeError�dictr)rQZindexersrCrDr)rQrCrDr�remap_label_indexers�srUcCsd|jpd|jpd}t||�|}t|�dkrP|d}|d|}|dkrXd}nd}d}t|||�S)z�Given a slice and the size of the dimension to which it will be applied,
    index it with another slice to return a new slice equivalent to applying
    the slices sequentially
    rrNr0)rJr*rr)Z	old_sliceZ
applied_slicer)rJ�itemsrHrIrrr�slice_slice�srWcCsrt|ttjttjf�st�t|t�r4|td�kr4|St|t�rft|t�rVt|||�}qnt||�|}n||}|S)N)	r
rrr+r�ndarray�AssertionErrorrWr*)Zold_indexerZapplied_indexerr)r"rrr�_index_indexer_1d�s

rZc@s^eZdZdd�Zedd��Zedd��Zedd��Zed	d
��Zdd�Z	ddd�Z
dd�Zd
S)�LazyIntegerRangecOs*||_||_d|jkst�t||�|_dS)zE
        Parameters
        ----------
        See np.arange
        rN)�args�kwdargsrYr�array)�selfr\r]rrr�__init__�szLazyIntegerRange.__init__cCst|j�fS)N)rr^)r_rrrr4�szLazyIntegerRange.shapecCs
tjd�S)NZint64)rr)r_rrrr�szLazyIntegerRange.dtypecCsdS)Nrr)r_rrrr�szLazyIntegerRange.ndimcCs
t|j�S)N)rr^)r_rrrr)szLazyIntegerRange.sizecCstj|�|S)N)rr^)r_rrrr�__getitem__szLazyIntegerRange.__getitem__NcCstj|j|j�S)N)rr'r\r])r_rrrr�	__array__szLazyIntegerRange.__array__cCsdt|�j|jfS)Nz%s(array=%r))�type�__name__r^)r_rrr�__repr__szLazyIntegerRange.__repr__)N)rd�
__module__�__qualname__r`�propertyr4rrr)rarbrerrrrr[�s
r[c@sPeZdZdZddd�Zdd�Zedd��Zdd	d
�Zdd�Z	d
d�Z
dd�ZdS)�LazilyIndexedArrayzIWrap an array that handles orthogonal indexing to make indexing lazy
    NcCs(|dkrtd�f|j}||_||_dS)z�
        Parameters
        ----------
        array : array_like
            Array like object to index.
        key : tuple, optional
            Array indexer. If provided, it is assumed to already be in
            canonical expanded form.
        N)rrr^r)r_r^rrrrr`s
zLazilyIndexedArray.__init__cCsltt||j��}g}xNt|jj|j�D]:\}}t|tt	j
f�rJ|j|�q&|jt|t
|�|��q&Wt|�S)N)�iterr&r�zipr^r4rr
rrr+rrZ�nextr)r_rrr)rrrr�_updated_key"szLazilyIndexedArray._updated_keycCsfg}xXt|jj|j�D]D\}}t|t�rB|jtt|j	|����qt|t
j�r|j|j�qWt
|�S)N)rkr^r4rr
rrrrr(rrXr)r)r_r4r)rrrrr4,s
zLazilyIndexedArray.shapecCst|j�}tj||jdd�S)N)r)�orthogonally_indexabler^rrr)r_rr^rrrrb6s
zLazilyIndexedArray.__array__cCst|�|j|j|��S)N)rcr^rm)r_rrrrra:szLazilyIndexedArray.__getitem__cCs|j|�}||j|<dS)N)rmr^)r_r�valuerrr�__setitem__=s
zLazilyIndexedArray.__setitem__cCsdt|�j|j|jfS)Nz%s(array=%r, key=%r))rcrdr^r)r_rrrreAszLazilyIndexedArray.__repr__)N)N)rdrfrg�__doc__r`rmrhr4rbrarprerrrrris



ricCs>t|tj�rt|�St|tj�r(t|�St|t�r:t|�S|S)N)	r
rrX�NumpyIndexingAdapterrE�Index�PandasIndexAdapterr�DaskIndexingAdapter)r^rrrrnFs
rnc@s:eZdZdZdd�Zd
dd�Zdd�Zd	d
�Zdd�ZdS)rrz�Wrap a NumPy array to use orthogonal indexing (array indexing
    accesses different dimensions independently, like netCDF4-python variables)
    cCstj|�|_dS)N)rrr^)r_r^rrrr`WszNumpyIndexingAdapter.__init__NcCstj|j|d�S)N)r)rrr^)r_rrrrrbZszNumpyIndexingAdapter.__array__cCs.t||j�}tdd�|D��r*t||j�}|S)Ncss"|]}t|ttjtf�VqdS)N)r
rrr+r)r$rrrrr%_sz4NumpyIndexingAdapter._convert_key.<locals>.<genexpr>)rrrLr7r4)r_rrrr�_convert_key]sz!NumpyIndexingAdapter._convert_keycCs|j|�}|j|S)N)rvr^)r_rrrrrads
z NumpyIndexingAdapter.__getitem__cCs|j|�}||j|<dS)N)rvr^)r_rrorrrrphs
z NumpyIndexingAdapter.__setitem__)N)	rdrfrgrqr`rbrvrarprrrrrrPs
rrc@s eZdZdZdd�Zdd�ZdS)ruz5Wrap a dask array to support orthogonal indexing
    cCs
||_dS)N)r^)r_r^rrrr`pszDaskIndexingAdapter.__init__cCsjt||j�}tdd�|D��r\|j}x@ttt|���D] \}}|td�f||f}q6Wn
|j|}|S)Ncss"|]}t|ttjtf�VqdS)N)r
rrr+r)r$rrrrr%usz2DaskIndexingAdapter.__getitem__.<locals>.<genexpr>)rrrLr^�reversedr5r6r)r_rroZaxis�subkeyrrrrass
zDaskIndexingAdapter.__getitem__N)rdrfrgrqr`rarrrrrumsruc@sLeZdZdZddd�Zedd��Zddd�Zed	d
��Zdd�Z	d
d�Z
dS)rtzvWrap a pandas.Index to be better about preserving dtypes and to handle
    indexing by length 1 tuples like numpy
    NcCshtj|�|_|dkr^t|tj�r,tjd�}n2t|d�r@|j	j}ntj
|j�sXtjd�}n|j}||_dS)N�O�
categories)rZsafe_cast_to_indexr^r
rE�PeriodIndexrr�hasattrrzZis_valid_numpy_dtype�_dtype)r_r^rrrrr`�s

zPandasIndexAdapter.__init__cCs|jS)N)r})r_rrrr�szPandasIndexAdapter.dtypecCsJ|dkr|j}|j}t|tj�r:tt��|j}WdQRXtj	|j
|d�S)N)r)rr^r
rEr{rr9Zasobjectrrr?)r_rr^rrrrb�s
zPandasIndexAdapter.__array__cCst|j�fS)N)rr^)r_rrrr4�szPandasIndexAdapter.shapecCs�t|t�rt|�dkr|\}|j|}t|tj�rBt||jd�}nR|tjkrZt	j
dd�}n:t|t�rzt	jt
|d|�d�}n|jtkr�t	j||jd�}|S)Nr)r�NaT�nsro)r
rrr^rErsrtrr~rZ
datetime64rZtimedelta64�getattrr>r)r_rr@rrrra�s



zPandasIndexAdapter.__getitem__cCsdt|�j|j|jfS)Nz%s(array=%r, dtype=%r))rcrdr^r)r_rrrre�szPandasIndexAdapter.__repr__)N)N)rdrfrgrqr`rhrrbr4rarerrrrrt~s


rt)rBNN)NN)�datetimerZnumpyrZpandasrErBrZpycompatrrrrr	rr&r*r7r;rArPrUrWrZZNDArrayMixinr[rirnrrrurtrrrr�<module>s,+
,
)6