
    >ieU                       d dl mZ d dl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 d
lmZ d dlmZmZmZmZmZmZmZmZ d dlm Z  d dl!m"Z" d dl#m$Z$m%Z%m&Z&m'Z' d dl(m)Z) d dl*m+Z+ d dl,m-Z- d dl.m/Z/m0Z0m1Z1m2Z2m3Z3  e4de5fi           Z6d Z7d Z8e$e-ddddd            Z9d Z:d Z;d Z<d Z= G d d          Z
 G d d e
          Z>dS )!    )annotationsN)Integral)is_datetime64_any_dtype)Rolling)normalize_arg)tokenize)BlockwiseDepDict)methods)check_axis_keyword_deprecation)Scalar_Frame_get_divisions_map_partitions_get_meta_map_partitions_maybe_from_pandasapply_and_enforcenew_dd_objectpartitionwise_graph)from_pandas)_maybe_align_partitions)insert_meta_param_descriptionis_dask_collectionis_dataframe_likeis_series_like)unpack_collections)HighLevelGraph)
no_default)Mapplyderived_fromfuncnamehas_keywordCombinedOutputc                   d}| 5t          |t                    r | j        d         |k    rt          |          |5t          |t                    r |j        d         |k    rt          |          d | ||fD             }t	          j        |          }t          || t          |           nd |t          |          nd f          S )NzqPartition size is less than overlapping window size. Try using ``df.repartition`` to increase the partition size.r   c                    g | ]}||S N ).0ps     6lib/python3.11/site-packages/dask/dataframe/rolling.py
<listcomp>z#_combined_parts.<locals>.<listcomp>8   s    NNN1Q    )
isinstancer   shapeNotImplementedErrorr
   concatr"   len)	prev_partcurrent_part	next_partbeforeaftermsgpartscombineds           r)   _combined_partsr9   )   s    	*  FH!=!=?1''%c***E8!<!<?1&&%c***NNL)<NNNE~e$$H'3C	NNN'3C	NNN	
  r+   c                   d |D             }|d         \  }}}d |D             } | |i |}	|d }t          |t          j                  r|}d }
|j        d         dk    r|	j        d         |j        d         z  }
|r|
r||
z  }||	j        |d          S t          |t          j                  r|}|r|
r||
z  }|	j        ||          S )Nc                <    g | ]}t          |t                    |S r&   r,   r"   r'   dfs     r)   r*   z!overlap_chunk.<locals>.<listcomp>E   s'    
?
?
?"
2~ > >
?2
?
?
?r+   r   c                L    g | ]!}t          |t                    r|d          n|"S r   r<   r'   args     r)   r*   z!overlap_chunk.<locals>.<listcomp>H   s/    OOO3jn55>CFF3OOOr+   )r,   datetime	timedeltar-   iloc)funcr4   r5   argskwargsdfsr8   prev_part_lengthnext_part_lengthout	expansions              r)   overlap_chunkrN   D   s   
?
?
?
?
?C36q60H 0OO$OOOD
$


C&(,-- "!I~aAIaLHN1$55	 ) )x  %+,, !   8FE6M""r+   T)metaenforce_metadatatransform_divisionsalign_dataframesc               	   t          |          st          |          rt          |          st          |d          n|}|f|z   }t	          t
                    rt          j                  t	          t
                    rt          j                  t	          t          j	                  st	          t          j	                  r.t          |j        j        j                  st          d          nEt	          t                    r!dk    rt	          t                    rdk    st!          d          |	                    dd          }
|	                    dd          }t%          |           sJ |
t'          g|R i |	}n%dt)          |           z   }
t'          | g|R i |	}|
 d	| }
|rEt+          |          }	 t-          |          }n%# t           $ r}t!          | d
          |d}~ww xY wd |D             }t/          ||| |	|          t1          d |D                       rH|
dft2          | t4          d |D             f|	fi}t7          j        |
||          }t;          ||
          S g }g }t=          |||| ||	          fd}|D ]}t	          |t>                    r6 ||          }|                     |           |                     |           MtC          |          }tE          |          \  }}|r+|                     |           |#                    |           |                     |           i }d}|	$                                D ]D\  }}tC          |          }tE          |          \  }}|#                    |           |||<   |rd}EtK          | d          rKd tM          dd                   D             }|'                    dtQ          |                     | fd} |r#tS          tT          |
| g|R |tV          d|}n#|r|	n|}tS          tV          |
| g|R i |d|i}t7          j        |
||          }tY          ||
          S )a]	  Apply a function to each partition, sharing rows with adjacent partitions.

    Parameters
    ----------
    func : function
        The function applied to each partition. If this function accepts
        the special ``partition_info`` keyword argument, it will recieve
        information on the partition's relative location within the
        dataframe.
    df: dd.DataFrame, dd.Series
    args, kwargs :
        Positional and keyword arguments to pass to the function.
        Positional arguments are computed on a per-partition basis, while
        keyword arguments are shared across all partitions. The partition
        itself will be the first positional argument, with all other
        arguments passed *after*. Arguments can be ``Scalar``, ``Delayed``,
        or regular Python objects. DataFrame-like args (both dask and
        pandas) will be repartitioned to align (if necessary) before
        applying the function; see ``align_dataframes`` to control this
        behavior.
    enforce_metadata : bool, default True
        Whether to enforce at runtime that the structure of the DataFrame
        produced by ``func`` actually matches the structure of ``meta``.
        This will rename and reorder columns for each partition,
        and will raise an error if this doesn't work,
        but it won't raise if dtypes don't match.
    before : int, timedelta or string timedelta
        The rows to prepend to partition ``i`` from the end of
        partition ``i - 1``.
    after : int, timedelta or string timedelta
        The rows to append to partition ``i`` from the beginning
        of partition ``i + 1``.
    transform_divisions : bool, default True
        Whether to apply the function onto the divisions and apply those
        transformed divisions to the output.
    align_dataframes : bool, default True
        Whether to repartition DataFrame- or Series-like args
        (both dask and pandas) so their divisions align before applying
        the function. This requires all inputs to have known divisions.
        Single-partition inputs will be split into multiple partitions.

        If False, all inputs must have either the same number of partitions
        or a single partition. Single-partition inputs will be broadcast to
        every partition of multi-partition inputs.
    $META

    See Also
    --------
    dd.DataFrame.map_overlap
       zMMust have a `DatetimeIndex` when using string offset for `before` and `after`r   z*before and after must be positive integerstokenNparent_metazoverlap--zx. If you don't want the partitions to be aligned, and are calling `map_overlap` directly, pass `align_dataframes=False`.c                <    g | ]}t          |t                    |S r&   )r,   r   r=   s     r)   r*   zmap_overlap.<locals>.<listcomp>   s'    
7
7
7"
2v 6 6
72
7
7
7r+   c              3  @   K   | ]}t          |t                    V  d S r%   )r,   r   rA   s     r)   	<genexpr>zmap_overlap.<locals>.<genexpr>   s,      
3
3s:c6""
3
3
3
3
3
3r+   c                     g | ]}|j         d fS r@   )_namerA   s     r)   r*   zmap_overlap.<locals>.<listcomp>   s    888C#)Q888r+   dependenciesc                   i }t          |           \  }}|                    |           t          |           \  }}|                    |           dt          |           z   }t	          t          ||                                 |                    D ]\  }\  }}	}
||f}t          ||	|
f||<   t          j	        ||| g          }t          ||          S )Nzoverlap-concat-r]   )_get_previous_partitionsupdate_get_nexts_partitionsr   	enumeratezip__dask_keys__r9   r   from_collectionsr   )rB   dskprevs_parts_dskprevsnexts_parts_dsknextsname_aiprevcurrentnextkeygraphr5   r4   	divisionsrO   s                r)   _handle_frame_argumentz+map_overlap.<locals>._handle_frame_argument   s    !9#v!F!F

?###!6sE!B!B

?###"Xc]]2(1s((**E22)
 )
 	M 	M$A$gt 1+C'wfeLCHH/3%PPPUFD)<<<r+   TFpartition_infoc                "    i | ]\  }}|f||d S ))numberdivisionr&   )r'   rm   rx   s      r)   
<dictcomp>zmap_overlap.<locals>.<dictcomp>  s8     
 
 
8 DQH55
 
 
r+   c                     |i |d| iS )Nru   r&   )ru   rG   rH   	orig_funcs      r)   rF   zmap_overlap.<locals>.func  s#    9dLfLL^LLLLr+   )r^   _func_metar^   )-r   r   r   r   r,   strpdto_timedeltarC   rD   r   index_meta_nonemptyinferred_type	TypeErrorr   
ValueErrorpopcallabler   r    r   r   r   allr   tupler   rf   r   r   r   appendr   r   extenditemsr!   rc   insertr	   r   r   rN   r   ) rF   r>   r4   r5   rO   rP   rQ   rR   rG   rH   namerV   rU   erI   layerrr   args2r^   rt   rB   arg2collectionskwargs3simplekvru   rg   kwargs4rs   r|   s      ```                         @@r)   map_overlapr   _   s   D 2	"3B"7"7	ASTVAWAW	B  54<D&# )((% '&&&(,-- KE8CU1V1V K&rx'>'LMM 	+  	 vx((	K!5(++ 

IJJJ::gt$$D**]D11KD>>vu>t>>>v>>HTNN*tVUDTDDDVDDUD !$''	*400DD 	 	 	 Q Q Q  	 8
7
7
7
7C#D#tVT;OOD

3
3d
3
3
333 
)1I8848889	
 /e$OOOeT4(((EL--sD$ I= = = = = = = =&   c6"" 	((--CLL$$$C  .s33k 	LL,,,,LLGF  1!+A..;K(((
 	F4)** 
M
 
(3B388
 
 

 	Q(88999		M 	M 	M 	M 	M  
!
 
 
 &
 
 
 
 #/&&!	
 	
 	
 	
 	
 	
 &	
 	
 	
 +D#LQQQEdI666s   G$ $
H.HHc                   i }| j         }d}dt          | |          z   }|rrt          |t                    r]g }t	          d| j                  D ]/}||f}t          j        ||f|f||<   |                    |           0|                    d           nt          |t          j
                  rt          j        | j                                                  j        dd         }	||	k                                    rt#          |          g }t	          d| j                  D ]0}||f}t$          ||dz
  f||f|f||<   |                    |           1|                    d           ndg| j        z  }||fS )zE
    Helper to get the nexts partitions required for the overlap
    ziPartition size is less than specified window. Try using ``df.repartition`` to increase the partition sizezoverlap-append-rT   Nrz   r   )r\   r   r,   r   rangenpartitionsr   headr   rC   rD   r   Seriesrs   diffrE   anyr   _head_timedelta)
r>   r5   rg   df_nametimedelta_partition_messagename_brk   rm   rq   deltass
             r)   rb   rb   <  s    ChG	F  
 "e!4!44F (E8,, (q".)) 	 	A1+C!e4CHLLT	E8-	.	. (2<((--//4QrT:FN!! 	:8999q".)) 	 	A1+C''1q5)9GQ<OCHLLT':r+   c                   i }| j         dt          | |          z   }|rat          |t                    rLdg}t	          | j        dz
            D ]/}||f}t          j        |f|f||<   |                    |           0nt          |t          j
                  r^t          j        | j                  }|                                j        dd         }||k                                    r|d         }	dg}t	          | j        dz
            D ]}||dz            }
t#          |
|z
  |	          }||         |}}||k    r"|dk    r|||         z
  }|dz
  }||k    r|dk    ||f}t$          fdt	          ||dz             D             |dz   f|f||<   |                    |           nXdg}t	          | j        dz
            D ]1}||f}t$          |fg|dz   f|f||<   |                    |           2ndg| j        z  }||fS )zH
    Helper to get the previous partitions required for the overlap
    zoverlap-prepend-NrT   rz   r   c                    g | ]}|fS r&   r&   )r'   r   r   s     r)   r*   z,_get_previous_partitions.<locals>.<listcomp>  s    ;;;agq\;;;r+   )r\   r   r,   r   r   r   r   tailr   rC   rD   r   r   rs   r   rE   r   max_tail_timedelta)r>   r4   rg   rl   ri   rm   rq   divsr   pt_zpt_ilbfirstjr   s                 @r)   r`   r`   a  s    ChG(2v"6"66F 5(*VX.. 5(r~)** 	 	A1+C!f5CHLL	
 
FH.	/	/ .(y&&!!B$' VO  "" #	"7DFE2>A-.. " " AE{ --7AqbjjQUU!F1I-EAA bjjQUU qk#;;;;5AE??;;;a!e$	C S!!!!)". FE2>A-.. " "qk#q\Na!e$	C S!!!!" ':r+   c                X    ||j         | j                                         |z   k              S )zReturn rows of ``next_`` whose index is before the last
    observation in ``current`` + ``after``.

    Parameters
    ----------
    current : DataFrame
    next_ : DataFrame
    after : timedelta

    Returns
    -------
    overlapped : DataFrame
    )r   r   )ro   next_r5   s      r)   r   r     s)      1 1 3 3e ;<==r+   c                L    t          j        fd| D                       }|S )a4  Return the concatenated rows of each dataframe in ``prevs`` whose
    index is after the first observation in ``current`` - ``before``.

    Parameters
    ----------
    current : DataFrame
    prevs : list of DataFrame objects
    before : timedelta

    Returns
    -------
    overlapped : DataFrame
    c                f    g | ]-}||j         j                                         z
  k             .S r&   )r   min)r'   rn   r4   ro   s     r)   r*   z#_tail_timedelta.<locals>.<listcomp>  s8    MMMtdjGM--//&89	:MMMr+   )r
   r/   )ri   ro   r4   selecteds    `` r)   r   r     s8     ~MMMMMuMMM H Or+   c                     e Zd ZdZddddefdZd Zed             Ze	d             Z
d Z ee          d	             Z ee          d
             Z ee          d             Z ee          d             Z ee          d             Z ee          d             Z ee          d             Z ee          dd            Z ee          dd            Z ee          d             Z ee          d             Z ee          d             Z ee          	 	 	 	 	 dd            Z ee          d             ZeZd ZdS )r   z%Provides rolling window calculations.NFc                    || _         || _        || _        || _        || _        || _         |j        j        di |                                  t          | j        t                    rd nd| _        d S )Nfreqr&   )objwindowmin_periodscenteraxiswin_typer~   rolling_rolling_kwargsr,   int	_win_type)selfr   r   r   r   r   r   s          r)   __init__zRolling.__init__  sx     &	 	33D0022333 ",DK!=!=I6r+   c                l    | j         | j        | j        | j        d}| j        t
          ur
| j        |d<   |S )N)r   r   r   r   r   )r   r   r   r   r   r   )r   rH   s     r)   r   zRolling._rolling_kwargs  sB    k+k	
 
 9J&&!YF6Nr+   c                ~    | j         dv p4t          | j        t                    o
| j        dk    p| j        j        dk    S )zm
        Indicator for whether the object has a single partition (True)
        or multiple (False).
        )rT   columnsrT   )r   r,   r   r   r   r   r   s    r)   _has_single_partitionzRolling._has_single_partition  sE     I' )4;11FdkQ6F)x#q(	
r+   c                    t                      5   | j        di |}d d d            n# 1 swxY w Y    t          ||          |i |S )Nr&   )r   r   getattr)r>   rolling_kwargsr   rG   rH   r   s         r)   pandas_rolling_methodzRolling.pandas_rolling_method  s    +-- 	3 	3 bj22>22G	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3%ww%%t6v666s   )--c                   |                                  } | j        | j        j        ||g|R i |}| j        r  | j        j        | j        ||g|R ||d|S | j        r| j        dz  }| j        |z
  dz
  }n3| j        dk    rt          j
        | j                  }d}n| j        dz
  }d}t          | j        | j        ||||g|R ||d|S )N)rU   rO      rT   r   r   )r   r   r   r   r   map_partitionsr   r   r   r   	Timedeltar   )r   method_namerG   rH   r   rO   r4   r5   s           r)   _call_methodzRolling._call_method  s^   --//)t)H#^[
CG
 
 
KQ
 
 % 
	*48** 	 
 "     ; 	[A%FK&(1,EE^v%%\$+..FEE[1_FE&H
 
 
 
 
 
 
 	
r+   c                ,    |                      d          S )Ncountr   r   s    r)   r   zRolling.count'  s      )))r+   c                ,    |                      d          S )Ncovr   r   s    r)   r   zRolling.cov+        '''r+   c                ,    |                      d          S )Nsumr   r   s    r)   r   zRolling.sum/  r   r+   c                ,    |                      d          S )Nmeanr   r   s    r)   r   zRolling.mean3        (((r+   c                ,    |                      d          S )Nmedianr   r   s    r)   r   zRolling.median7  s      ***r+   c                ,    |                      d          S )Nr   r   r   s    r)   r   zRolling.min;  r   r+   c                ,    |                      d          S )Nr   r   r   s    r)   r   zRolling.max?  r   r+   rT   c                0    |                      dd          S )NstdrT   ddofr   r   r   s     r)   r   zRolling.stdC        Q ///r+   c                0    |                      dd          S )NvarrT   r   r   r   s     r)   r   zRolling.varG  r   r+   c                ,    |                      d          S )Nskewr   r   s    r)   r   zRolling.skewK  r   r+   c                ,    |                      d          S )Nkurtr   r   s    r)   r   zRolling.kurtO  r   r+   c                .    |                      d|          S )Nquantiler   )r   r   s     r)   r   zRolling.quantileS  s      X666r+   cythonc           	     J    |pi }|pd}|                      d||||||          S )Nr&   r   )rawengineengine_kwargsrG   rH   r   )r   rF   r   r   r   rG   rH   s          r)   r   zRolling.applyW  sH     2zr  ' ! 
 
 	
r+   c                &     | j         d|g|R i |S )Naggr   )r   rF   rG   rH   s       r)   	aggregatezRolling.aggregatem  s'     t >t>>>v>>>r+   c           	         d }|                                  }| j        |d<   | j        |d<   d                    d                    d t          |                                |          D                                 S )Nc                ,    | \  }}dddddd}||         S )Nr   rT   r         r   r   r   r   r   r&   )itemr   r   _orders       r)   orderzRolling.__repr__.<locals>.ordert  s2    DAq  F !9r+   r   r   zRolling [{}],c              3  .   K   | ]\  }}|| d| V  d S )N=r&   )r'   r   r   s      r)   rZ   z#Rolling.__repr__.<locals>.<genexpr>  s>        Aq= 

q

 === r+   )rq   )r   r   r   formatjoinsortedr   )r   r  r   s      r)   __repr__zRolling.__repr__s  s    		 		 		 --//#';x %)^z"$$HH  ">#7#7#9#9uEEE    
 
 	
r+   )rT   )Fr   NNN)__name__
__module____qualname____doc__r   r   r   propertyr   staticmethodr   r   r   
pd_Rollingr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	  r&   r+   r)   r   r     s       //
 J J J J0	 	 	 	
 	
 X	
 7 7 \7
&
 &
 &
P \** * * \*( ( ( \*( ( ( \*) ) ) \*+ + + \*( ( ( \*( ( ( \*0 0 0 0 \*0 0 0 0 \*) ) ) \*) ) ) \*7 7 7 \* 
 
 
 
* \*? ? ? C
 
 
 
 
r+   r   c                  \     e Zd Z	 	 	 	 	 d	 fd	Z fdZedddd            Z fdZ xZS )
RollingGroupbyNFr   c                   |j         | _         |j        | _        |j        }| j        t	          | j        t
                    r	| j        g}nt          | j                  }t	          |j        t
                    r|                    |j                   n|	                    |j                   ||         }t                                          ||||||           d S )Nr   )_groupby_kwargs_slice_groupby_slicer   r,   r   listbyr   r   superr   )
r   groupbyr   r   r   r   r   r   sliced_plus	__class__s
            r)   r   zRollingGroupby.__init__  s      '6%nk*$-s33 8#23"4#677'*c** /""7:....""7:...k"C# 	 	
 	
 	
 	
 	
r+   c                    t                                                      }|                    dd           dv r|                    d           |S )Nr   )r   r   )r  r   getr   )r   rH   r  s     r)   r   zRollingGroupby._rolling_kwargs  sH    ((**::fd##|33JJvr+   groupby_kwargsgroupby_slicec                    | j         di |}|r||         } |j        di |} t          ||          |i |                    d          S )Nrz   )levelr&   )r  r   r   
sort_index)	r>   r   r   r   r!  rG   rH   r  r   s	            r)   r   z$RollingGroupby.pandas_rolling_method  st     "*..~.. 	-m,G!'/33N33%ww%%t6v66AAAKKKr+   c                X     t                      j        |g|R | j        | j        d|S )Nr  )r  r   r  r  )r   r   rG   rH   r  s       r)   r   zRollingGroupby._call_method  sP    #uww#

 
  /-	
 

 
 
 	
r+   )NNFNr   )	r
  r  r  r   r   r  r   r   __classcell__)r  s   @r)   r  r    s         
 
 
 
 
 
B      L L L L \L
 
 
 
 
 
 
 
 
r+   r  )?
__future__r   rC   numbersr   pandasr   pandas.api.typesr   pandas.core.windowr   r  dask.array.corer   	dask.baser   dask.blockwiser	   dask.dataframer
   dask.dataframe._compatr   dask.dataframe.corer   r   r   r   r   r   r   r   dask.dataframe.ior   dask.dataframe.multir   dask.dataframe.utilsr   r   r   r   dask.delayedr   dask.highlevelgraphr   dask.typingr   
dask.utilsr   r   r   r    r!   typer   r"   r9   rN   r   rb   r`   r   r   r  r&   r+   r)   <module>r:     s   " " " " " "            4 4 4 4 4 4 4 4 4 4 4 4 ) ) ) ) ) )       + + + + + + " " " " " " A A A A A A	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 * ) ) ) ) ) 8 8 8 8 8 8            , + + + + + . . . . . . " " " " " " D D D D D D D D D D D D D D&"55  6# # #6  
Y7 Y7 Y7 Y7 Y7x" " "J> > >B> > >"  (A
 A
 A
 A
 A
 A
 A
 A
H@
 @
 @
 @
 @
W @
 @
 @
 @
 @
r+   