
    tfV                       d dl mZ d dl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/m0Z0m1Z1m2Z2m3Z3m4Z4  e5de6fi       Z7d Z8d Z9e%e.ddddd       Z:d Z;d Z<d Z=d Z> G d d      Z G d d e      Z?y)!    )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                z   d}| -t        |t              r| j                  d   |k7  rt        |      |-t        |t              r|j                  d   |k7  rt        |      | ||fD cg c]  }||	 }}t	        j
                  |      }t        || t        |       nd |t        |      f      S d f      S c c}w )NzqPartition size is less than overlapping window size. Try using ``df.repartition`` to increase the partition size.r   )
isinstancer   shapeNotImplementedErrorr
   concatr"   len)		prev_partcurrent_part	next_partbeforeaftermsgppartscombineds	            `/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/dask/dataframe/rolling.py_combined_partsr3   *   s    	*  FH!=??1'%c**E8!<??1&%c**"L)<N1QNEN~~e$H'3C	N'3C	N	
  :>	
  Os   (B80B8c                   |D cg c]  }t        |t              s| }}|d   \  }}}	|D 
cg c]  }
t        |
t              r|
d   n|
 }}
 | |i |}|d }t        |t        j                        r|}d }|j                  d   dk7  r|j                  d   |j                  d   z  }|r|r||z  }|	|j
                  |d  S t        |t        j                        r|	}|r|r||z  }|j
                  ||  S c c}w c c}
w )Nr   )r$   r"   datetime	timedeltar%   iloc)funcr,   r-   argskwargsdfdfsr1   prev_part_lengthnext_part_lengthargout	expansions                r2   overlap_chunkrB   E   s   
?"
2~ >2
?C
?36q60H 0JNO3jn5CF3>ODO


C&(,,-!I~~aAIIaLHNN1$55	))xx  %++, 88FE6""/ @ Ps   C8C8C=T)metaenforce_metadatatransform_divisionsalign_dataframesc          	     J   ! 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                        r4t        |j                  j                  j                        s@t        d      t	        t              rdk\  rt	        t              rdk\  st!        d      |	j#                  dd      }
|	j#                  dd      }t%        |       sJ |
t'        g|i |	}n dt)        |       z   }
t'        | g|i |	}|
 d	| }
|rt+        |      }	 t-        |      }|D cg c]  }t	        |t.              s| }}t1        ||| |	|      t3        d |D              rS|
dft4        | t6        |D cg c]  }|j8                  df c}f|	fi}t;        j<                  |
||      }t?        ||
      S g }g }tA        |||| ||	        fd}|D ]  }t	        |t.              r+ ||      }|jC                  |       |jC                  |       >tE        |      }tG        |      \  }}|r#|jC                  |       |jI                  |       ||jC                  |        i }d}|	jK                         D ]9  \  }}tE        |      }tG        |      \  }}|jI                  |       |||<   |s8d}; tM        | d      rGtO         dd       D ci c]  \  }}|f||d }}}|jQ                  dtS        |             | !!fd} |r tU        tV        |
| g||tX        d|}n!|r|	n|}tU        tX        |
| g|i |d|i}t;        j<                  |
||      }t[        ||
       S # t         $ r}t!        | d
      |d}~ww xY wc c}w c c}w c c}}w )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 receive
        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              3  <   K   | ]  }t        |t                y w)N)r$   r   ).0r?   s     r2   	<genexpr>zmap_overlap.<locals>.<genexpr>   s     
3s:c6"
3s   dependenciesc                x   i }t        |       \  }}|j                  |       t        |       \  }}|j                  |       dt        |       z   }t	        t        || j                         |            D ]  \  }\  }}	}
||f}t        ||	|
f||<    t        j                  ||| g      }t        ||      S )Nzoverlap-concat-rO   )_get_previous_partitionsupdate_get_nexts_partitionsr   	enumeratezip__dask_keys__r3   r   from_collectionsr   )r?   dskprevs_parts_dskprevsnexts_parts_dsknextsname_aiprevcurrentnextkeygraphr-   r,   	divisionsrC   s                r2   _handle_frame_argumentz+map_overlap.<locals>._handle_frame_argument   s    !9#v!F

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

?#"Xc]2(1s((*E2)
 	M$A$gt 1+C'wfeLCH		M //3%PUFD)<<    TFpartition_info)numberdivisionc                     |i |d| iS )Nrh    )rh   r9   r:   	orig_funcs      r2   r8   zmap_overlap.<locals>.func  s    dLfL^LLrg   )rP   _func_metarP   ).r   r   r   r   r$   strpdto_timedeltar5   r6   r   index_meta_nonemptyinferred_type	TypeErrorr   
ValueErrorpopcallabler   r    r   r   r   r   allr   tuple_namer   rX   r   r   appendr   r   extenditemsr!   rU   insertr	   r   r   rB   r   )"r8   r;   r,   r-   rC   rD   rE   rF   r9   r:   namerJ   rI   er<   r?   layerrd   args2rP   rf   arg2collectionskwargs3simplekvr_   rk   rh   rY   kwargs4re   rn   s"     ```                           @@r2   map_overlapr   `   s   D 2"3B"7ASTVAW 	B  54<D&#(%&&(,,-E8CUCU1V&rxx'>'>'L'LM+  vx(!5(+
IJJ::gt$D**]D1KD>>vu>t>v>HTN*tVUDTDVDV1UGD!$'	*40D 
7"
2v 62
7C
7#D#tVT;OD

3d
331I48C#))Q89	
 //e$OeT4((EL--sD$I=&  c6"(-CLL$C .s3kLL,LL GF 1!+A.;K(
F 4)*  )3B8
8 DQH55
 

 	Q(89		M !
 
 &
 
 #&!	
 	
 	
 &	
 ++D#LQEdI66_  	# Q Q 	 8 9t
s0   O5 +PP;PP5	P>PPc                   i }| j                   }d}dt        | |      z   }|rot        |t              r_g }t	        d| j
                        D ]/  }||f}t        j                  ||f|f||<   |j                  |       1 |j                  d       ||fS t        |t        j                        rt        j                  | j                        j                         j                  dd }	||	kD  j!                         rt#        |      g }t	        d| j
                        D ]+  }||f}t$        ||dz
  f||f|f||<   |j                  |       - |j                  d       ||fS 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-rH   Nri   r   )r}   r   r$   r   rangenpartitionsr   headr~   r5   r6   rr   Seriesre   diffr7   anyrx   _head_timedelta)
r;   r-   rY   df_nametimedelta_partition_messagename_br]   r_   rc   deltass
             r2   rT   rT   =  s    ChhG	F  
 "e!44FE8,q"..) 	A1+C!e4CHLL	 	T : 
E8--	.2<<(--/44Qr:FN!899q"..) 	A1+C''1q5)9GQ<OCHLL	 	T : ':rg   c                   i }| j                   }dt        | |      z   }|rat        |t              rQdg}t	        | j
                  dz
        D ]/  }||f}t        j                  ||f|f||<   |j                  |       1 ||fS t        |t        j                        rJt        j                  | j                        }|j                         j                  dd }	||	kD  j!                         r|d   }
dg}t	        | j
                  dz
        D ]  }||dz      }t#        ||z
  |
      }||   |}}||kD  r|dkD  r||	|   z
  }|dz
  }||kD  r|dkD  r||f}t$        t	        ||dz         D cg c]  }||f c}||dz   f|f||<   |j                  |        ||fS dg}t	        | j
                  dz
        D ],  }||f}t$        ||fg||dz   f|f||<   |j                  |       . ||fS dg| j
                  z  }||fS c c}w )zH
    Helper to get the previous partitions required for the overlap
    zoverlap-prepend-NrH   ri   r   )r}   r   r$   r   r   r   r   tailr~   r5   r6   rr   r   re   r   r7   r   max_tail_timedelta)r;   r,   rY   r   r^   r[   r_   rc   divsr   pt_zpt_ilbfirstjr   s                   r2   rR   rR   b  sY    ChhG(2v"66F*VX.r~~)* 	A1+C!f5CHLL	h :_ 
FH..	/yy&!!!B' VO  "7DFE2>>A-. " AE{ -7AqbjQU!F1I-EAA bjQU qk#+0AE?;agq\;a!e$	C S!)"F : FE2>>A-. "qk#q\Na!e$	C S!" : ':' <s   (G:c                \    ||j                   | 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
    )rt   r   )ra   next_r-   s      r2   r   r     s*      1 1 3e ;<==rg   c           	         t        j                  | D cg c]/  }||j                  |j                  j                         |z
  kD     1 c}      }|S c c}w )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
    )r
   r'   rt   min)r[   ra   r,   r`   selecteds        r2   r   r     sM     ~~GLMtdjjGMM--/&89	:MH O 	Ns   4A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y)r   z%Provides rolling window calculations.NFc                D   || _         || _        || _        || _        || _        || _         |j                  j                  di | j                          t        | j                  t              rd nd| _        | j                  dv rt        j                  dt               y y )Nfreq)rt   rH   rowszTUsing axis=1 in Rolling has been deprecated and will be removed in a future version.rm   )objwindowmin_periodscenteraxiswin_typerp   rolling_rolling_kwargsr$   int	_win_typewarningswarnFutureWarning)selfr   r   r   r   r   r   s          r2   __init__zRolling.__init__  s     &	 		3D0023 ",DKK!=699,,MM' -rg   c                    | 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   r:   s     r2   r   zRolling._rolling_kwargs  sG    kk++kk	
 99J&!YYF6Nrg   c                    | j                   dv xsF t        | j                  t              xr | j                  dk  xs | j                  j
                  dk(  S )zm
        Indicator for whether the object has a single partition (True)
        or multiple (False).
        )rH   columnsrH   )r   r$   r   r   r   r   r   s    r2   _has_single_partitionzRolling._has_single_partition  sM     II' )4;;1FdkkQ6F)xx##q(	
rg   c                    t               5   | j                  di |}d d d         t        |      |i |S # 1 sw Y   xY w)Nrm   )r   r   getattr)r;   rolling_kwargsr   r9   r:   r   s         r2   pandas_rolling_methodzRolling.pandas_rolling_method  sI    +- 	3 bjj2>2G	3%ww%t6v66	3 	3s	   8Ac                &   | j                         } | j                  | j                  j                  ||g|i |}| j                  r/ | j                  j
                  | j                  ||g|||d|S | j                  r"| j                  dz  }| j                  |z
  dz
  }nB| j                  dk(  r"t        j                  | j                        }d}n| j                  dz
  }d}t        | j                  | j                  ||||g|||d|S )N)rI   rC      rH   r   r   )r   r   r   ru   r   map_partitionsr   r   r   rr   	Timedeltar   )r   method_namer9   r:   r   rC   r,   r-   s           r2   _call_methodzRolling._call_method  s@   --/)t))HH##^[
CG
KQ
 %%*488**** 	
 "   ;;[[A%FKK&(1,E^^v%\\$++.FE[[1_FE&&HH
 
 
 
 	
rg   c                $    | j                  d      S )Ncountr   r   s    r2   r   zRolling.count/  s      ))rg   c                $    | j                  d      S )Ncovr   r   s    r2   r   zRolling.cov3        ''rg   c                $    | j                  d      S )Nsumr   r   s    r2   r   zRolling.sum7  r   rg   c                $    | j                  d      S )Nmeanr   r   s    r2   r   zRolling.mean;        ((rg   c                $    | j                  d      S )Nmedianr   r   s    r2   r   zRolling.median?  s      **rg   c                $    | j                  d      S )Nr   r   r   s    r2   r   zRolling.minC  r   rg   c                $    | j                  d      S )Nr   r   r   s    r2   r   zRolling.maxG  r   rg   c                (    | j                  dd      S )NstdrH   ddofr   r   r   s     r2   r   zRolling.stdK        Q //rg   c                (    | j                  dd      S )NvarrH   r   r   r   s     r2   r   zRolling.varO  r   rg   c                $    | j                  d      S )Nskewr   r   s    r2   r   zRolling.skewS  r   rg   c                $    | j                  d      S )Nkurtr   r   s    r2   r   zRolling.kurtW  r   rg   c                &    | j                  d|      S )Nquantiler   )r   r   s     r2   r   zRolling.quantile[  s      X66rg   c           	     J    |xs i }|xs d}| j                  d||||||      S )Nrm   r   )rawengineengine_kwargsr9   r:   r   )r   r8   r   r   r   r9   r:   s          r2   r   zRolling.apply_  sC     2zr  ' ! 
 	
rg   c                0     | j                   d|g|i |S )Naggr   )r   r8   r9   r:   s       r2   	aggregatezRolling.aggregateu  s!     t  >t>v>>rg   c           	         d }| j                         }| j                  |d<   | j                  |d<   dj                  dj	                  d t        |j                         |      D                    S )Nc                &    | \  }}dddddd}||   S )Nr   rH   r         r   r   r   r   r   rm   )itemr   r   _orders       r2   orderzRolling.__repr__.<locals>.order|  s-    DAq F !9rg   r   r   zRolling [{}],c              3  4   K   | ]  \  }}|	| d|   y w)N=rm   )rM   r   r   s      r2   rN   z#Rolling.__repr__.<locals>.<genexpr>  s+      Aq= #Qqc
s   )rc   )r   r   r   formatjoinsortedr   )r   r   r   s      r2   __repr__zRolling.__repr__{  so    		 --/#';;x %)^^z"$$HH ">#7#7#9uE 
 	
rg   )rH   )FcythonNNN)__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   rm   rg   r2   r   r     s   /
 >	 	
 	
 7 7
&
P ** * *( ( *( ( *) ) *+ + *( ( *( ( *0 0 *0 0 *) ) *) ) *7 7 * 
 
* *? ? C
rg   r   c                  V     e Zd Z	 	 	 	 	 d fd	Z fdZedddd       Z fdZ xZS )RollingGroupbyNc                   |j                   | _         |j                  | _        |j                  }| j                  t	        | j                  t
              r| j                  g}nt        | j                        }t	        |j                  t
              r|j                  |j                         n|j                  |j                         ||   }t        	| -  ||||||       y )Nr   )_groupby_kwargs_slice_groupby_slicer   r$   rq   listbyr~   r   superr   )
r   groupbyr   r   r   r   r   r   sliced_plus	__class__s
            r2   r   zRollingGroupby.__init__  s      '66%nnkk*$--s3#223"4#6#67'**c*""7::.""7::.k"C# 	 	
rg   c                n    t         |          }|j                  dd       dv r|j                  d       |S )Nr   )r   rt   )r  r   getry   )r   r:   r  s     r2   r   zRollingGroupby._rolling_kwargs  s4    (*::fd#|3JJvrg   groupby_kwargsgroupby_slicec                    | j                   di |}|r||   } |j                  di |} t        ||      |i |j                  d      S )Nri   )levelrm   )r  r   r   
sort_index)	r;   r   r   r  r  r9   r:   r  r   s	            r2   r   z$RollingGroupby.pandas_rolling_method  s^     "**.~.m,G!'//3N3%ww%t6v6AAAKKrg   c                X    t        |   |g|| j                  | j                  d|S )Nr  )r  r   r	  r  )r   r   r9   r:   r  s       r2   r   zRollingGroupby._call_method  sC    w#

  //--	

 
 	
rg   )NNFNr   )	r   r   r  r   r   r  r   r   __classcell__)r  s   @r2   r  r    sG     
B  L L
 
rg   r  )@
__future__r   r5   r   numbersr   pandasrr   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"   r3   rB   r   rT   rR   r   r   r  rm   rg   r2   <module>r/     s    "     4 4 )  + " A	 	 	 * 8  , . " D D&"56#6  
Y7 Y7x"J>B>"(H
 H
V@
W @
rg   