U
    +if`\                     @  s   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
 er\d dlmZmZ d dlmZ 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mZmZmZmZm Z m!Z!m"Z"m#Z# d dl$m%Z%m&Z& G dd de&Z'G dd de%e'Z(dS )    )annotations)dedent)TYPE_CHECKINGAnyCallable)AxisWindowingRankType)	DataFrameSeries)NDFrame)function)doc)BaseIndexerExpandingIndexerGroupbyIndexer)
_shared_docsargs_compatcreate_section_headerkwargs_compatnumba_notestemplate_headertemplate_returnstemplate_see_alsowindow_agg_numba_parameterswindow_apply_parameters)BaseWindowGroupbyRollingAndExpandingMixinc                      sb  e Zd ZU dZddddgZded< dddddd fddZddddZee	d e
de
dddd fddZeZeeedeed ed	d! d"d#d$d% fd&d'Zeeed(eedeed ed	d! d"d)d*d%
dd,d-d.d/d0d1d2 fd3d4Zeeed(ee eedeed eed5ed	d! d"d6d6d%d	d	d7d.d/d7 fd8d9Zeeed(ee eedeed eed5ed	d! d"d:d;d%d	d	d7d.d/d7 fd<d=Zeeed(ee eedeed eed5ed	d! d"d>d?d%d	d	d7d.d/d7 fd@dAZeeed(ee eedeed eed5ed	d! d"dBdBd%d	d	d7d.d/d7 fdCdDZeeed(e eedeed eed5ed	d! d"dEdEd%dd.d/d7 fdFdGZeeed(e
dHdIddeedJeedeed dKeed5e
dLdIddedMe
dNdIddd"dOdPd%dd	d	d7dd.d/dQ fdRdSZeeed(e
dHdIddeedJeedeed dTeed5e
dUdIddedMe
dVdIddd"dWdXd%dd	d	d7dd.d/dQ fdYdZZeeed(e
dHdIddeeedeed eed5d[edMe
d\dIddd"d]d^d%ddd_ fd`daZ eeed(eedeed dbeed5dcd"ddded% fdfdgZ!eeed(eedeed dheed5diedMe
djdIddd"dkdld% fdmdnZ"eeed(e
dodIddeedeed ed	d! d"dpdpd%ddrdds fdtduZ#eedved(e
dwdIddeedeed eedMe
dxdIddd"dydyd%dd|d-d-d} fd~dZ$eeed(e
ddIddeedeed ed	d! d"ddd%ddddd fddZ%eeed(e
ddIddeedeed e
ddIddeed5e
ddIddd"ddd%ddddd fddZ&  Z'S )	Expandinga  
    Provide expanding window calculations.

    Parameters
    ----------
    min_periods : int, default 1
        Minimum number of observations in window required to have a value;
        otherwise, result is ``np.nan``.

    center : bool, default False
        If False, set the window labels as the right edge of the window index.

        If True, set the window labels as the center of the window index.

        .. deprecated:: 1.1.0

    axis : int or str, default 0
        If ``0`` or ``'index'``, roll across the rows.

        If ``1`` or ``'columns'``, roll across the columns.

    method : str {'single', 'table'}, default 'single'
        Execute the rolling operation per single column or row (``'single'``)
        or over the entire object (``'table'``).

        This argument is only implemented when specifying ``engine='numba'``
        in the method call.

        .. versionadded:: 1.3.0

    Returns
    -------
    ``Expanding`` subclass

    See Also
    --------
    rolling : Provides rolling window calculations.
    ewm : Provides exponential weighted functions.

    Notes
    -----
    See :ref:`Windowing Operations <window.expanding>` for further usage details
    and examples.

    Examples
    --------
    >>> df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]})
    >>> df
         B
    0  0.0
    1  1.0
    2  2.0
    3  NaN
    4  4.0

    **min_periods**

    Expanding sum with 1 vs 3 observations needed to calculate a value.

    >>> df.expanding(1).sum()
         B
    0  0.0
    1  1.0
    2  3.0
    3  3.0
    4  7.0
    >>> df.expanding(3).sum()
         B
    0  NaN
    1  NaN
    2  3.0
    3  3.0
    4  7.0
    min_periodscenteraxismethodz	list[str]_attributes   Nr   singler   intr   str)objr   r    r!   c                   s   t  j||||||d d S )N)r'   r   r   r    r!   	selection)super__init__)selfr'   r   r   r    r!   r(   	__class__ O/home/mars/bis/venv/lib/python3.8/site-packages/pandas/core/window/expanding.pyr*   {   s    	zExpanding.__init__r   returnc                 C  s   t  S )z[
        Return an indexer class that will compute the window start and end bounds
        )r   r+   r.   r.   r/   _get_window_indexer   s    zExpanding._get_window_indexer	aggregatez
        See Also
        --------
        pandas.DataFrame.aggregate : Similar DataFrame method.
        pandas.Series.aggregate : Similar Series method.
        a  
        Examples
        --------
        >>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]})
        >>> df
           A  B  C
        0  1  4  7
        1  2  5  8
        2  3  6  9

        >>> df.ewm(alpha=0.5).mean()
                  A         B         C
        0  1.000000  4.000000  7.000000
        1  1.666667  4.666667  7.666667
        2  2.428571  5.428571  8.428571
        zSeries/Dataframe )Zsee_alsoZexamplesklassr    c                   s   t  j|f||S N)r)   r4   )r+   funcargskwargsr,   r.   r/   r4      s     zExpanding.aggregateZReturnszSee AlsoZ	expandingzcount of non NaN observationscount)Zwindow_methodZaggregation_descriptionZ
agg_methodc                   s
   t   S r7   )r)   r<   r2   r,   r.   r/   r<      s    zExpanding.countZ
Parameterszcustom aggregation functionapplyFzCallable[..., Any]boolz
str | Nonezdict[str, bool] | Noneztuple[Any, ...] | Nonezdict[str, Any] | None)r8   rawengineengine_kwargsr9   r:   c                   s   t  j||||||dS )N)r?   r@   rA   r9   r:   )r)   r=   )r+   r8   r?   r@   rA   r9   r:   r,   r.   r/   r=      s    zExpanding.applyZNotessumr@   rA   c                  s&   t d|| t j|||d|S )NrB   rC   )nvvalidate_expanding_funcr)   rB   r+   r@   rA   r9   r:   r,   r.   r/   rB      s    zExpanding.summaximummaxc                  s&   t d|| t j|||d|S )NrH   rC   )rD   rE   r)   rH   rF   r,   r.   r/   rH      s    zExpanding.maxminimumminc                  s&   t d|| t j|||d|S )NrJ   rC   )rD   rE   r)   rJ   rF   r,   r.   r/   rJ     s    zExpanding.minmeanc                  s&   t d|| t j|||d|S )NrK   rC   )rD   rE   r)   rK   rF   r,   r.   r/   rK   0  s    zExpanding.meanmedianc                   s   t  jf ||d|S )NrC   )r)   rL   )r+   r@   rA   r:   r,   r.   r/   rL   J  s    zExpanding.medianz
        ddof : int, default 1
            Delta Degrees of Freedom.  The divisor used in calculations
            is ``N - ddof``, where ``N`` represents the number of elements.

        
z1.4z/numpy.std : Equivalent method for NumPy array.
z
        The default ``ddof`` of 1 used in :meth:`Series.std` is different
        than the default ``ddof`` of 0 in :func:`numpy.std`.

        A minimum of one period is required for the rolling calculation.

        ZExamplesa  
        >>> s = pd.Series([5, 5, 6, 7, 5, 5, 5])

        >>> s.expanding(3).std()
        0         NaN
        1         NaN
        2    0.577350
        3    0.957427
        4    0.894427
        5    0.836660
        6    0.786796
        dtype: float64
        zstandard deviationstdddofr@   rA   c                  s(   t d|| t jf |||d|S )NrN   rO   )rD   rE   r)   rN   r+   rP   r@   rA   r9   r:   r,   r.   r/   rN   a  s    7  zExpanding.stdz/numpy.var : Equivalent method for NumPy array.
z
        The default ``ddof`` of 1 used in :meth:`Series.var` is different
        than the default ``ddof`` of 0 in :func:`numpy.var`.

        A minimum of one period is required for the rolling calculation.

        a  
        >>> s = pd.Series([5, 5, 6, 7, 5, 5, 5])

        >>> s.expanding(3).var()
        0         NaN
        1         NaN
        2    0.333333
        3    0.916667
        4    0.800000
        5    0.700000
        6    0.619048
        dtype: float64
        Zvariancevarc                  s(   t d|| t jf |||d|S )NrR   rO   )rD   rE   r)   rR   rQ   r,   r.   r/   rR     s    7  zExpanding.varz:A minimum of one period is required for the calculation.

z
        >>> s = pd.Series([0, 1, 2, 3])

        >>> s.expanding().sem()
        0         NaN
        1    0.707107
        2    0.707107
        3    0.745356
        dtype: float64
        zstandard error of meansem)rP   c                   s   t  jf d|i|S )NrP   )r)   rS   )r+   rP   r9   r:   r,   r.   r/   rS     s    $zExpanding.semz:scipy.stats.skew : Third moment of a probability density.
zDA minimum of three periods is required for the rolling calculation.
zunbiased skewnessskewc                   s   t  jf |S r7   )r)   rT   r+   r:   r,   r.   r/   rT     s    zExpanding.skewz/scipy.stats.kurtosis : Reference SciPy method.
z<A minimum of four periods is required for the calculation.

a[  
        The example below will show a rolling calculation with a window size of
        four matching the equivalent function call using `scipy.stats`.

        >>> arr = [1, 2, 3, 4, 999]
        >>> import scipy.stats
        >>> print(f"{{scipy.stats.kurtosis(arr[:-1], bias=False):.6f}}")
        -1.200000
        >>> print(f"{{scipy.stats.kurtosis(arr, bias=False):.6f}}")
        4.999874
        >>> s = pd.Series(arr)
        >>> s.expanding(4).kurt()
        0         NaN
        1         NaN
        2         NaN
        3   -1.200000
        4    4.999874
        dtype: float64
        z,Fisher's definition of kurtosis without biaskurtc                   s   t  jf |S r7   )r)   rV   rU   r,   r.   r/   rV     s    &zExpanding.kurta  
        quantile : float
            Quantile to compute. 0 <= quantile <= 1.
        interpolation : {{'linear', 'lower', 'higher', 'midpoint', 'nearest'}}
            This optional parameter specifies the interpolation method to use,
            when the desired quantile lies between two data points `i` and `j`:

                * linear: `i + (j - i) * fraction`, where `fraction` is the
                  fractional part of the index surrounded by `i` and `j`.
                * lower: `i`.
                * higher: `j`.
                * nearest: `i` or `j` whichever is nearest.
                * midpoint: (`i` + `j`) / 2.
        quantilelinearfloatrW   interpolationc                   s   t  jf ||d|S )NrZ   )r)   rW   )r+   rW   r[   r:   r,   r.   r/   rW   9  s    "zExpanding.quantilez.. versionadded:: 1.4.0 

a  
        method : {{'average', 'min', 'max'}}, default 'average'
            How to rank the group of records that have the same value (i.e. ties):

            * average: average rank of the group
            * min: lowest rank in the group
            * max: highest rank in the group

        ascending : bool, default True
            Whether or not the elements should be ranked in ascending order.
        pct : bool, default False
            Whether or not to display the returned rankings in percentile
            form.
        a+  
        >>> s = pd.Series([1, 4, 2, 3, 5, 3])
        >>> s.expanding().rank()
        0    1.0
        1    2.0
        2    2.0
        3    3.0
        4    5.0
        5    3.5
        dtype: float64

        >>> s.expanding().rank(method="max")
        0    1.0
        1    2.0
        2    2.0
        3    3.0
        4    5.0
        5    4.0
        dtype: float64

        >>> s.expanding().rank(method="min")
        0    1.0
        1    2.0
        2    2.0
        3    3.0
        4    5.0
        5    3.0
        dtype: float64
        rankaverageTr   r!   	ascendingpctc                   s   t  jf |||d|S )Nr^   )r)   r\   )r+   r!   r_   r`   r:   r,   r.   r/   r\   a  s    DzExpanding.ranka   
        other : Series or DataFrame, optional
            If not supplied then will default to self and produce pairwise
            output.
        pairwise : bool, default None
            If False then only matching columns between self and other will be
            used and the output will be a DataFrame.
            If True then all pairwise combinations will be calculated and the
            output will be a MultiIndexed DataFrame in the case of DataFrame
            inputs. In the case of missing elements, only complete pairwise
            observations will be used.
        ddof : int, default 1
            Delta Degrees of Freedom.  The divisor used in calculations
            is ``N - ddof``, where ``N`` represents the number of elements.
        zsample covariancecovzDataFrame | Series | Nonezbool | NoneotherpairwiserP   c                   s   t  jf |||d|S Nrb   )r)   ra   r+   rc   rd   rP   r:   r,   r.   r/   ra     s    $zExpanding.covaN  
        other : Series or DataFrame, optional
            If not supplied then will default to self and produce pairwise
            output.
        pairwise : bool, default None
            If False then only matching columns between self and other will be
            used and the output will be a DataFrame.
            If True then all pairwise combinations will be calculated and the
            output will be a MultiIndexed DataFrame in the case of DataFrame
            inputs. In the case of missing elements, only complete pairwise
            observations will be used.
        z
        cov : Similar method to calculate covariance.
        numpy.corrcoef : NumPy Pearson's correlation calculation.
        an  
        This function uses Pearson's definition of correlation
        (https://en.wikipedia.org/wiki/Pearson_correlation_coefficient).

        When `other` is not specified, the output will be self correlation (e.g.
        all 1's), except for :class:`~pandas.DataFrame` inputs with `pairwise`
        set to `True`.

        Function will return ``NaN`` for correlations of equal valued sequences;
        this is the result of a 0/0 division error.

        When `pairwise` is set to `False`, only matching columns between `self` and
        `other` will be used.

        When `pairwise` is set to `True`, the output will be a MultiIndex DataFrame
        with the original index on the first level, and the `other` DataFrame
        columns on the second level.

        In the case of missing elements, only complete pairwise observations
        will be used.
        Zcorrelationcorrc                   s   t  jf |||d|S re   )r)   rg   rf   r,   r.   r/   rg     s    ?zExpanding.corr)r#   Nr   r$   N)FNNNN)NN)r#   )r#   )r#   )rX   )r]   TF)NNr#   )NNr#   )(__name__
__module____qualname____doc__r"   __annotations__r*   r3   r   r   r   r4   Zaggr   r   r   r   r<   r   r=   r   r   r   r   rB   rH   rJ   rK   rL   replacerN   rR   rS   rT   rV   rW   r\   ra   rg   __classcell__r.   r.   r,   r/   r   -   s@  
K     


      








        1       1     #  %  
     ?     
   	      :   r   c                   @  s*   e Zd ZdZejej ZddddZdS )ExpandingGroupbyz5
    Provide a expanding groupby implementation.
    r   r0   c                 C  s   t | jjtd}|S )z
        Return an indexer class that will compute the window start and end bounds

        Returns
        -------
        GroupbyIndexer
        )Zgroupby_indiceswindow_indexer)r   _grouperindicesr   )r+   rp   r.   r.   r/   r3     s
    z$ExpandingGroupby._get_window_indexerN)rh   ri   rj   rk   r   r"   r   r3   r.   r.   r.   r/   ro     s   ro   N))
__future__r   textwrapr   typingr   r   r   Zpandas._typingr   r   Zpandasr	   r
   Zpandas.core.genericr   Zpandas.compat.numpyr   rD   Zpandas.util._decoratorsr   Zpandas.core.indexers.objectsr   r   r   Zpandas.core.window.docr   r   r   r   r   r   r   r   r   r   Zpandas.core.window.rollingr   r   r   ro   r.   r.   r.   r/   <module>   s$   0     l