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__/groupby.cpython-36.pyc
3

���h�M�@s�ddlZddlZddlZddlmZddlmZddl	m
Z
mZmZddl
mZddlmZmZmZddlmZmZmZd	d
�Zdd�Zd
d�ZGdd�de�ZGdd�dee
�Zeje�eje�Gdd�dee�Zeje�eje�dS)�N�)�ops)�concat)�ImplementsArrayReduce�ImplementsDatasetReduce�_maybe_promote)�zip)�peek_at�maybe_wrap_array�safe_cast_to_index)�as_variable�Variable�
CoordinatecCs\tj|dd�\}}dd�tt|��D�}x*t|�D]\}}|dkr2||j|�q2W||fS)a�Group an array by its unique values.

    Parameters
    ----------
    ar : array-like
        Input array. This will be flattened if it is not already 1-D.

    Returns
    -------
    values : np.ndarray
        Sorted, unique values as returned by `np.unique`.
    indices : list of lists of int
        Each element provides the integer indices in `ar` with values given by
        the corresponding value in `unique_values`.
    T)�sortcSsg|]}g�qS�r)�.0�_rr�3/tmp/pip-build-5_djhm0z/xray/xarray/core/groupby.py�
<listcomp> sz'unique_value_groups.<locals>.<listcomp>r)�pdZ	factorize�range�len�	enumerate�append)�arZinverse�values�groups�n�grrr�unique_value_groupssrcCst|�\}}|S)z\Return a fill value that appropriately promotes types when used with
    np.concatenate
    )r)�dtypeZ
fill_valuerrr�_get_fill_value(sr!cs�ddlm}ddlm}t�|�r^|tdd��jj�D��t�fdd��jj�D���j	�}nBt�|�r�|t
�j�t�fdd��jj�D���j�j	d�}nt
�|S)	Nr)�Dataset)�	DataArraycss |]\}}|t|j�fVqdS)N)r!r )r�k�vrrr�	<genexpr>4sz_dummy_copy.<locals>.<genexpr>c3s*|]"\}}|�jkr|t|j�fVqdS)N)�dimsr!r )rr$r%)�
xarray_objrrr&6sc3s*|]"\}}|�jkr|t|j�fVqdS)N)r'r!r )rr$r%)r(rrr&<s)�name�attrs)�datasetr"Z	dataarrayr#�
isinstance�dictZ	data_vars�itemsZcoordsr*r!r r)�AssertionError)r(r"r#�resr)r(r�_dummy_copy0s 




r1c@s�eZdZdZd#dd�Zedd��Zdd	�Zd
d�Zdd
�Z	dd�Z
ed$dd��Zdd�Z
dd�Zdd�Zdd�Zdd�Zd%dd�Zd&dd �Zd!d"�ZdS)'�GroupBya
A object that implements the split-apply-combine pattern.

    Modeled after `pandas.GroupBy`. The `GroupBy` object can be iterated over
    (unique_value, grouped_array) pairs, but the main way to interact with a
    groupby object are with the `apply` or `reduce` methods. You can also
    directly call numpy methods like `mean` or `std`.

    You should create a GroupBy object by using the `DataArray.groupby` or
    `Dataset.groupby` methods.

    See Also
    --------
    Dataset.groupby
    DataArray.groupby
    FNcCs�ddlm}|jdkrtd��t|dd�dkr6td��t|d�sHtd��|j\}y|j|}Wn$tk
r�|j|j	|�}YnX|j
|kr�td	��d}|dk	�r@t|�}	|	js�td
��t
jtj|	j
�|	�}
|
j|�j�}|j�j�r�|j}|j�}|j}dd�t|dd�|dd��D�t|dd�g}
t|j|j�}nd|j|jk�r�|j|jfk�rftd
��tj|j
�}
|�s�dd�|
D�}
|}nt|�\}}
t|j|�}||_||_||_|
|_ ||_!d|_"||_#dS)a:Create a GroupBy object

        Parameters
        ----------
        obj : Dataset or DataArray
            Object to group.
        group : DataArray or Coordinate
            1-dimensional array with the group values.
        squeeze : boolean, optional
            If "group" is a coordinate of object, `squeeze` controls whether
            the subarrays have a dimension of length 1 along that coordinate or
            if the dimension is squeezed out.
        grouper : pd.Grouper, optional
            Used for grouping values along the `group` array.
        r)�
as_datasetz`group` must be 1 dimensionalr)Nz`group` must have a namer'z$`group` must have a 'dims' attributezZthe group variable's length does not match the length of this variable along its dimensionz&index must be monotonic for resamplingcSsg|]\}}t||��qSr)�slice)r�i�jrrrr�sz$GroupBy.__init__.<locals>.<listcomp>zN`group` is required to be a coordinate if `group.name` is a dimension in `obj`cSsg|]}t||d��qS)r)r4)rr5rrrr�s���r7)$r+r3�ndim�
ValueError�getattr�hasattrr'�	TypeError�shape�get_axis_num�sizerZis_monotonicrZSeries�npZarange�groupby�firstZisnull�any�indexZdropnarrr4rr)r�obj�group�	group_dim�
group_indices�unique_coord�_groups�_full_index)�selfrErFZsqueezeZgrouperr3rGZ
expected_sizeZ
full_indexrD�sZfirst_itemsZbinsrHrIZ
unique_valuesrrr�__init__VsX



"zGroupBy.__init__cCs&|jdkr tt|jj|j��|_|jS)N)rJr-rrIrrH)rLrrrr�s

zGroupBy.groupscCs|jjS)N)rIr?)rLrrr�__len__�szGroupBy.__len__cCst|jj|j��S)N)rrIr�
_iter_grouped)rLrrr�__iter__�szGroupBy.__iter__ccs*x$|jD]}|jjf|j|i�VqWdS)z'Iterate over each element in this groupN)rHrEZiselrG)rL�indicesrrrrP�szGroupBy._iter_groupedcCs,|j|jkr|j}|j}n
|j}d}||fS)N)rGr'rFrHrI)rL�applied_example�
concat_dim�	positionsrrr�_infer_concat_args�szGroupBy._infer_concat_argscstj����fdd��}|S)Ncs.�s�n
�fdd�}|j||�}|j|�}|S)Ncs
�||�S)Nr)�x�y)�frr�<lambda>�sz2GroupBy._binary_op.<locals>.func.<locals>.<lambda>)�_yield_binary_applied�_concat)rL�otherr�applied�combined)rY�	reflexiverr�func�s
z GroupBy._binary_op.<locals>.func)�	functools�wraps)rYr`Zignored_kwargsrar)rYr`r�
_binary_op�szGroupBy._binary_opc	cs�d}d}x�|D]x\}}y|jf|jj|i�}d}WnBtk
rNtd��Yn(tk
rt|dkrlt|�}|}YnX|||�}|VqW|s�td|jj��dS)NFTzYGroupBy objects only support binary ops when the other argument is a Dataset or DataArrayz"no overlapping labels %r dimension)�selrFr)�AttributeErrorr<�KeyErrorr1r9)	rLrar]�dummyZfound_some_valuesZgroup_valuerEZ	other_sel�resultrrrr[�s"


zGroupBy._yield_binary_appliedcCs6|jdk	r2|jj|jkr2|jj|ji}|jf|�}|S)z�Our index contained empty groups (e.g., from a resampling). If we
        reduced on that dimension, we want to restore the full index.
        N)rKrFr)r'Zreindex)rLr_Zindexersrrr�_maybe_restore_empty_groups�sz#GroupBy._maybe_restore_empty_groupscCs
|j|�S)a�Fill missing values in this object by group.

        This operation follows the normal broadcasting and alignment rules that
        xarray uses for binary arithmetic, except the result is aligned to this
        object (``join='left'``) instead of aligned to the intersection of
        index coordinates (``join='inner'``).

        Parameters
        ----------
        value : valid type for the grouped object's fillna method
            Used to fill all matching missing values by group.

        Returns
        -------
        same type as the grouped object

        See also
        --------
        Dataset.fillna
        DataArray.fillna
        )Z_fillna)rL�valuerrr�fillna�szGroupBy.fillnacCs
|j|�S)a�Return an object of the same shape with all entries where cond is
        True and all other entries masked.

        This operation follows the normal broadcasting and alignment rules that
        xarray uses for binary arithmetic.

        Parameters
        ----------
        cond : DataArray or Dataset

        Returns
        -------
        same type as the grouped object

        See also
        --------
        Dataset.where
        )Z_where)rLZcondrrr�wheresz
GroupBy.wherecCs2t|jdttjf�r|jS|j||j||dd�S)NrT)�skipna�
keep_attrsZ
allow_lazy)r,rH�intr@�integerrE�reducerG)rL�oprnrorrr�_first_or_lastszGroupBy._first_or_lastTcCs|jtj||�S)zIReturn the first element of each group along the group dimension
        )rtrrB)rLrnrorrrrBsz
GroupBy.firstcCs|jtj||�S)zHReturn the last element of each group along the group dimension
        )rtr�last)rLrnrorrrru#szGroupBy.lastcs|j�fdd��S)zfAssign coordinates by group.

        See also
        --------
        Dataset.assign_coords
        cs|jf��S)N)�
assign_coords)�ds)�kwargsrrrZ/sz'GroupBy.assign_coords.<locals>.<lambda>)�apply)rLrxr)rxrrv(szGroupBy.assign_coords)FN)F)NT)NT)�__name__�
__module__�__qualname__�__doc__rN�propertyrrOrQrPrV�staticmethodrdr[rjrlrmrtrBrurvrrrrr2Fs"
L			

r2c@sFeZdZdZdd�Zdd�Zdd�Zdd	d
�Zddd�Zddd�Z	d
S)�DataArrayGroupByz=GroupBy object specialized to grouping DataArray objects
    ccs,|jj}x|jD]}||j|iVqWdS)zWFast version of `_iter_grouped` that yields Variables without
        metadata
        N)rE�variablerHrG)rL�varrRrrr�_iter_grouped_shortcut5sz'DataArrayGroupBy._iter_grouped_shortcutcCsFtj|||dd�}|jj|jj�|jj|�}t|dd�|j|j<|S)NT)�shortcut)�copy)	r
rr*�updaterEZ_replace_maybe_drop_dimsrZ_coordsr))rLr^rTrU�stackedrirrr�_concat_shortcut=sz!DataArrayGroupBy._concat_shortcutcs$�fdd�}t|j|d�}|j|�S)Ncs8|�jjkr�jj\}|�jjkr0�jj|�}nd}|S)Ng��.A)rFr)r'rEr>)Z	dimension�axis)rLrr�lookup_orderJs
z9DataArrayGroupBy._restore_dim_order.<locals>.lookup_order)�key)�sortedr'Z	transpose)rLr�r�Z	new_orderr)rLr�_restore_dim_orderIs	z#DataArrayGroupBy._restore_dim_orderFcsF|r|j�}n|j�}��fdd�|D�}|j||d�}|j|�}|S)aWApply a function over each array in the group and concatenate them
        together into a new array.

        `func` is called like `func(ar, *args, **kwargs)` for each array `ar`
        in this group.

        Apply uses heuristics (like `pandas.GroupBy.apply`) to figure out how
        to stack together the array. The rule is:
        1. If the dimension along which the group coordinate is defined is
           still in the first grouped array after applying `func`, then stack
           over this dimension.
        2. Otherwise, stack over the new dimension given by name of this
           grouping (the argument to the `groupby` function).

        Parameters
        ----------
        func : function
            Callable to apply to each array.
        shortcut : bool, optional
            Whether or not to shortcut evaluation under the assumptions that:
            (1) The action of `func` does not depend on any of the array
                metadata (attributes or coordinates) but only on the data and
                dimensions.
            (2) The action of `func` creates arrays with homogeneous metadata,
                that is, with the same dimensions and attributes.
            If these conditions are satisfied `shortcut` provides significant
            speedup. This should be the case for many common groupby operations
            (e.g., applying numpy ufuncs).
        **kwargs
            Used to call `func(ar, **kwargs)` for each array `ar`.

        Returns
        -------
        applied : DataArray
            The result of splitting, applying and combining this array.
        c3s |]}t|�|f���VqdS)N)r
)rZarr)rarxrrr&sz)DataArrayGroupBy.apply.<locals>.<genexpr>)r�)r�rPr\rj)rLrar�rxZgroupedr^r_rir)rarxrryVs%

zDataArrayGroupBy.applycCsZt|�\}}|j|�\}}|r.|j|||�}nt|||d�}t|t|j��rV|j|�}|S)N)rU)r	rVr�rr,�typerEr�)rLr^r�rSrTrUr_rrrr\�s
zDataArrayGroupBy._concatNTcs"�����fdd�}|j||d�S)a�Reduce the items in this group by applying `func` along some
        dimension(s).

        Parameters
        ----------
        func : function
            Function which can be called in the form
            `func(x, axis=axis, **kwargs)` to return the result of collapsing an
            np.ndarray over an integer valued axis.
        dim : str or sequence of str, optional
            Dimension(s) over which to apply `func`.
        axis : int or sequence of int, optional
            Axis(es) over which to apply `func`. Only one of the 'dimension'
            and 'axis' arguments can be supplied. If neither are supplied, then
            `func` is calculated over all dimension for each group item.
        keep_attrs : bool, optional
            If True, the datasets's attributes (`attrs`) will be copied from
            the original object to the new one.  If False (default), the new
            object will be returned without attributes.
        **kwargs : dict
            Additional keyword arguments passed on to `func`.

        Returns
        -------
        reduced : Array
            Array with summarized data and the indicated dimension(s)
            removed.
        cs|j���fd�i���S)Nro)rr)r)r��dimrarorxrr�reduce_array�sz-DataArrayGroupBy.reduce.<locals>.reduce_array)r�)ry)rLrar�r�ror�rxr�r)r�r�rarorxrrr�szDataArrayGroupBy.reduce)F)F)NNFT)
rzr{r|r}r�r�r�ryr\rrrrrrr�2s
.
r�c@s.eZdZdd�Zdd�Zddd�Zd	d
�ZdS)�DatasetGroupBycs<�jdd���fdd�|j�D�}|j|�}|j|�}|S)a�Apply a function over each Dataset in the group and concatenate them
        together into a new Dataset.

        `func` is called like `func(ds, *args, **kwargs)` for each dataset `ds`
        in this group.

        Apply uses heuristics (like `pandas.GroupBy.apply`) to figure out how
        to stack together the datasets. The rule is:
        1. If the dimension along which the group coordinate is defined is
           still in the first grouped item after applying `func`, then stack
           over this dimension.
        2. Otherwise, stack over the new dimension given by name of this
           grouping (the argument to the `groupby` function).

        Parameters
        ----------
        func : function
            Callable to apply to each sub-dataset.
        **kwargs
            Used to call `func(ds, **kwargs)` for each sub-dataset `ar`.

        Returns
        -------
        applied : Dataset
            The result of splitting, applying and combining this dataset.
        r�Nc3s|]}�|f��VqdS)Nr)rrw)rarxrrr&�sz'DatasetGroupBy.apply.<locals>.<genexpr>)�poprPr\rj)rLrarxr^r_rir)rarxrry�s


zDatasetGroupBy.applycCs,t|�\}}|j|�\}}t|||d�}|S)N)rU)r	rVr)rLr^rSrTrUr_rrrr\�szDatasetGroupBy._concatNFcs����fdd�}|j|�S)a�Reduce the items in this group by applying `func` along some
        dimension(s).

        Parameters
        ----------
        func : function
            Function which can be called in the form
            `func(x, axis=axis, **kwargs)` to return the result of collapsing an
            np.ndarray over an integer valued axis.
        dim : str or sequence of str, optional
            Dimension(s) over which to apply `func`.
        axis : int or sequence of int, optional
            Axis(es) over which to apply `func`. Only one of the 'dimension'
            and 'axis' arguments can be supplied. If neither are supplied, then
            `func` is calculated over all dimension for each group item.
        keep_attrs : bool, optional
            If True, the datasets's attributes (`attrs`) will be copied from
            the original object to the new one.  If False (default), the new
            object will be returned without attributes.
        **kwargs : dict
            Additional keyword arguments passed on to `func`.

        Returns
        -------
        reduced : Array
            Array with summarized data and the indicated dimension(s)
            removed.
        cs|j���f��S)N)rr)rw)r�rarorxrr�reduce_dataset�sz-DatasetGroupBy.reduce.<locals>.reduce_dataset)ry)rLrar�rorxr�r)r�rarorxrrr�szDatasetGroupBy.reducecs|j�fdd��S)zbAssign data variables by group.

        See also
        --------
        Dataset.assign
        cs|jf��S)N)�assign)rw)rxrrrZsz'DatasetGroupBy.assign.<locals>.<lambda>)ry)rLrxr)rxrr�szDatasetGroupBy.assign)NF)rzr{r|ryr\rrr�rrrrr��s!
!r�) rbZnumpyr@Zpandasr�r�combiner�commonrrrZpycompatr�utilsr	r
rr�rr
rrr!r1�objectr2r�Zinject_reduce_methodsZinject_binary_opsr�rrrr�<module>s(m

R