o
    ;DiW                     @   sb  d dl mZmZmZ d dlZd dlZd dlZd dlmZm	Z	 d dl
mZ d dlmZmZmZmZmZmZmZ d dlmZ 		d,d	d
Zee_G dd deZdd Z		d-ddZee_G dd deZdd Zdd Zee_G dd deZdd Z		d.ddZee_G d d! d!eZ d"d# Z!d$d% Z"e"e_"G d&d' d'eZ#d(d) Z$e$e_$G d*d+ d+eZ%dS )/    )absolute_importprint_functiondivisionN)next	text_type)comparable_itemgetter)Table	rowgettervalues
itervaluesheaderdata	asindices)sortvariablevaluec                 C   s   t | ||||dS )aM	  
    Reshape a table, melting fields into data. E.g.::

        >>> import petl as etl
        >>> table1 = [['id', 'gender', 'age'],
        ...           [1, 'F', 12],
        ...           [2, 'M', 17],
        ...           [3, 'M', 16]]
        >>> table2 = etl.melt(table1, 'id')
        >>> table2.lookall()
        +----+----------+-------+
        | id | variable | value |
        +====+==========+=======+
        |  1 | 'gender' | 'F'   |
        +----+----------+-------+
        |  1 | 'age'    |    12 |
        +----+----------+-------+
        |  2 | 'gender' | 'M'   |
        +----+----------+-------+
        |  2 | 'age'    |    17 |
        +----+----------+-------+
        |  3 | 'gender' | 'M'   |
        +----+----------+-------+
        |  3 | 'age'    |    16 |
        +----+----------+-------+

        >>> # compound keys are supported
        ... table3 = [['id', 'time', 'height', 'weight'],
        ...           [1, 11, 66.4, 12.2],
        ...           [2, 16, 53.2, 17.3],
        ...           [3, 12, 34.5, 9.4]]
        >>> table4 = etl.melt(table3, key=['id', 'time'])
        >>> table4.lookall()
        +----+------+----------+-------+
        | id | time | variable | value |
        +====+======+==========+=======+
        |  1 |   11 | 'height' |  66.4 |
        +----+------+----------+-------+
        |  1 |   11 | 'weight' |  12.2 |
        +----+------+----------+-------+
        |  2 |   16 | 'height' |  53.2 |
        +----+------+----------+-------+
        |  2 |   16 | 'weight' |  17.3 |
        +----+------+----------+-------+
        |  3 |   12 | 'height' |  34.5 |
        +----+------+----------+-------+
        |  3 |   12 | 'weight' |   9.4 |
        +----+------+----------+-------+

        >>> # a subset of variable fields can be selected
        ... table5 = etl.melt(table3, key=['id', 'time'],
        ...                   variables=['height'])
        >>> table5.lookall()
        +----+------+----------+-------+
        | id | time | variable | value |
        +====+======+==========+=======+
        |  1 |   11 | 'height' |  66.4 |
        +----+------+----------+-------+
        |  2 |   16 | 'height' |  53.2 |
        +----+------+----------+-------+
        |  3 |   12 | 'height' |  34.5 |
        +----+------+----------+-------+

    See also :func:`petl.transform.reshape.recast`.

    )key	variablesvariablefield
valuefield)MeltView)tabler   r   r   r    r   O/var/www/Datamplify/venv/lib/python3.10/site-packages/petl/transform/reshape.pymelt   s   Er   c                   @   "   e Zd Z		dddZdd ZdS )	r   Nr   r   c                 C   s"   || _ || _|| _|| _|| _d S N)sourcer   r   r   r   )selfr   r   r   r   r   r   r   r   __init___   s
   
zMeltView.__init__c                 C   s   t | j| j| j| j| jS r   )itermeltr   r   r   r   r   r   r   r   r   __iter__g   s   zMeltView.__iter__NNr   r   __name__
__module____qualname__r   r"   r   r   r   r   r   ]   s
    
r   c              
   #   s|   |d u r|d u rt dt| }zt| W n
 ty!   Y d S w d  |d ur/t ||d urBt|ttfs=|f}t ||d u rSfddtt	 D |d u rmfddtt	 D  fddD }t
 } fddD }|| || t|V  |D ]0}||}	t|D ]$\}
}zt|	}||
 |||  t|V  W q ty   Y qw qd S )Nz)either key or variables must be specifiedc                       g | ]}| vr|qS r   r   .0i)variables_indicesr   r   
<listcomp>       zitermelt.<locals>.<listcomp>c                    r(   r   r   r)   )key_indicesr   r   r-      r.   c                       g | ]} | qS r   r   r)   hdrr   r   r-          c                    r0   r   r   r)   r1   r   r   r-      r3   )
ValueErroriterr   StopIterationr   
isinstancelisttuplerangelenr	   appendzip
IndexError)r   r   r   r   r   itgetkeyouthdrrowkvr+   or   )r2   r/   r,   r   r    l   sN   





r      c              	   C   s   t | ||||||dS )a  
    Recast molten data. E.g.::

        >>> import petl as etl
        >>> table1 = [['id', 'variable', 'value'],
        ...           [3, 'age', 16],
        ...           [1, 'gender', 'F'],
        ...           [2, 'gender', 'M'],
        ...           [2, 'age', 17],
        ...           [1, 'age', 12],
        ...           [3, 'gender', 'M']]
        >>> table2 = etl.recast(table1)
        >>> table2
        +----+-----+--------+
        | id | age | gender |
        +====+=====+========+
        |  1 |  12 | 'F'    |
        +----+-----+--------+
        |  2 |  17 | 'M'    |
        +----+-----+--------+
        |  3 |  16 | 'M'    |
        +----+-----+--------+

        >>> # specifying variable and value fields
        ... table3 = [['id', 'vars', 'vals'],
        ...           [3, 'age', 16],
        ...           [1, 'gender', 'F'],
        ...           [2, 'gender', 'M'],
        ...           [2, 'age', 17],
        ...           [1, 'age', 12],
        ...           [3, 'gender', 'M']]
        >>> table4 = etl.recast(table3, variablefield='vars', valuefield='vals')
        >>> table4
        +----+-----+--------+
        | id | age | gender |
        +====+=====+========+
        |  1 |  12 | 'F'    |
        +----+-----+--------+
        |  2 |  17 | 'M'    |
        +----+-----+--------+
        |  3 |  16 | 'M'    |
        +----+-----+--------+

        >>> # if there are multiple values for each key/variable pair, and no
        ... # reducers function is provided, then all values will be listed
        ... table6 = [['id', 'time', 'variable', 'value'],
        ...           [1, 11, 'weight', 66.4],
        ...           [1, 14, 'weight', 55.2],
        ...           [2, 12, 'weight', 53.2],
        ...           [2, 16, 'weight', 43.3],
        ...           [3, 12, 'weight', 34.5],
        ...           [3, 17, 'weight', 49.4]]
        >>> table7 = etl.recast(table6, key='id')
        >>> table7
        +----+--------------+
        | id | weight       |
        +====+==============+
        |  1 | [66.4, 55.2] |
        +----+--------------+
        |  2 | [53.2, 43.3] |
        +----+--------------+
        |  3 | [34.5, 49.4] |
        +----+--------------+

        >>> # multiple values can be reduced via an aggregation function
        ... def mean(values):
        ...     return float(sum(values)) / len(values)
        ...
        >>> table8 = etl.recast(table6, key='id', reducers={'weight': mean})
        >>> table8
        +----+--------------------+
        | id | weight             |
        +====+====================+
        |  1 | 60.800000000000004 |
        +----+--------------------+
        |  2 |              48.25 |
        +----+--------------------+
        |  3 |              41.95 |
        +----+--------------------+

        >>> # missing values are padded with whatever is provided via the
        ... # missing keyword argument (None by default)
        ... table9 = [['id', 'variable', 'value'],
        ...           [1, 'gender', 'F'],
        ...           [2, 'age', 17],
        ...           [1, 'age', 12],
        ...           [3, 'gender', 'M']]
        >>> table10 = etl.recast(table9, key='id')
        >>> table10
        +----+------+--------+
        | id | age  | gender |
        +====+======+========+
        |  1 |   12 | 'F'    |
        +----+------+--------+
        |  2 |   17 | None   |
        +----+------+--------+
        |  3 | None | 'M'    |
        +----+------+--------+

    Note that the table is scanned once to discover variables, then a second
    time to reshape the data and recast variables as fields. How many rows are
    scanned in the first pass is determined by the `samplesize` argument.

    See also :func:`petl.transform.reshape.melt`.

    )r   r   r   
samplesizereducersmissing)
RecastView)r   r   r   r   rG   rH   rI   r   r   r   recast   s   mrK   c                   @   s$   e Zd Z			d	ddZdd ZdS )
rJ   Nr   r   rF   c                 C   s@   || _ || _|| _|| _|| _|d u rt | _n|| _|| _d S r   )r   r   r   r   rG   dictrH   rI   )r   r   r   r   r   rG   rH   rI   r   r   r   r     s   

zRecastView.__init__c                 C   s"   t | j| j| j| j| j| j| jS r   )
iterrecastr   r   r   r   rG   rH   rI   r!   r   r   r   r"   $  s   zRecastView.__iter__Nr   r   rF   NNr$   r   r   r   r   rJ     s    
rJ   c                 #   s    t | }zt|}W n
 ty   Y d S w ttt| ||r-tttfs-fr?ttr5n
tttfs?fsKfdd D sWfdd D  v saJ d vsiJ dvsqJ dD ]}	|	 v sJ d|	 qsD ]}	|	 v sJ d|	 q 	 fd	dD }
 fd
dD }ttr}n/t
t}t|d|D ]}t|D ]\}	||	 |  qq|D ]
}	t||	 ||	< qt}D ]	}	|||	  qt|V  t| d} t| dd }t|
 }tj|
 }tj||d}|D ]l\}}t|}||d }tdkr+t|}n|g}t|D ]C\}	||	 D ]9fdd|D }t|dkrR|}nt|dkr^|d }n|v rh| }nt}||}|| q;q3t|V  qd S )Nc                    s    g | ]}|vr| kr|qS r   r   r*   f)r   variablefieldsr   r   r-   H      ziterrecast.<locals>.<listcomp>c                    s    g | ]}| vr|kr|qS r   r   rO   )	keyfieldsr   r   r   r-   N  rR   zinvalid value field: %szvalue field cannot be keyfieldsz$value field cannot be variable fieldzinvalid keyfields field: %szinvalid variable field: %sc                       g | ]}  |qS r   indexrO   fldsr   r   r-   ]      c                    rT   r   rU   rO   rW   r   r   r-   ^  rY   r   r      c                    s    g | ]}|  kr| qS r   r   )r*   r)r+   
valueindexr   r   r   r-     s     )r5   r   r6   r8   mapr   r7   r9   rL   rV   collectionsdefaultdictset	itertoolsislicer=   addsortedextendr   r   operator
itemgettergroupbyr;   r<   )r   r   r   r   rG   rH   rI   r?   r2   rP   
keyindicesvariableindicesr   rB   rA   getsortablekeygetactualkeygroups_group	key_valueout_rowvalsvalredur   )rX   r+   rS   r   r]   r   rQ   r   rM   *  s   










rM   c                 C      t | S )a   
    Transpose rows into columns. E.g.::

        >>> import petl as etl
        >>> table1 = [['id', 'colour'],
        ...           [1, 'blue'],
        ...           [2, 'red'],
        ...           [3, 'purple'],
        ...           [5, 'yellow'],
        ...           [7, 'orange']]
        >>> table2 = etl.transpose(table1)
        >>> table2
        +----------+--------+-------+----------+----------+----------+
        | id       | 1      | 2     | 3        | 5        | 7        |
        +==========+========+=======+==========+==========+==========+
        | 'colour' | 'blue' | 'red' | 'purple' | 'yellow' | 'orange' |
        +----------+--------+-------+----------+----------+----------+

    See also :func:`petl.transform.reshape.recast`.

    )TransposeViewr   r   r   r   	transpose  s   ry   c                   @      e Zd Zdd Zdd ZdS )rw   c                 C   
   || _ d S r   r   )r   r   r   r   r   r        
zTransposeView.__init__c                 C   s
   t | jS r   )itertransposer   r!   r   r   r   r"     r}   zTransposeView.__iter__Nr$   r   r   r   r   rw         rw   c                 #   sN    t }fdd|D }tt|D ] t fdd|  D V  qd S )Nc                    s   g | ]}t  qS r   )r5   )r*   ro   r|   r   r   r-     r3   z!itertranspose.<locals>.<listcomp>c                 3   s    | ]}|  V  qd S r   r   r*   rB   )r+   r   r   	<genexpr>      z itertranspose.<locals>.<genexpr>)r   r:   r;   r9   )r   r2   itsr   )r+   r   r   r~     s   r~   FTc
           
      C   s   t | |||||||||	d
S )aE  
    Construct a pivot table. E.g.::

        >>> import petl as etl
        >>> table1 = [['region', 'gender', 'style', 'units'],
        ...           ['east', 'boy', 'tee', 12],
        ...           ['east', 'boy', 'golf', 14],
        ...           ['east', 'boy', 'fancy', 7],
        ...           ['east', 'girl', 'tee', 3],
        ...           ['east', 'girl', 'golf', 8],
        ...           ['east', 'girl', 'fancy', 18],
        ...           ['west', 'boy', 'tee', 12],
        ...           ['west', 'boy', 'golf', 15],
        ...           ['west', 'boy', 'fancy', 8],
        ...           ['west', 'girl', 'tee', 6],
        ...           ['west', 'girl', 'golf', 16],
        ...           ['west', 'girl', 'fancy', 1]]
        >>> table2 = etl.pivot(table1, 'region', 'gender', 'units', sum)
        >>> table2
        +--------+-----+------+
        | region | boy | girl |
        +========+=====+======+
        | 'east' |  33 |   29 |
        +--------+-----+------+
        | 'west' |  35 |   23 |
        +--------+-----+------+

        >>> table3 = etl.pivot(table1, 'region', 'style', 'units', sum)
        >>> table3
        +--------+-------+------+-----+
        | region | fancy | golf | tee |
        +========+=======+======+=====+
        | 'east' |    25 |   22 |  15 |
        +--------+-------+------+-----+
        | 'west' |     9 |   31 |  18 |
        +--------+-------+------+-----+

        >>> table4 = etl.pivot(table1, 'gender', 'style', 'units', sum)
        >>> table4
        +--------+-------+------+-----+
        | gender | fancy | golf | tee |
        +========+=======+======+=====+
        | 'boy'  |    15 |   29 |  24 |
        +--------+-------+------+-----+
        | 'girl' |    19 |   24 |   9 |
        +--------+-------+------+-----+

    See also :func:`petl.transform.reshape.recast`.

    )rI   	presorted
buffersizetempdircache)	PivotView)
r   f1f2f3aggfunrI   r   r   r   r   r   r   r   pivot  s   5r   c                   @   r   )	r   NFTc                 C   sJ   |r|| _ nt|||f||	|
d| _ |||| _| _| _|| _|| _d S )N)r   r   r   r   )r   r   r   r   r   r   rI   )r   r   r   r   r   r   rI   r   r   r   r   r   r   r   r     s   
zPivotView.__init__c                 C   s   t | j| j| j| j| j| jS r   )	iterpivotr   r   r   r   r   rI   r!   r   r   r   r"     s   zPivotView.__iter__NFNNTr$   r   r   r   r   r     s
    
r   c                 #   s   t t| |}t|}|  |g}|| t|V  t| }zt|}	W n ty2   g }	Y nw tt	t
|	}
|
|}|
|}|
| tj|t|dD ]6\}}|g|gt|  }tj|t|dD ]\}}| fdd|D }||d|| < qkt|V  qSd S )NrZ   c                    s   g | ]}|  qS r   r   r   f3ir   r   r-   /  r3   ziterpivot.<locals>.<listcomp>r[   )ra   r   r8   r   rf   r9   r5   r   r6   r^   r   rV   rb   ri   rg   rh   r;   )r   r   r   r   r   rI   f2valsrA   r?   r2   rX   f1if2iv1v1rowsoutrowv2v12rowsaggvalr   r   r   r     s4   




r   c                 C   rv   )a  
    Convert a table to a sequence of values in row-major order. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar', 'baz'],
        ...           ['A', 1, True],
        ...           ['C', 7, False],
        ...           ['B', 2, False],
        ...           ['C', 9, True]]
        >>> list(etl.flatten(table1))
        ['A', 1, True, 'C', 7, False, 'B', 2, False, 'C', 9, True]

    See also :func:`petl.transform.reshape.unflatten`.

    )FlattenViewrx   r   r   r   flatten4  s   r   c                   @   rz   )r   c                 C   r{   r   rx   )r   r   r   r   r   r   M  r}   zFlattenView.__init__c                 c   s&    t | jD ]
}|D ]}|V  q
qd S r   )r   r   )r   rB   r   r   r   r   r"   P  s   zFlattenView.__iter__Nr$   r   r   r   r   r   K  r   r   c                  O   s   t | i |S )a  
    Convert a sequence of values in row-major order into a table. E.g.::

        >>> import petl as etl
        >>> a = ['A', 1, True, 'C', 7, False, 'B', 2, False, 'C', 9]
        >>> table1 = etl.unflatten(a, 3)
        >>> table1
        +-----+----+-------+
        | f0  | f1 | f2    |
        +=====+====+=======+
        | 'A' |  1 | True  |
        +-----+----+-------+
        | 'C' |  7 | False |
        +-----+----+-------+
        | 'B' |  2 | False |
        +-----+----+-------+
        | 'C' |  9 | None  |
        +-----+----+-------+

        >>> # a table and field name can also be provided as arguments
        ... table2 = [['lines'],
        ...           ['A'],
        ...           [1],
        ...           [True],
        ...           ['C'],
        ...           [7],
        ...           [False],
        ...           ['B'],
        ...           [2],
        ...           [False],
        ...           ['C'],
        ...           [9]]
        >>> table3 = etl.unflatten(table2, 'lines', 3)
        >>> table3
        +-----+----+-------+
        | f0  | f1 | f2    |
        +=====+====+=======+
        | 'A' |  1 | True  |
        +-----+----+-------+
        | 'C' |  7 | False |
        +-----+----+-------+
        | 'B' |  2 | False |
        +-----+----+-------+
        | 'C' |  9 | None  |
        +-----+----+-------+

    See also :func:`petl.transform.reshape.flatten`.

    )UnflattenView)argskwargsr   r   r   	unflattenV  s   3r   c                   @   rz   )r   c                 O   sh   t |dkr|d | _|d | _nt |dkr't|d |d | _|d | _nJ d|dd | _d S )N   r   r[      Fzinvalid argumentsrI   )r;   inputperiodr
   getrI   )r   r   r   r   r   r   r     s   
zUnflattenView.__init__c                 c   s    | j }| j}| j}tdd t|D }|V  t }|D ]}t||k r+|| qt|V  |g}qt|dkrSt||k rL||g|t|   t|V  d S d S )Nc                 s   s    | ]}d | V  qdS )zf%sNr   r)   r   r   r   r     r   z)UnflattenView.__iter__.<locals>.<genexpr>r   )	r   r   rI   r9   r:   r8   r;   r<   rf   )r   inptr   rI   rA   rB   rD   r   r   r   r"     s"   
zUnflattenView.__iter__Nr$   r   r   r   r   r     s    r   r#   rN   r   )&
__future__r   r   r   rb   r_   rg   petl.compatr   r   petl.comparisonr   petl.util.baser   r	   r
   r   r   r   r   petl.transform.sortsr   r   r   r    rK   rJ   rM   ry   rw   r~   r   r   r   r   r   r   r   r   r   r   r   <module>   sH    $
J3
rq	
:6