a
    DBìj‘9  ã                   @  sò   d Z ddlmZ ddlmZ ddlZddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZ dZG dd„ dƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )zLIndexer objects for computing start/end window bounds for rolling operationsé    )Úannotations)Ú	timedeltaN)Ú
BaseOffset)Ú calculate_variable_window_bounds)ÚAppender)Úensure_platform_int)ÚDatetimeIndex)ÚNanoa¾  
Computes the bounds of a window.

Parameters
----------
num_values : int, default 0
    number of values that will be aggregated over
window_size : int, default 0
    the number of rows in a window
min_periods : int, default None
    min_periods passed from the top level rolling API
center : bool, default None
    center passed from the top level rolling API
closed : str, default None
    closed passed from the top level rolling API
step : int, default None
    step passed from the top level rolling API
    .. versionadded:: 1.5
win_type : str, default None
    win_type passed from the top level rolling API

Returns
-------
A tuple of ndarray[int64]s, indicating the boundaries of each
window
c                	   @  sF   e Zd ZdZdddddœdd	„Zeeƒddd
ddd
ddœdd„ƒZdS )ÚBaseIndexeraà  
    Base class for window bounds calculations.

    Examples
    --------
    >>> from pandas.api.indexers import BaseIndexer
    >>> class CustomIndexer(BaseIndexer):
    ...     def get_window_bounds(self, num_values, min_periods, center, closed, step):
    ...         start = np.empty(num_values, dtype=np.int64)
    ...         end = np.empty(num_values, dtype=np.int64)
    ...         for i in range(num_values):
    ...             start[i] = i
    ...             end[i] = i + self.window_size
    ...         return start, end
    >>> df = pd.DataFrame({"values": range(5)})
    >>> indexer = CustomIndexer(window_size=2)
    >>> df.rolling(indexer).sum()
        values
    0	1.0
    1	3.0
    2	5.0
    3	7.0
    4	4.0
    Nr   únp.ndarray | NoneÚintÚNone)Úindex_arrayÚwindow_sizeÚreturnc                 K  s.   || _ || _| ¡ D ]\}}t| ||ƒ qd S ©N)r   r   ÚitemsÚsetattr)Úselfr   r   ÚkwargsÚkeyÚvalue© r   ú[/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pandas/core/indexers/objects.pyÚ__init__H   s    zBaseIndexer.__init__ú
int | Noneúbool | Noneú
str | Noneútuple[np.ndarray, np.ndarray]©Ú
num_valuesÚmin_periodsÚcenterÚclosedÚstepr   c                 C  s   t ‚d S r   )ÚNotImplementedError©r   r    r!   r"   r#   r$   r   r   r   Úget_window_boundsQ   s    	zBaseIndexer.get_window_bounds)Nr   )r   NNNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Úget_window_bounds_docr'   r   r   r   r   r
   .   s    ÿ	     úr
   c                	   @  s2   e Zd ZdZeeƒdddddddd	œd
d„ƒZdS )ÚFixedWindowIndexerz3Creates window boundaries that are of fixed length.r   Nr   r   r   r   r   r   c           	      C  sŽ   |s| j dkr| j d d }nd}tjd| |d | |dd�}|| j  }|dv rZ|d8 }|dv rj|d8 }t |d|¡}t |d|¡}||fS )Nr   é   é   Úint64©Zdtype©ÚleftÚboth)r3   Zneither)r   ÚnpÚarangeÚclip)	r   r    r!   r"   r#   r$   ÚoffsetÚendÚstartr   r   r   r'   `   s    	
z$FixedWindowIndexer.get_window_bounds)r   NNNN©r(   r)   r*   r+   r   r,   r'   r   r   r   r   r-   ]   s        úr-   c                	   @  s2   e Zd ZdZeeƒdddddddd	œd
d„ƒZdS )ÚVariableWindowIndexerzNCreates window boundaries that are of variable length, namely for time series.r   Nr   r   r   r   r   r   c                 C  s   t || j|||| jƒS r   )r   r   r   r&   r   r   r   r'   ~   s    úz'VariableWindowIndexer.get_window_bounds)r   NNNNr;   r   r   r   r   r<   {   s        úr<   c                	      sR   e Zd ZdZddddddd	œ‡ fd
d„Zeeƒddddddddœdd„ƒZ‡  ZS )ÚVariableOffsetWindowIndexeraP  
    Calculate window boundaries based on a non-fixed offset such as a BusinessDay.

    Examples
    --------
    >>> from pandas.api.indexers import VariableOffsetWindowIndexer
    >>> df = pd.DataFrame(range(10), index=pd.date_range("2020", periods=10))
    >>> offset = pd.offsets.BDay(1)
    >>> indexer = VariableOffsetWindowIndexer(index=df.index, offset=offset)
    >>> df
                0
    2020-01-01  0
    2020-01-02  1
    2020-01-03  2
    2020-01-04  3
    2020-01-05  4
    2020-01-06  5
    2020-01-07  6
    2020-01-08  7
    2020-01-09  8
    2020-01-10  9
    >>> df.rolling(indexer).sum()
                   0
    2020-01-01   0.0
    2020-01-02   1.0
    2020-01-03   2.0
    2020-01-04   3.0
    2020-01-05   7.0
    2020-01-06  12.0
    2020-01-07   6.0
    2020-01-08   7.0
    2020-01-09   8.0
    2020-01-10   9.0
    Nr   r   r   zDatetimeIndex | NonezBaseOffset | Noner   )r   r   Úindexr8   r   c                   sJ   t ƒ j||fi |¤Ž t|tƒs(tdƒ‚|| _t|tƒs@tdƒ‚|| _d S )Nzindex must be a DatetimeIndex.z(offset must be a DateOffset-like object.)Úsuperr   Ú
isinstancer   Ú
ValueErrorr>   r   r8   )r   r   r   r>   r8   r   ©Ú	__class__r   r   r   ¹   s    

z$VariableOffsetWindowIndexer.__init__r   r   r   r   r   c                 C  sä  |d urt dƒ‚|dkr4tjddd�tjddd�fS |d u rN| jd urJdnd}|dv }|dv }| j|d	  | jd k r|d
}nd	}|| j }	tj|dd�}
|
 d
¡ tj|dd�}| d
¡ d|
d< |rÐd	|d< nd|d< tdƒ}td	|ƒD ]ð}| j| }||	 }|�r|td	ƒ8 }||
|< t|
|d	  |ƒD ]0}| j| | | }||k�r,||
|<  �q^�q,| j||d	   | | }||k�rž|�sž||d	  d	 ||< n(||k�r¶|d	 ||< n||d	  ||< |sê||  d	8  < qê|
|fS )Nz/step not implemented for variable offset windowr   r0   r1   Úrightr4   )rD   r4   r2   r.   éÿÿÿÿ)	r%   r5   Úemptyr>   r8   Úfillr   Úranger	   )r   r    r!   r"   r#   r$   Zright_closedZleft_closedZindex_growth_signZoffset_diffr:   r9   ÚzeroÚiZ	end_boundZstart_boundÚjZ
start_diffZend_diffr   r   r   r'   É   sR    	







z-VariableOffsetWindowIndexer.get_window_bounds)Nr   NN)r   NNNN)	r(   r)   r*   r+   r   r   r,   r'   Ú__classcell__r   r   rB   r   r=   •   s   %    û     úr=   c                	   @  s2   e Zd ZdZeeƒdddddddd	œd
d„ƒZdS )ÚExpandingIndexerz;Calculate expanding window bounds, mimicking df.expanding()r   Nr   r   r   r   r   r   c                 C  s&   t j|t jd�t jd|d t jd�fS )Nr1   r.   )r5   Zzerosr0   r6   r&   r   r   r   r'     s    
þz"ExpandingIndexer.get_window_bounds)r   NNNNr;   r   r   r   r   rM     s        úrM   c                	   @  s2   e Zd ZdZeeƒdddddddd	œd
d„ƒZdS )ÚFixedForwardWindowIndexera¿  
    Creates window boundaries for fixed-length windows that include the current row.

    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

    >>> indexer = pd.api.indexers.FixedForwardWindowIndexer(window_size=2)
    >>> df.rolling(window=indexer, min_periods=1).sum()
         B
    0  1.0
    1  3.0
    2  2.0
    3  4.0
    4  4.0
    r   Nr   r   r   r   r   r   c                 C  s`   |rt dƒ‚|d urt dƒ‚|d u r(d}tjd||dd�}|| j }| jrXt |d|¡}||fS )Nz.Forward-looking windows can't have center=TruezAForward-looking windows don't support setting the closed argumentr.   r   r0   r1   )rA   r5   r6   r   r7   )r   r    r!   r"   r#   r$   r:   r9   r   r   r   r'   B  s    	ÿ
z+FixedForwardWindowIndexer.get_window_bounds)r   NNNNr;   r   r   r   r   rN   )  s        úrN   c                	      s^   e Zd ZdZdddedfddddddd	œ‡ fd
d„Zeeƒddddddddœdd„ƒZ‡  Z	S )ÚGroupbyIndexerzMCalculate bounds to compute groupby rolling, mimicking df.groupby().rolling()Nr   r   zint | BaseIndexerzdict | Noneztype[BaseIndexer]r   )r   r   Úgroupby_indicesÚwindow_indexerÚindexer_kwargsr   c                   sH   |pi | _ || _|r| ¡ ni | _tƒ jf || j d|¡dœ|¤Ž dS )a4  
        Parameters
        ----------
        index_array : np.ndarray or None
            np.ndarray of the index of the original object that we are performing
            a chained groupby operation over. This index has been pre-sorted relative to
            the groups
        window_size : int or BaseIndexer
            window size during the windowing operation
        groupby_indices : dict or None
            dict of {group label: [positional index of rows belonging to the group]}
        window_indexer : BaseIndexer
            BaseIndexer class determining the start and end bounds of each group
        indexer_kwargs : dict or None
            Custom kwargs to be passed to window_indexer
        **kwargs :
            keyword arguments that will be available when get_window_bounds is called
        r   ©r   r   N)rP   rQ   ÚcopyrR   r?   r   Úpop)r   r   r   rP   rQ   rR   r   rB   r   r   r   _  s    
þýzGroupbyIndexer.__init__r   r   r   r   r   r   c                 C  sZ  g }g }d}| j  ¡ D ]ø\}	}
| jd ur:| j t|
ƒ¡}n| j}| jf || jdœ| j¤Ž}| t	|
ƒ||||¡\}}| 
tj¡}| 
tj¡}t	|ƒt	|ƒks¤J dƒ‚t ||t	|
ƒ ¡}|t	|
ƒ7 }t ||d d g¡j
tjdd�}| | t|ƒ¡¡ | | t|ƒ¡¡ qt	|ƒdk�r>tjg tjd�tjg tjd�fS t |¡}t |¡}||fS )	Nr   rS   z6these should be equal in length from get_window_boundsrE   r.   F)rT   r1   )rP   r   r   Ztaker   rQ   r   rR   r'   ÚlenZastyper5   r0   r6   ÚappendÚarrayZconcatenate)r   r    r!   r"   r#   r$   Zstart_arraysZ
end_arraysZwindow_indices_startr   Úindicesr   Zindexerr:   r9   Zwindow_indicesr   r   r   r'   ƒ  sJ    
þýÿÿþÿÿ 

z GroupbyIndexer.get_window_bounds)r   NNNN)
r(   r)   r*   r+   r
   r   r   r,   r'   rL   r   r   rB   r   rO   \  s   ú$     úrO   c                	   @  s2   e Zd ZdZeeƒdddddddd	œd
d„ƒZdS )ÚExponentialMovingWindowIndexerz/Calculate ewm window bounds (the entire window)r   Nr   r   r   r   r   r   c                 C  s$   t jdgt jd�t j|gt jd�fS )Nr   r1   )r5   rX   r0   r&   r   r   r   r'   ¼  s    	z0ExponentialMovingWindowIndexer.get_window_bounds)r   NNNNr;   r   r   r   r   rZ   ¹  s        úrZ   )r+   Ú
__future__r   Údatetimer   Únumpyr5   Zpandas._libs.tslibsr   Zpandas._libs.window.indexersr   Zpandas.util._decoratorsr   Zpandas.core.dtypes.commonr   Zpandas.core.indexes.datetimesr   Zpandas.tseries.offsetsr	   r,   r
   r-   r<   r=   rM   rN   rO   rZ   r   r   r   r   Ú<module>   s&   / 3]