o
    Zh6                     @   s@  d dl 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
Z
d dlZd dlZd dlZd dlZd dlZd dlmZmZ i add Zd	d
 Zejdejdejdejdejdejdejdejdej dej!dej"dddej#dej$diZ%dd Z&dd Z'dd Z(d d! Z)d"d# Z*d$d% Z+d&d' Z,d(d) Z-d*d+ Z.d,d- Z/d.d/ Z0d0d1 Z1d2d3 Z2d4d5 Z3ddd6d7Z4		9ded:d;Z5d<d= Z6dfd>d?Z7d@dA Z8	dgdCdDZ9h dEZ:dddFdGZ;dHdI Z<dJdK Z=dddLdMZ>	dddNdOZ?dPdQ Z@dRdS ZAddTdTej#ej$ej#ejej"ejBdU	ZCdVdW ZDdXdY ZEdZd[ ZFd\d] ZGd^d_ ZHd`da ZIdbdc ZJdS )h    N)Sequence)futures)deepcopy)zip_longest)_pandas_api	frombytesc                	   C   s   t sjt i tjjdtjjdtjjdtjjdtjjdtjj	dtjj
dtjjdtjjd	tjjd
tjjdtjjdtjjdtjjdtjjdtjjdtjjdtjjdtjjdtjjdi t S )Nemptyboolint8int16int32int64uint8uint16uint32uint64float16float32float64datetimebytesunicode)_logical_type_mapupdatepalibZType_NAZ	Type_BOOLZ	Type_INT8Z
Type_INT16Z
Type_INT32Z
Type_INT64Z
Type_UINT8ZType_UINT16ZType_UINT32ZType_UINT64ZType_HALF_FLOATZ
Type_FLOATZType_DOUBLEZType_DATE32ZType_DATE64ZType_TIME32ZType_TIME64ZType_BINARYZType_FIXED_SIZE_BINARYZType_STRING r   r   L/var/www/html/lang_env/lib/python3.10/site-packages/pyarrow/pandas_compat.pyget_logical_type_map)   sR   	
r   c                 C   s   t  }z|| j W S  tyL   t| tjjrY dS t| tjjr+dt	| j
 Y S t| tjjr?| jd ur;d Y S d Y S t| tjjrIY dS Y dS w )NZcategoricalzlist[{}]
datetimetzdatetimedecimalobject)r   idKeyError
isinstancer   r   ZDictionaryTypeZListTypeformatget_logical_typeZ
value_typeTimestampTypetzZDecimal128Type)
arrow_typeZlogical_type_mapr   r   r   r(   F   s   r(   r	   r
   r   r   r   r   r   r   r   r   r   zdatetime64[D]r   stringr   c                 C   sd   zt | jj W S  ty1   t| jdrY dS t| jdkr!Y dS t| }|dkr-Y dS | Y S w )Nr*   r    datetime64[ns]r,   r   )_numpy_logical_type_mapdtypetyper%   hasattrstrr   Zinfer_dtype)Zpandas_collectionresultr   r   r   get_logical_type_from_numpyi   s   
r4   c                 C   s   | j }t|dkr(t| d| }|d usJ t|j|jd}t|jj }||fS t|dr<dtj	
|ji}d}||fS d }t|}||fS )Ncategorycat)Znum_categoriesorderedr*   timezoner-   )r/   r2   getattrlen
categoriesr7   codesr1   r   r   Ztzinfo_to_stringr*   )columnr/   ZcatsmetadataZphysical_dtyper   r   r   get_extension_dtype_infox   s    
r?   c                 C   s   t |}t| \}}|dkr|j|jd}d}|dur+t|ts+td|t|j	|du s<t|ts<J tt|||du rCdn||||dS )a  Construct the metadata for a given column

    Parameters
    ----------
    column : pandas.Series or pandas.Index
    name : str
    arrow_type : pyarrow.DataType
    field_name : str
        Equivalent to `name` when `column` is a `Series`, otherwise if `column`
        is a pandas Index then `field_name` will not be the same as `name`.
        This is the name of the field in the arrow Table's schema.

    Returns
    -------
    dict
    r"   )	precisionscaler#   Nz6Column name must be a string. Got column {} of type {}Nonename
field_namepandas_type
numpy_typer>   )
r(   r?   r@   rA   r&   r2   	TypeErrorr'   r0   __name__)r=   rD   r+   rE   Zlogical_typestring_dtypeextra_metadatar   r   r   get_column_metadata   s,   

rL   c              	   C   s  t dd |D }t |}|d||  }	||| d }
g }t| ||	D ]\}}}t||||d}|| q%g }|durg }t||
|D ],\}}}t|trQqF|jdurbt|jtsb||j t|t|j||d}|| qFt |dkrt	j
d| dtd	d
 g }t|jd|jg}t|jd|jjg}t||D ]\}}t||}|| qng  } }}dt|||| dtjdtjddiS )a  Returns a dictionary containing enough metadata to reconstruct a pandas
    DataFrame as an Arrow Table, including index columns.

    Parameters
    ----------
    columns_to_convert : list[pd.Series]
    df : pandas.DataFrame
    index_levels : List[pd.Index]
    index_descriptors : List[Dict]
    preserve_index : bool
    types : List[pyarrow.DataType]

    Returns
    -------
    dict
    c                 S   s   g | ]	}t |ts|qS r   )r&   dict).0descrr   r   r   
<listcomp>       
z&construct_metadata.<locals>.<listcomp>N)rD   r+   rE   Fr   z&The DataFrame has non-str index name `z@` which will be converted to string and not roundtrip correctly.   
stacklevellevelsnamess   pandaspyarrow)Zlibraryversion)index_columnscolumn_indexescolumnsZcreatorZpandas_versionutf8)r:   ziprL   appendr&   rM   rD   r2   _column_name_to_stringswarningswarnUserWarningr9   r[   _get_simple_index_descriptorjsondumpsr   __version__r   rX   encode)columns_to_convertdfcolumn_namesindex_levelsindex_descriptorspreserve_indextypesZnum_serialized_index_levelsZntypesZdf_typesZindex_typesZcolumn_metadatacolZsanitized_namer+   r>   Zindex_column_metadataZnon_str_index_nameslevelZ
descriptorrZ   rU   rV   rD   r   r   r   construct_metadata   sl   


	rq   c                 C   sT   t | \}}t| }d|v rtjdtdd |dkr"|rJ ddi}|||||dS )	NmixedzlThe DataFrame has column names of mixed type. They will be converted to strings and not roundtrip correctly.rR   rS   r   encodingUTF-8rC   )r?   r4   r`   ra   rb   )rp   rD   rJ   rK   rF   r   r   r   rc     s    rc   c                 C   sd   t | tr| S t | tr| dS t | trtttt| S t | tr(td| du r.dS t| S )a!  Convert a column name (or level) to either a string or a recursive
    collection of strings.

    Parameters
    ----------
    name : str or tuple

    Returns
    -------
    value : str or tuple

    Examples
    --------
    >>> name = 'foo'
    >>> _column_name_to_strings(name)
    'foo'
    >>> name = ('foo', 'bar')
    >>> _column_name_to_strings(name)
    "('foo', 'bar')"
    >>> import pandas as pd
    >>> name = (1, pd.Timestamp('2017-02-01 00:00:00'))
    >>> _column_name_to_strings(name)
    "('1', '2017-02-01 00:00:00')"
    r\   z%Unsupported type for MultiIndex levelN)	r&   r2   r   decodetuplemapr_   r   rH   rD   r   r   r   r_   "  s   




r_   c                 C   s(   | j dur| j |vrt| j S d|S )zReturn the name of an index level or a default name if `index.name` is
    None or is already a column name.

    Parameters
    ----------
    index : pandas.Index
    i : int

    Returns
    -------
    name : str
    Nz__index_level_{:d}__)rD   r_   r'   )indexirj   r   r   r   _index_level_nameI  s   

r{   c                 C   s:  t | ||}| jjstdt| j|d urt| ||S g }|dur)t| jng }g }g }|D ]%}| | }	t	|}t
|	rGtd|||	 |d  || q1g }
g }t|D ]0\}}t|||}t|t
jjry|d u ryt|}n|| |d  |}|| |
| q_|| }||||
|||fS )Nz Duplicate column names found: {}F-Sparse pandas data (column {}) not supported.)_resolve_columns_of_interestr[   	is_unique
ValueErrorr'   list$_get_columns_to_convert_given_schema_get_index_level_valuesry   r_   r   	is_sparserH   r^   	enumerater{   r&   pd
RangeIndex_get_range_index_descriptor)ri   schemarm   r[   rj   rk   rh   convert_fieldsrD   ro   rl   index_column_namesrz   index_levelrO   	all_namesr   r   r   _get_columns_to_convert\  sN   






r   c                 C   s:  g }g }g }g }g }g }|j D ]}	z| |	 }
d}W n@ tyY   zt| |	}
W n ttfy7   td|	w |du rCtd|	|du rUt|
tjj	rUtd|	d}Y nw t
|	}	t|
rjtd|	||	}||
 || ||	 |r||	 ||	 ||
 q|| }|||||||fS )z
    Specialized version of _get_columns_to_convert in case a Schema is
    specified.
    In that case, the Schema is used as the single point of truth for the
    table structure (types, which columns are included, order of columns, ...).
    FzNname '{}' present in the specified schema is not found in the columns or indexzlname '{}' present in the specified schema corresponds to the index, but 'preserve_index=False' was specifiedNa  name '{}' is present in the schema, but it is a RangeIndex which will not be converted as a column in the Table, but saved as metadata-only not in columns. Specify 'preserve_index=True' to force it being added as a column, or remove it from the specified schemaTr|   )rV   r%   _get_index_level
IndexErrorr'   r   r&   r   r   r   r_   r   rH   fieldr^   )ri   r   rm   rj   rh   r   rl   r   rk   rD   ro   Zis_indexr   r   r   r   r   r     sf   








r   c                 C   s8   |}|| j jvrt|rt|tdd }| j |S )z_
    Get the index level of a DataFrame given 'name' (column name in an arrow
    Schema).
    Z__index_level_)ry   rV   _is_generated_index_nameintr:   get_level_values)ri   rD   keyr   r   r   r     s   r   c                 C   s,   zt |  | W S  ty   t|  Y S w N)rd   re   rH   r2   rx   r   r   r   _level_name  s   
r   c                 C   s.   dt | jt| dt| dt| ddS )Nrangestartstopstep)kindrD   r   r   r   )r   rD   r   Zget_rangeindex_attribute)rp   r   r   r   r     s   


r   c                    s(   t t d g} fddt|D S )NrU   c                    s   g | ]}  |qS r   )r   )rN   rz   ry   r   r   rP     s    z+_get_index_level_values.<locals>.<listcomp>)r:   r9   r   )ry   nr   r   r   r     s   r   c                    sR   |d ur|d urt d|d ur|j}|S |d ur$ fdd|D }|S  j}|S )NzJSchema and columns arguments are mutually exclusive, pass only one of themc                    s   g | ]	}| j v r|qS r   )r[   rN   cri   r   r   rP   	  s    z0_resolve_columns_of_interest.<locals>.<listcomp>)r   rV   r[   )ri   r   r[   r   r   r   r}     s   r}   c                 C   s   t | d ||\}}}}}}}g }	|D ]T}
|
j}t|r%tj|
ddj}n<t|rEt|
tj	j
r6|
dn|
d d }tj|ddj}nt||
jd \}}tj||}|d u ratj|
ddj}|	| qt|| |||||	}||	|fS )NT)from_pandasr   )r   valuesr   Zis_categoricalr   arrayr0   Zis_extension_array_dtyper&   r   Seriesheadget_datetimetz_typer/   r   Z_ndarray_to_arrow_typer^   rq   )ri   rm   r[   r   rj   _rl   rY   rh   rn   r   r   type_r   r>   r   r   r   dataframe_to_types  s<   



r      Tc           !   	      sD  t | |||\}}}}	}
}}|d u r-t| t| j}}||d kr+|dkr+t }nd}fdd dd }|dkrH fddt||D }nNg }t|*}t||D ]\}}||jrg|	 || qU|	|
 || qUW d    n1 s|w   Y  t|D ]\}}t|tjr| ||< qd	d |D }|d u rg }t||D ]\}}|d ur|nd
}|	t|| qt|}t|| ||
|	||}|jrt|jnt }|| ||}d }t|dkrz'|	d d }|dkr|	d d }|	d d }|	d d } tt||| }W n
 ty   Y nw |||fS )Nd   r   c              
      s   |d u r	d}d }n|j }|j}ztj| |d d}W n" tjtjtjfy< } z| jd| j	| j
f7  _|d }~ww |sO|jdkrOtdt||j|S )NT)r0   r   safez0Conversion failed for column {!s} with type {!s}r   z>Field {} was non-nullable but pandas column had {} null values)Znullabler0   r   r   ZArrowInvalidZArrowNotImplementedErrorZArrowTypeErrorargsr'   rD   r/   Z
null_countr   r2   )ro   r   Zfield_nullabler   r3   e)r   r   r   convert_columnG  s0   z+dataframe_to_arrays.<locals>.convert_columnc                 S   s$   t | tjo| jjot| jjtjS r   )	r&   npZndarrayflags
contiguous
issubclassr/   r0   integer)arrr   r   r   _can_definitely_zero_copy]  s
   z6dataframe_to_arrays.<locals>._can_definitely_zero_copyc                    s   g | ]	\}} ||qS r   r   )rN   r   f)r   r   r   rP   c  s    z'dataframe_to_arrays.<locals>.<listcomp>c                 S   s   g | ]}|j qS r   r0   rN   xr   r   r   rP   r  s    rB   r   r   r   r   r   r   )r   r:   r[   r   	cpu_countr]   r   ZThreadPoolExecutorr   r^   Zsubmitr   r&   Futurer3   r   r   rq   r>   r   rM   r   with_metadatar   r   )!ri   r   rm   Znthreadsr[   r   r   rj   r   rl   rY   rh   r   ZnrowsZncolsr   Zarraysexecutorr   r   rz   Z	maybe_futrn   fieldsrD   r   pandas_metadatar>   Zn_rowsr   r   r   r   r   )r   r   r   dataframe_to_arrays2  sz   







r   c                 C   sd   | j jtjkr| |fS t|r$|d u r$|j}|j}t	||}| |fS |d u r.t
| j }| |fS r   )r/   r0   r   Z
datetime64r   Zis_datetimetzr*   unitr   	timestampZfrom_numpy_dtype)r   r/   r   r*   r   r   r   r   r     s   r   c                 C   s>  ddl m  m} | dd}| d }d| v r-tjj|| d | d d}|j||d}|S d	| v ret	|j
\}}	t|| d	 }
t rYtjj|d
|
dd}|j||d}|S |j|||j|
d}|S d| v r| d }t|dksuJ ||d  }|| }t|dstd||}|j||d}|S |j||d}|S )a  
    Construct a pandas Block from the `item` dictionary coming from pyarrow's
    serialization or returned by arrow::python::ConvertTableToPandas.

    This function takes care of converting dictionary types to pandas
    categorical, Timestamp-with-timezones to the proper pandas Block, and
    conversion to pandas ExtensionBlock

    Parameters
    ----------
    item : dict
        For basic types, this is a dictionary in the form of
        {'block': np.ndarray of values, 'placement': pandas block placement}.
        Additional keys are present for other types (dictionary, timezone,
        object).
    columns :
        Column names of the table being constructed, used for extension types
    extension_columns : dict
        Dictionary of {column_name: pandas_dtype} that includes all columns
        and corresponding dtypes that will be converted to a pandas
        ExtensionBlock.

    Returns
    -------
    pandas Block

    r   Nblock	placement
dictionaryr7   )r;   r7   )r   r8   r   Fr/   copy)r   klassr/   Zpy_arrayr   __from_arrow__zGThis column does not support to be converted to a pandas ExtensionArray)pandas.core.internalscoreZ	internalsgetr   Zcategorical_typeZ
from_codesZ
make_blockr   Zdatetime_datar/   make_datetimetz	is_ge_v21r   r   viewZDatetimeTZBlockr:   r1   r   r   )itemr[   extension_columns_intZ	block_arrr   r6   r   r   r   r/   Zpd_arrr   rD   pandas_dtypeZ
pd_ext_arrr   r   r   _reconstruct_block  sH   

r   c                 C   s&   t  rd} tj|}t j| |dS )Nnsr*   )r   Zis_v1r   r   string_to_tzinfoZdatetimetz_type)r   r*   r   r   r   r     s   r   Fc                 C   s   ddl m} ddlm} g }g }|jj}	|s=|	d ur=|	d }|	dg }|	d }
t||	}t||
||\}}t	|||}nt
j|j}t	|g |}t| t|||}t| |||}||g}|||}t
 rq|||j}|S ||}|S )Nr   )BlockManager)	DataFramer[   rZ   rY   )r   r   Zpandasr   r   r   r   _add_any_metadata_reconstruct_index_get_extension_dtypesr   r   r   num_rows'_check_data_column_metadata_consistency_deserialize_column_index_table_to_blocksr   Z	_from_mgraxes)optionstabler;   Zignore_metadatatypes_mapperr   r   all_columnsrZ   r   rl   ry   Zext_columns_dtypesr[   blocksr   Zmgrri   r   r   r   table_to_dataframe  s8   

r   >   r   r   r   r   r   r   r   r#   r   r   r
   r	   r   c           
   	   C   s   i }t jdu r	|S |D ]2}z|d }W n ty    |d }Y nw |d }|tvr=t |}t|t jr=t|dr=|||< q| jD ] }|j}	t|	t	j
raz|	 }W n	 ty[   Y qAw |||j< qA|rz| jD ]}|j}	||	}|dury|||j< qg|S )a  
    Based on the stored column pandas metadata and the extension types
    in the arrow schema, infer which columns should be converted to a
    pandas extension dtype.

    The 'numpy_type' field in the column metadata stores the string
    representation of the original pandas dtype (and, despite its name,
    not the 'pandas_type' field).
    Based on this string representation, a pandas/numpy dtype is constructed
    and then we can check if this dtype supports conversion from arrow.

    NrE   rD   rG   r   )r   Zextension_dtyper%   _pandas_supported_numpy_typesr   r&   r1   r   r0   r   ZBaseExtensionTypeZto_pandas_dtypeNotImplementedErrorrD   )
r   Zcolumns_metadatar   Zext_columnscol_metarD   r/   r   r   typr   r   r   r     sD   






r   c                 C   s   t dd | D sJ d S )Nc                 s   s0    | ]}|d  du rd|v p|d  duV  qdS )rD   NrE   r   r   r   r   r   	<genexpr>\  s
    
z:_check_data_column_metadata_consistency.<locals>.<genexpr>)all)r   r   r   r   r   W  s   r   c                    s   dd | j D }|rdd |D   fdd|D }n|}t|dkr&tjndd }|s3tj|}ntjjjt	t
||d	d |D pEd d
}t|dkrSt||}t|}|S )Nc                 S   s"   g | ]}t |trt|n|qS r   )r&   r   r   r   r   r   r   rP   c  s    z-_deserialize_column_index.<locals>.<listcomp>c                 S   s&   i | ]}| d t|d |d qS rE   rD   )r   r_   r   r   r   r   
<dictcomp>f  s    z-_deserialize_column_index.<locals>.<dictcomp>c                    s   g | ]}  ||qS r   r   )rN   rD   Zcolumns_name_dictr   r   rP   j  s    r   c                 S   s   | fS r   r   )r   r   r   r   <lambda>s  s    z+_deserialize_column_index.<locals>.<lambda>c                 S   s   g | ]}|d  qS rx   r   )rN   	col_indexr   r   r   rP   }  s    rV   r   )rj   r:   astliteral_evalr   r   Index
MultiIndexfrom_tuplesr   rw   "_reconstruct_columns_from_metadata _flatten_single_level_multiindex)block_tabler   rZ   Zcolumn_stringsZcolumns_valuesZto_pairr[   r   r   r   r   b  s,   

r   c                 C   s6  dd |D }g }g }| }|D ]O}t |tr't| ||||\}}	}
|	d u r&qn-|d dkrK|d }
tjj|d |d |d |
d	}	t|	t| krJqn	td
|d |	|	 |	|
 qtj}t|dkrt|j
j||d}||fS t|dkr|d }t ||js|j||d d}||fS || j}||fS )Nc                 S   s   i | ]}| d |d |qS r   r   r   r   r   r   r     s    z&_reconstruct_index.<locals>.<dictcomp>r   r   rD   r   r   r   )r   rD   zUnrecognized index kind: {}r   r   r   rx   )r&   r2   _extract_index_levelr   r   r   r:   r   r'   r^   r   from_arraysr   r   )r   rl   r   r   field_name_to_metadataZindex_arraysZindex_namesresult_tablerO   r   
index_namer   ry   r   r   r   r     sP   




	r   c                 C   s   || d }t ||}| j|}|dkr|d d fS tj}| |}	|	j|dj}
t|
dr6|
j	j
s6|
 }
t|	jtjjrQ|	jjd urQt|j|
dd|	jj}n	|j|
|
jdd}||j|}|||fS )NrD   )r   r   F)r   r   ) _backwards_compatible_index_namer   get_field_indexr   r   r=   	to_pandasr   r1   r   Z	writeabler   r&   r0   r   r   r)   r*   make_tz_awarer   r/   Zremove_column)r   r  rE   r  r   logical_namer  rz   r   ro   r   r   r   r   r   r     s"   




r   c                 C   s   | |kr
t | r
dS |S )a1  Compute the name of an index column that is compatible with older
    versions of :mod:`pyarrow`.

    Parameters
    ----------
    raw_name : str
    logical_name : str

    Returns
    -------
    result : str

    Notes
    -----
    * Part of :func:`~pyarrow.pandas_compat.table_to_blockmanager`
    N)r   )raw_namer	  r   r   r   r    s   r  c                 C   s   d}t || d uS )Nz^__index_level_\d+__$)rematch)rD   patternr   r   r   r     s   r   r-   )	r   r!   r    r   r   r,   r   Zfloatingr   c                 C   s:   zt |  W S  ty   d| v rtj Y S t|  Y S w )a  Get the numpy dtype that corresponds to a pandas type.

    Parameters
    ----------
    pandas_type : str
        The result of a call to pandas.lib.infer_dtype.

    Returns
    -------
    dtype : np.dtype
        The dtype that corresponds to `pandas_type`.
    rr   )_pandas_logical_type_mapr%   r   object_r/   )rF   r   r   r   _pandas_type_to_numpy_type  s   

r  c                 C   s   t | tjjr
| jS d S r   )r&   r   r   r   r<   )mir   r   r   _get_multiindex_codes  s   r  c                    s  t j t| ddp| g}t| p fdd|D }dd t||i dD }g }tdd}|D ]K\}}}	t|}
|
tj	krB|
|}|d	kr]tj|d
 d d } j|dd|}n
|j|
krg||
}|j|	kru|d	kru||	}|| q/ j||| jdS )a_  Construct a pandas MultiIndex from `columns` and column index metadata
    in `column_indexes`.

    Parameters
    ----------
    columns : List[pd.Index]
        The columns coming from a pyarrow.Table
    column_indexes : List[Dict[str, str]]
        The column index metadata deserialized from the JSON schema metadata
        in a :class:`~pyarrow.Table`.

    Returns
    -------
    result : MultiIndex
        The index reconstructed using `column_indexes` metadata with levels of
        the correct type.

    Notes
    -----
    * Part of :func:`~pyarrow.pandas_compat.table_to_blockmanager`
    rU   Nc                    s   g | ]	}  t|qS r   )r   r:   )rN   rp   r   r   r   rP   ?  s    z6_reconstruct_columns_from_metadata.<locals>.<listcomp>c                 S   s0   g | ]\}}|| d t|j| ddfqS )rF   rG   N)r   r2   r/   )rN   rp   r   r   r   r   rP   D  s    
)	fillvaluerg   rt   r    r   r>   r8   T)utcr   )r   r   r9   r  r   operatormethodcallerr  r   bytes_rw   r   r   r   to_datetime
tz_convertr/   Zastyper^   r   rV   )r[   rZ   rU   labelsZlevels_dtypesZ
new_levelsencoderrp   r   Znumpy_dtyper/   r*   r   r  r   r   %  s6   




r   c                    s4   |j  tj| ||t } fdd|D S )Nc                    s   g | ]}t | qS r   )r   )rN   r   r[   r   r   r   rP   m  s    z$_table_to_blocks.<locals>.<listcomp>)rj   r   r   Ztable_to_blocksr   keys)r   r   r;   r   r3   r   r  r   r   f  s   
r   c                    sj   t j}t| |jr3| jdkr3| j\ t| \} j}| js!t	d|j
 fdd|D || jd dS | S )Nr   zFound non-unique column indexc                    s    g | ]}|d kr | ndqS )r  Nr   )rN   Z_labelrU   r   r   rP   ~  s     z4_flatten_single_level_multiindex.<locals>.<listcomp>r   )r/   rD   )r   r   r&   r   ZnlevelsrU   r  r/   r~   r   r   rV   )ry   r   r  r/   r   r  r   r   q  s   
r   c                 C   s  i }i }| j }|d }dd |D }t|}t|d | }t|d D ]q\}}	|	d}
|
sC|	d }
||kr=|||  }
|
d u rCd}
||
}|dkr|	d	 d
kr| | }t|jtjj	s_q$|	d }|sfq$|d}|r||jj
kr| }tjd|d}tjj||d}t|| j|||< |||< q$t|dkrg }g }tt| j D ]$}||v r|||  |||  q|| |  || j |  qtjj|t |dS | S )NrY   c                 S   s   g | ]	}t |tr|qS r   )r&   r2   )rN   Zidx_colr   r   r   rP     rQ   z%_add_any_metadata.<locals>.<listcomp>r[   rE   rD   rB   r  rF   r    r>   r8   r   r   r   r   )r   )r   r:   r   r   r  r&   r0   r   r   r)   r*   r  r   ZArrayr   r   rD   r   r^   Tabler   )r   r   Zmodified_columnsZmodified_fieldsr   rY   Zn_index_levelsZ	n_columnsrz   r   r
  idxro   r>   Zmetadata_tzZ	convertedZtz_aware_typer   r[   r   r   r   r   r     s^   


r   c                 C   s$   t j|}| jdj|} | S )zB
    Make a datetime64 Series timezone-aware for the given tz
    r  )r   r   r   dtZtz_localizer  )Zseriesr*   r   r   r   r    s
   
r  r   )r   NT)NN)NFN)Kr   collections.abcr   Z
concurrentr   Zconcurrent.futures.threadr   r   	itertoolsr   rd   r  r  r`   numpyr   rW   r   Zpyarrow.libr   r   r   r   r(   Zbool_r
   r   r   r   r   r   r   r   r   r   Zstr_r  r.   r4   r?   rL   rq   rc   r_   r{   r   r   r   r   r   r   r}   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r  r  r  r  r   r   r   r   r  r   r   r   r   <module>   s   -V'@@	
"
b
B
&
9
(7
AA