o
    h                     @   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Zd dl	m
Z
 d dlZd dlZd dlZd dlZzd dlZW n eyE   dZY nw d dlZd dlmZmZmZ i ai ai add Zd	d
 Zdd Zdd Zdd Zdd Z 	dQd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( Z+dQd)d*Z,		,dRd-d.Z-d/d0 Z.dSd1d2Z/d3d4 Z0	dTd6d7Z1h d8Z2d9d: Z3d;d< Z4d=d> Z5dQd?d@Z6	dQdAdBZ7dCdD Z8dEdF Z9dGdH Z:dIdJ Z;dKdL Z<dMdN Z=dOdP Z>dS )U    N)Sequence)futures)deepcopy)zip_longest)_pandas_api	frombytesis_threading_enabledc                	   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libType_NA	Type_BOOL	Type_INT8
Type_INT16
Type_INT32
Type_INT64
Type_UINT8Type_UINT16Type_UINT32Type_UINT64Type_HALF_FLOAT
Type_FLOATType_DOUBLEType_DATE32Type_DATE64Type_TIME32Type_TIME64Type_BINARYType_FIXED_SIZE_BINARYType_STRING r2   r2   I/var/www/vscode/kcb/lib/python3.10/site-packages/pyarrow/pandas_compat.pyget_logical_type_map.   sR   	
r4   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 )Ncategoricalzlist[{}]
datetimetzdatetimedecimalobject)r4   idKeyError
isinstancer   r   DictionaryTypeListTypeformatget_logical_type
value_typeTimestampTypetzDecimal128Type)
arrow_typelogical_type_mapr2   r2   r3   r@   K   s   r@   c                   C   sd   t s0t tjdtjdtjdtjdtjdtjdtj	dtj
dtjd	tjd
tjdddtjdtjdi t S )Nr
   r   r   r   r   r   r   r   r   r   r   datetime64[D]r   stringr   )_numpy_logical_type_mapr   npbool_r   r   r   r   r   r   r   r   r   r   str_bytes_r2   r2   r2   r3   get_numpy_logical_type_map\   s$   rN   c                 C   st   t  }z|| jj W S  ty9   t| jdrY dS t| jdr)t| j Y S t| }|dkr5Y dS | Y S w )NrC   r6   
datetime64rH   r   )	rN   dtypetyper;   hasattrstr
startswithr   infer_dtype)pandas_collectionnumpy_logical_type_mapresultr2   r2   r3   get_logical_type_from_numpyr   s   
rY   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)num_categoriesorderedrC   timezonedatetime64[ns])rP   rS   getattrlen
categoriesr]   codesrR   r   r   tzinfo_to_stringrC   )columnrP   catsmetadataphysical_dtyper2   r2   r3   get_extension_dtype_info   s    
ri   c                 C   s   t |}t| \}}|dkr|j|jd}d}|dur5t|tr%t|s5t|ts5t	d
|t|jt|tsBJ tt||||||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
    r8   )	precisionscaler9   Nz6Column name must be a string. Got column {} of type {}name
field_namepandas_type
numpy_typerg   )r@   ri   rj   rk   r<   floatrJ   isnanrS   	TypeErrorr?   rQ   __name__)re   rm   rE   rn   logical_typestring_dtypeextra_metadatar2   r2   r3   get_column_metadata   s4   
rx   c              	   C   s  |du rdd |D }t dd |D }t |}	|d|	|  }
||	| d }g }t| |||
D ]\}}}}t||||d}|| q1g }|durg }t|||D ],\}}}t|tr^qS|jdurot|jtso||j t|t|j||d}|| qSt |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
    column_names : list[str | None]
    column_field_names: list[str]
    index_levels : List[pd.Index]
    index_descriptors : List[Dict]
    preserve_index : bool
    types : List[pyarrow.DataType]

    Returns
    -------
    dict
    Nc                 S   s   g | ]}t |qS r2   )rS   .0rm   r2   r2   r3   
<listcomp>       z&construct_metadata.<locals>.<listcomp>c                 S   s   g | ]	}t |ts|qS r2   )r<   dict)rz   descrr2   r2   r3   r{          
)rm   rE   rn   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)libraryversion)index_columnscolumn_indexescolumnscreatorpandas_versionutf8)ra   ziprx   appendr<   r}   rm   rS   _column_name_to_stringswarningswarnUserWarningr`   r   _get_simple_index_descriptorjsondumpsr   __version__r   r   encode)columns_to_convertdfcolumn_namesindex_levelsindex_descriptorspreserve_indextypescolumn_field_namesnum_serialized_index_levelsntypesdf_typesindex_typescolumn_metadatacolrm   rn   rE   rg   index_column_metadatanon_str_index_nameslevel
descriptorr   r   r   r2   r2   r3   construct_metadata   sp   


	r   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.r   r   r   encodingUTF-8rl   )ri   rY   r   r   r   )r   rm   rv   rw   ro   r2   r2   r3   r   %  s    r   c                 C   sx   t | tr| S t | tr| dS t | trtttt| S t | tr(td| du s6t | t	r8t
| r8| 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<   rS   r   decodetuplemapr   r   rs   rq   rJ   rr   rm   r2   r2   r3   r   9  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}__)rm   r   r?   )indexir   r2   r2   r3   _index_level_name`  s   

r   c                 C   sN  t | ||}| jjstdt| j|d urt| ||S g }g }|dur+t| jng }g }g }|D ],}	| |	 }
t	|	}	t
|
rItd|	||
 |d  ||	 |t|	 q3g }g }t|D ]0\}}t|||}	t|t
jjr|d u rt|}n|| |d  |	}||	 || qh|| }||||||||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_valuesr   r   r   	is_sparsers   r   rS   	enumerater   r<   pd
RangeIndex_get_range_index_descriptor)r   schemar   r   r   r   r   r   convert_fieldsrm   r   r   index_column_namesr   index_levelr~   	all_namesr2   r2   r3   _get_columns_to_converts  sR   







r   c                 C   s4  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
|
rft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   )r   r;   _get_index_level
IndexErrorr?   r   r<   r   r   r   r   rs   fieldr   )r   r   r   r   r   r   r   r   r   rm   r   is_indexr   r   r2   r2   r3   r     sd   








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).
    __index_level_)r   r   _is_generated_index_nameintra   get_level_values)r   rm   keyr2   r2   r3   r     s   r   c                 C   s,   zt |  | W S  ty   t|  Y S w N)r   r   rs   rS   r   r2   r2   r3   _level_name   s   
r   c                 C   s.   dt | jt| dt| dt| ddS )Nrangestartstopstep)kindrm   r   r   r   )r   rm   r   get_rangeindex_attribute)r   r2   r2   r3   r   	  s   


r   c                    s(   t t d g} fddt|D S )Nr   c                    s   g | ]}  |qS r2   )r   rz   r   r   r2   r3   r{         z+_get_index_level_values.<locals>.<listcomp>)ra   r`   r   )r   nr2   r   r3   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 r2   )r   rz   cr   r2   r3   r{      s    z0_resolve_columns_of_interest.<locals>.<listcomp>)r   r   r   )r   r   r   r2   r   r3   r     s   r   c              
   C   s   t | d ||\}}}}}}}	}g }
|	D ]T}|j}t|r&tj|ddj}n<t|rFt|tj	j
r7|dn|d d }tj|ddj}nt||jd \}}tj||}|d u rbtj|ddj}|
| qt|	| |||||
|d}||
|fS )NT)from_pandasr   r   )r   valuesr   is_categoricalr   arrayrQ   is_extension_array_dtyper<   r   Seriesheadget_datetimetz_typerP   r   _ndarray_to_arrow_typer   r   )r   r   r   r   r   r   _r   r   r   r   r   r   type_r	   rg   r2   r2   r3   dataframe_to_types'  s>   




r      Tc           "   
      sD  t | |||\}}}}	}
}}}|d u r.t| t| j}}||d kr,|dkr,t }nd}t s3d}fdd dd }|dkrN fddt||D }nNg }t|*}t||D ]\}}||j	rm|
 || q[|
| || q[W d    n1 sw   Y  t|D ]\}}t|tjr| ||< qd	d |D }|d u rg }t||D ]\}}|
t|| qt|}t|| |||
|||d
}|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)rQ   r   safez0Conversion failed for column {!s} with type {!s}r   z>Field {} was non-nullable but pandas column had {} null values)nullablerQ   r   r   ArrowInvalidArrowNotImplementedErrorArrowTypeErrorargsr?   rm   rP   
null_countr   rS   )r   r   field_nullabler   rX   e)r   r2   r3   convert_columnc  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<   rJ   ndarrayflags
contiguous
issubclassrP   rQ   integer)arrr2   r2   r3   _can_definitely_zero_copyy  s
   z6dataframe_to_arrays.<locals>._can_definitely_zero_copyc                    s   g | ]	\}} ||qS r2   r2   )rz   r   f)r   r2   r3   r{     s    z'dataframe_to_arrays.<locals>.<listcomp>c                 S   s   g | ]}|j qS r2   rQ   )rz   xr2   r2   r3   r{     s    r   r   r   r   r   r   r   )r   ra   r   r   	cpu_countr   r   r   ThreadPoolExecutorr   r   submitr   r<   FuturerX   r   r   r   rg   r   r}   r   with_metadatar   r   )"r   r   r   nthreadsr   r   r   r   r   r   r   r   r   r   nrowsncolsr   arraysexecutorr   r  r   	maybe_futr   fieldsrm   r   pandas_metadatarg   n_rowsr   r   r   r   r2   )r   r   r3   dataframe_to_arraysJ  s~   	








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   )rP   rQ   rJ   rO   r   is_datetimetzrC   unitr   	timestampfrom_numpy_dtype)r   rP   r   rC   r  r2   r2   r3   r     s   r   c                 C   s$  ddl m  m} | dd}| d }d| v r%tjj|| d | d d}n`d| v rZt|j	\}}	t
|| d }
t rItjj|d	|
d
d}n<|}|rY|j|||j|
d}|S n+d| v r| d }t|dksjJ ||d  }|| }t|ds}td||}n|}|r|j||dS ||f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
dictionaryr]   )rb   r]   r^   r   F)rP   copy)r  klassrP   py_arrayr   __from_arrow__zGThis column does not support to be converted to a pandas ExtensionArray)r  )pandas.core.internalscore	internalsgetr   categorical_type
from_codesrJ   datetime_datarP   make_datetimetz	is_ge_v21r   r   view
make_blockDatetimeTZBlockra   rR   r   r  )itemr   extension_columnsreturn_block_int	block_arrr  r   r  r   rP   r  rm   pandas_dtyper2   r2   r3   _reconstruct_block  sF   
r0  c                 C   s&   t  rd} tj|}t j| |dS )NnsrC   )r   is_v1r   r   string_to_tzinfodatetimetz_type)r  rC   r2   r2   r3   r%    s   r%  Fc                    sV  g }g }|j j}|s3|d ur3|d }|dg }|d }t||}t||||\}}	t|||| |ntj|j	}	t|g || |t
| t|||}
|j tj| ||t }t ryddlm}  fdd|D }|||	|
d}|S dd	lm} dd
lm}  fdd|D }|
|	g}|||}t r|||j}|S ||}|S )Nr   r   r   r   )create_dataframe_from_blocksc                    s   g | ]
}t | d dqS )F)r,  r0  rz   r*  r   ext_columns_dtypesr2   r3   r{   -  s    z&table_to_dataframe.<locals>.<listcomp>)r   r   )BlockManager)	DataFramec                    s   g | ]}t | qS r2   r7  r8  r9  r2   r3   r{   8  s    
)r   r  r!  _add_any_metadata_reconstruct_index_get_extension_dtypesr   r   r   num_rows'_check_data_column_metadata_consistency_deserialize_column_indexr   r   r   table_to_blocksr   keysis_ge_v3pandas.api.internalsr6  r  r;  pandasr<  r&  	_from_mgraxes)optionstablerb   ignore_metadatatypes_mapperall_columnsr   r  r   r   r   rX   r6  blocksr   r;  r<  rI  mgrr2   r9  r3   table_to_dataframe  sV   




rQ  >   r
   r   r   r   r   r   r9   r   r   r   r   r   r   c              	   C   s  |d }|pg }i }t jdu r|S |r)| jD ]}|j}||}	|	dur(|	||j< q| jD ]%}|j}|j|vrQt|tjrQz| }	W n	 t	yK   Y q,w |	||j< q,|D ]]}
z|
d }W n t
yi   |
d }Y nw |
d }||vr|tvrt |}	t|	t jrt|	t jjr|s||v rqTztj| j|jrW qTW n	 t
y   Y nw t|	dr|	||< qTt  r|s| jD ],}|j|vrtj|jstj|jstj|jr|j|vrt jjtjd||j< q|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.

    strings_to_categoricalNrn   rm   rp   r  )na_value)r   extension_dtyper   rQ   rm   r<   r   BaseExtensionTypeto_pandas_dtypeNotImplementedErrorr;   _pandas_supported_numpy_typesr/  r   StringDtyper   is_dictionaryr   rR   uses_string_dtype	is_stringis_large_stringis_string_viewrJ   nan)rK  columns_metadatarM  rJ  rb   rR  ext_columnsr   typr/  col_metarm   rP   r2   r2   r3   r?  O  st   









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 )rm   Nrn   r2   r   r2   r2   r3   	<genexpr>  s
    
z:_check_data_column_metadata_consistency.<locals>.<genexpr>)all)rN  r2   r2   r3   rA    s   rA  c                    s   |rdd |D   fdd| j D }n| j }t|dkr2tjjjtttj	|dd |D d}ntjj
||r>|d d	 nd d
}t|dkrMt||}|S )Nc                 S   s&   i | ]}| d t|d |d qS rn   rm   )r!  r   r   r2   r2   r3   
<dictcomp>  s    z-_deserialize_column_index.<locals>.<dictcomp>c                    s   g | ]}  ||qS r2   r!  ry   columns_name_dictr2   r3   r{     s    z-_deserialize_column_index.<locals>.<listcomp>r   c                 S   s   g | ]}|d  qS r   r2   )rz   	col_indexr2   r2   r3   r{     r|   r   r   rm   r   )r   ra   r   r   
MultiIndexfrom_tuplesr   r   astliteral_evalIndex"_reconstruct_columns_from_metadata)block_tablerN  r   columns_valuesr   r2   ri  r3   rB    s&   

rB  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 rf  rh  r   r2   r2   r3   rg    s    z&_reconstruct_index.<locals>.<dictcomp>r   r   rm   r   r   r   )r   rm   zUnrecognized index kind: {}r   rl  r   r   )r<   rS   _extract_index_levelr   r   r   ra   r   r?   r   rm  from_arraysrq  r@  )rK  r   rN  rM  field_name_to_metadataindex_arraysindex_namesresult_tabler~   r   
index_namer   r   r2   r2   r3   r>    sP   




	r>  c           
      C   sl   || d }t ||}| j|}|dkr|d d fS | |}|j|d}	d |	_||j|}||	|fS )Nrm   )rM  ) _backwards_compatible_index_namer   get_field_indexre   	to_pandasrm   remove_column)
rK  rz  rn   rw  rM  logical_namer{  r   r   r   r2   r2   r3   ru    s   




ru  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  r2   r2   r3   r}    s   r}  c                 C   s   d}t || d uS )Nz^__index_level_\d+__$)rematch)rm   patternr2   r2   r3   r   /  s   r   c                   C   s2   t st ddddtjdtjtjtjtjd
 t S )NrG   r_   rS   )
r   r7   r6   r   r   rH   r   floatingr8   r	   )_pandas_logical_type_mapr   rJ   rM   r   r   object_r2   r2   r2   r3   get_pandas_logical_type_map4  s   r  c                 C   s@   t  }z||  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`.
    r   )r  r;   rJ   r  rP   )ro   pandas_logical_type_mapr2   r2   r3   _pandas_type_to_numpy_typeG  s   

r  c                 C   s  t j}t| ddp| g}t| ddpdg}dd t||i dD }g }tdd}|D ]\}}	}
t|	}|tjkr>|	|}|	d	krgt
j|d
 d d }|j|dd|}t  rf|t|d
 }n1|	dkrwt jdd |D }n!|jdkr|
dkrd|	v s|	dv r|| q+|j|kr||}|j|
kr|	d	kr||
}|| q+t|dkr|j||| jdS |j|d
 |d
 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`
    r   Nrc   c                 S   s0   g | ]\}}|| d t|j| ddfqS )ro   rp   N)r!  rS   rP   )rz   r   rk  r2   r2   r3   r{   {  s    
z6_reconstruct_columns_from_metadata.<locals>.<listcomp>)	fillvaluer   r   r6   r   rg   r^   T)utcr8   c                 S   s   g | ]}t |qS r2   )r8   Decimalr   r2   r2   r3   r{     r   rS   r9   r   )r   rH   r   rl  )rP   rm   )r   r   r`   r   operatormethodcallerr  rJ   rM   r   r   r   r4  to_datetime
tz_convertrE  as_unitr$  rq  rP   r   astypera   rm  r   rm   )r   r   r   r   labelslevels_dtypes
new_levelsencoderr   r/  numpy_dtyperP   rC   r2   r2   r3   rr  ^  sH   






rr  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 )Nr   c                 S   s   g | ]	}t |tr|qS r2   )r<   rS   )rz   idx_colr2   r2   r3   r{     r   z%_add_any_metadata.<locals>.<listcomp>r   rn   rm   Noner|  ro   r6   rg   r^   r1  r2  r  r   )r   )r   ra   r   r!  r~  r<   rQ   r   r   rB   rC   r  r  Arrayr   r   rm   r   r   Tablerv  )rK  r  modified_columnsmodified_fieldsr   r   n_index_levels	n_columnsr   rc  r  idxr   rg   metadata_tz	convertedtz_aware_typer  r   r  r2   r2   r3   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   r4  dttz_localizer  )seriesrC   r2   r2   r3   make_tz_aware  s
   
r  r   )r   NT)NNT)NFN)?ro  collections.abcr   
concurrentr   concurrent.futures.threadr  r   r8   	itertoolsr   r   r  r  r   numpyrJ   ImportErrorr   r   pyarrow.libr   r   r   r   rI   r  r4   r@   rN   rY   ri   rx   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r0  r%  rQ  rX  r?  rA  rB  r>  ru  r}  r   r  r  rr  r=  r  r2   r2   r2   r3   <module>   s|   2
_'B>	
#
e
E
9S
!7
WA