a
    yn„f›4  ã                   @  s    d dl m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Ze ¡ Ze ¡ Zd	d
dœdd„Ze	ƒ deddœdd„ƒZdd„ Zdd„ ZdS )é    )Úannotations)Úreduce)ÚiconcatN)Úhigh_level_function)ÚNumpy)ÚNumpyMetadata)Úto_dataframeÚintÚstr)ÚindexÚreturnc                 C  s   d|  d S )NÚsubÚentry© )r   r   r   úe/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/awkward/operations/ak_to_dataframe.pyÚ_default_levelname   s    r   ÚinnerÚvalues©ÚhowÚ	levelnameÚ	anonymousc                c  s   | fV  t | |||ƒS )a  
    Args:
        array: Array-like data (anything #ak.to_layout recognizes).
        how (None or str): Passed to
            [pd.merge](https://pandas.pydata.org/pandas-docs/version/1.0.3/reference/api/pandas.merge.html)
            to combine DataFrames for each multiplicity into one DataFrame. If
            None, a list of Pandas DataFrames is returned.
        levelname (int -> str): Computes a name for each level of the row index
            from the number of levels deep.
        anonymous (str): Column name to use if the `array` does not contain
            records; otherwise, column names are derived from record fields.

    Converts Awkward data structures into Pandas
    [MultiIndex](https://pandas.pydata.org/pandas-docs/stable/user_guide/advanced.html)
    rows and columns. The resulting DataFrame(s) contains no Awkward structures.

    #ak.Array structures can't be losslessly converted into a single DataFrame;
    different fields in a record structure might have different nested list
    lengths, but a DataFrame can have only one index.

    If `how` is None, this function always returns a list of DataFrames (even
    if it contains only one DataFrame); otherwise `how` is passed to
    [pd.merge](https://pandas.pydata.org/pandas-docs/version/1.0.3/reference/api/pandas.merge.html)
    to merge them into a single DataFrame with the associated loss of data.

    In the following example, nested lists are converted into MultiIndex rows.
    The index level names `"entry"`, `"subentry"` and `"subsubentry"` can be
    controlled with the `levelname` parameter. The column name `"values"` is
    assigned because this array has no fields; it can be controlled with the
    `anonymous` parameter.

        >>> ak.to_dataframe(ak.Array([[[1.1, 2.2], [], [3.3]],
        ...                           [],
        ...                           [[4.4], [5.5, 6.6]],
        ...                           [[7.7]],
        ...                           [[8.8]]]))
                                    values
        entry subentry subsubentry
        0     0        0               1.1
                       1               2.2
              2        0               3.3
        2     0        0               4.4
              1        0               5.5
                       1               6.6
        3     0        0               7.7
        4     0        0               8.8

    In this example, nested records are converted into MultiIndex columns.
    (MultiIndex rows and columns can be mixed; these examples are deliberately
    simple.)

        >>> ak.to_dataframe(ak.Array([
        ...     {"I": {"a": _, "b": {"i": _}}, "II": {"x": {"y": {"z": _}}}}
        ...     for _ in range(0, 50, 10)]))
                I      II
                a   b   x
                    i   y
                        z
        entry
        0       0   0   0
        1      10  10  10
        2      20  20  20
        3      30  30  30
        4      40  40  40

    The following two examples show how fields of different length lists are
    merged. With `how="inner"` (default), only subentries that exist for all
    fields are preserved; with `how="outer"`, all subentries are preserved at
    the expense of requiring missing values.

        >>> ak.to_dataframe(ak.Array([{"x": [], "y": [4.4, 3.3, 2.2, 1.1]},
        ...                           {"x": [1], "y": [3.3, 2.2, 1.1]},
        ...                           {"x": [1, 2], "y": [2.2, 1.1]},
        ...                           {"x": [1, 2, 3], "y": [1.1]},
        ...                           {"x": [1, 2, 3, 4], "y": []}]),
        ...                          how="inner")
                        x    y
        entry subentry
        1     0         1  3.3
        2     0         1  2.2
              1         2  1.1
        3     0         1  1.1

    The same with `how="outer"`:

        >>> ak.to_dataframe(ak.Array([{"x": [], "y": [4.4, 3.3, 2.2, 1.1]},
        ...                           {"x": [1], "y": [3.3, 2.2, 1.1]},
        ...                           {"x": [1, 2], "y": [2.2, 1.1]},
        ...                           {"x": [1, 2, 3], "y": [1.1]},
        ...                           {"x": [1, 2, 3, 4], "y": []}]),
        ...                          how="outer")
                          x    y
        entry subentry
        0     0         NaN  4.4
              1         NaN  3.3
              2         NaN  2.2
              3         NaN  1.1
        1     0         1.0  3.3
              1         NaN  2.2
              2         NaN  1.1
        2     0         1.0  2.2
              1         2.0  1.1
        3     0         1.0  1.1
              1         2.0  NaN
              2         3.0  NaN
        4     0         1.0  NaN
              1         2.0  NaN
              2         3.0  NaN
              3         4.0  NaN
    )Ú_impl)Úarrayr   r   r   r   r   r   r      s    sr   c              
     s  zdd l }W n. ty: } ztdƒ|‚W Y d }~n
d }~0 0 |d ur„d }t| d ˆˆ d�D ]&}|d u rj|}qX|j|||ddd�}qX|S ‡ ‡fdd„‰tjj| ddd	�}t|tjj	ƒrÊ|j
|j|jd
 … }	n|}	g }
d }ˆ|	g dƒD �]ú\}}}t|tjj	ƒ�r
|d
d … }t|ƒdk�r ˆ g}n|j |g¡}t |jtj¡�rPtj |d¡}n t |jtj¡�rptj |d¡}|d u�rxt|ƒt|ƒk�rxtdd„ t||ƒD ƒƒ�rx|
d j}t||jƒ�rÊt|jƒ‰ntdd„ |D ƒƒ‰t|jƒ‰tˆˆƒ‰ˆˆk�r |j ‡‡fdd„|D ƒ¡}||
d _ˆˆk�rF|j ‡‡fdd„|D ƒ¡}|j||
d j|d�}|j|
d |gd
d�|
d< nbt|ƒdk�rž|jt|ƒˆdƒd�}n&|jj |‡fdd„t!t|ƒƒD ƒd�}|
 "|j|||d�¡ |}qâ|
D ]2}t|j|jƒ�rä|jj#d
k�rä|j $d¡|_�qä|
S )Nr   zbinstall the 'pandas' package with:

    pip install pandas --upgrade

or

    conda install pandasr   T)r   Z
left_indexZright_indexc                   s°  ˆj rˆjsˆˆ ¡ ˆˆ ƒS ˆ d¡dv r>tj ˆ¡ˆˆ fgS ˆjdk�rˆjddd�\}}|j	d d… |j	dd …  }}|| ‰tj
js’tj
jr¤ˆjj ˆtj¡‰tˆƒdkrÐt tjtˆƒˆjd�ˆ¡g}n‡fdd	„ˆD ƒ}| tj|d ˆjd�t |ˆ¡ ¡ ˆ||ˆ ƒS ˆj�rftˆˆƒ‰ˆj�rBtj ˆ¡ˆˆ fgS tt‡ ‡‡‡fd
d„ˆjD ƒg ƒS nFtˆtjjƒ�r˜tt‡ ‡‡‡fdd„ˆjD ƒg ƒS tj ˆ¡ˆˆ fgS d S )NZ	__array__)ÚstringÚ
bytestringé   )ÚaxisÚdepthéÿÿÿÿr   ©Údtypec                   s   g | ]}t  |ˆ ¡‘qS r   )ÚnumpyÚrepeat©Ú.0Úx)Úcountsr   r   Ú
<listcomp>»   ó    z*_impl.<locals>.recurse.<locals>.<listcomp>c                 3  s*   | ]"}ˆˆ  |¡ˆg ˆ ¢|‘R ƒV  qd S ©N©Ú_getitem_field©r%   Ún©Ú	col_namesÚlayoutÚrecurseÚ
row_arraysr   r   Ú	<genexpr>É   s   ÿz)_impl.<locals>.recurse.<locals>.<genexpr>c                 3  s*   | ]"}ˆˆ  |¡ˆg ˆ ¢|‘R ƒV  qd S r*   r+   r-   r/   r   r   r4   Ó   s   ÿ) Ú
is_indexedÚ	is_optionÚprojectZ	parameterÚakÚ
operationsZto_numpyZpurelist_depthZ_offsets_and_flattenedÚdataZ_utilÚwinZbits32ÚbackendÚindex_nplikeZastypeÚnpZint32Úlenr"   r#   Zaranger!   ÚappendÚis_unionÚ_union_to_recordr   r   ÚfieldsÚ
isinstanceÚcontentsÚRecordArray)r1   r3   r0   ÚoffsetsZ	flattenedZstartsÚstopsZnewrows)r   r2   )r0   r'   r1   r3   r   r2   ©   sP    ÿ
ÿÿ
þú	þú
z_impl.<locals>.recurseÚerror)Zallow_recordZprimitive_policyr   r   Únans   nanc                 s  s   | ]\}}t  ||¡V  qd S r*   )r"   Zarray_equal)r%   r&   Úyr   r   r   r4   ö   s   z_impl.<locals>.<genexpr>r   c                 s  s   | ]}t |ƒV  qd S r*   )r?   r$   r   r   r   r4   þ   r)   c                   s   g | ]}|d ˆ ˆ   ‘qS ©)Ú r   r$   )ÚmaxnumÚnumoldr   r   r(     r)   z_impl.<locals>.<listcomp>c                   s   g | ]}|d ˆ ˆ   ‘qS rL   r   r$   )rN   Únumnewr   r   r(     r)   )r:   r   Úcolumns)r   )Únamec                   s   g | ]}ˆ |ƒ‘qS r   r   )r%   Úi)r   r   r   r(     r)   )Únames)%ÚpandasÚImportErrorr   Úmerger8   r9   Z	to_layoutrD   ÚrecordZRecordr   Úatr?   Z
MultiIndexÚfrom_tuplesr>   Z
issubdtyper!   Zstr_r"   ÚmaZfilledÚbytes_ÚallÚziprQ   ZlevelsÚmaxZ	DataFramer   ÚconcatZ
RangeIndexZfrom_arraysÚranger@   ZnlevelsZget_level_values)r   r   r   r   rU   ÚerrÚoutÚdfr1   Zlayout2ZtablesZlast_row_arraysÚcolumnr3   r0   rQ   Z
oldcolumnsZnewframer   Útabler   )r   r   rN   rP   rO   r2   r   r   �   s�    ÿø
ÿ
4ÿþÿý



ÿ

ÿÿÿr   c              	   C  sø  g }| j D ]f}|jr*|js*| | ¡ ¡ q
|jrB| t||ƒ¡ q
|jrf| tjj	|t
jddd�¡ q
| |¡ q
tdd„ |D ƒƒsžtj j| j| j|| jd�S tƒ }g }|D ]X}t|tj jƒrè|jD ] }||vrÄ| |¡ | |¡ qÄq¬||vr¬| |¡ | |¡ q¬tj  tj | jjj| jdt
jd�¡tj  ¡ ¡}g }|D ]¢}	g }
|D ]p}t|tj jƒ�r˜|jD ]&}|	|k�rd|
 | |¡¡  �q¸�qd|
 |¡ n |	|k�r®|
 |¡ n
|
 |¡ �qJ| tj jj| j| j|
| jd�¡ �q>tj  ||| j¡S d S )	Nr   F)r   Z	highlevelc                 s  s   | ]}t |tjjƒV  qd S r*   )rD   r8   rE   rF   r$   r   r   r   r4   0  r)   z#_union_to_record.<locals>.<genexpr>)Ú
parametersr   r    ) rE   r5   r6   r@   r7   rA   rB   r8   r9   Z	fill_noner>   rJ   ÚanyZ
UnionArrayÚtagsr   rg   ÚsetrD   rF   rC   ÚaddZIndexedOptionArrayZIndex64r<   r=   ÚfullÚlengthZint64Z
EmptyArrayr,   Z
simplified)Z
unionarrayr   rE   r1   ÚseenZ	all_namesÚfieldZmissingarrayZ
all_fieldsrR   Zunion_contentsr   r   r   rB   "  sv    
ÿü



ÿÿú	



üÿ	rB   )Ú
__future__r   Ú	functoolsr   Úoperatorr   Zawkwardr8   Zawkward._dispatchr   Zawkward._nplikes.numpyr   Zawkward._nplikes.numpy_liker   Ú__all__Úinstancer"   r>   r   r   r   rB   r   r   r   r   Ú<module>   s    ÿx 