o
    R
i\                     @   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/home/ubuntu/.local/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	 d Y S t| tjj
r@| jd ur<d Y S d Y S tj| rIY dS Y dS w )Ncategoricalzlist[]
datetimetzdatetimedecimalobject)r4   idKeyError
isinstancer   r   DictionaryTypeListTypeget_logical_type
value_typeTimestampTypetztypes
is_decimal)
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$   rO   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   r7   
datetime64rI   r   )	rO   dtypetyper<   hasattrstr
startswithr   infer_dtype)pandas_collectionnumpy_logical_type_mapresultr2   r2   r3   get_logical_type_from_numpyr   s   
rZ   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rAdtj	
|ji}d|j d}||fS d }t|}||fS )Ncategorycat)num_categoriesorderedrC   timezonezdatetime64[r6   )rQ   rT   getattrlen
categoriesr^   codesrS   r   r   tzinfo_to_stringrC   unit)columnrQ   catsmetadataphysical_dtyper2   r2   r3   get_extension_dtype_info   s    
rj   c                 C   s   t |}t| \}}|dkr|j|jd}d}|dur7t|tr%t|s7t|ts7t	d| dt
|j t|tsDJ 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
    r9   )	precisionscaler:   Nz)Column name must be a string. Got column z	 of type name
field_namepandas_type
numpy_typerh   )r@   rj   rk   rl   r=   floatrK   isnanrT   	TypeErrorrR   __name__)rf   rn   rF   ro   logical_typestring_dtypeextra_metadatar2   r2   r3   get_column_metadata   s4   ry   c              
   C   s  |du rdd |D }dd t ||D }t|}	t|}
|d|
|	  }||
|	 d }g }t | |||D ]\}}}}t||||d}|| q6g }|durg }t ||D ]'\\}}}|jdurnt|jtsn||j t|t|j||d}|| qWt|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  } }}t|dr|jni }zt| W n  ty } zi }tj	d| dt
d
d W Y d}~nd}~ww 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   )rT   .0rn   r2   r2   r3   
<listcomp>       z&construct_metadata.<locals>.<listcomp>c                 S   s"   g | ]\}}t |ts||fqS r2   )r=   dict)r{   level
descriptorr2   r2   r3   r|      s    )rn   rF   ro   Fr   z&The DataFrame has non-str index name `z@` which will be converted to string and not roundtrip correctly.   
stacklevellevelsnamesattrsz(Could not serialize pd.DataFrame.attrs: z!, defaulting to empty attributes.s   pandaspyarrow)libraryversion)index_columnscolumn_indexescolumns
attributescreatorpandas_versionutf8)zipra   ry   appendrn   r=   rT   _column_name_to_stringswarningswarnUserWarningr`   r   _get_simple_index_descriptorrS   r   jsondumps	Exceptionr   __version__r   r   encode)columns_to_convertdfcolumn_namesindex_levelsindex_descriptorspreserve_indexrD   column_field_namesserialized_index_levelsnum_serialized_index_levelsntypesdf_typesindex_typescolumn_metadatacolrn   ro   rF   rh   index_column_metadatanon_str_index_namesr   r   r   r   r   r   er2   r2   r3   construct_metadata   s   



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-8rm   )rj   rZ   r   r   r   )r   rn   rw   rx   rp   r2   r2   r3   r   2  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=   rT   r   decodetuplemapr   r   rt   rr   rK   rs   rn   r2   r2   r3   r   F  s   




r   c                 C   s,   | j dur| j |vrt| j S d|d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
    N__index_level_d__)rn   r   )indexir   r2   r2   r3   _index_level_namem  s   
r   c                 C   sP  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	
|
rJtd|	 d||
 |d  ||	 |t|	 q3g }g }t|D ]0\}}t|||}	t|t	jjr|d u rt|}n|| |d  |	}||	 || qi|| }||||||||fS )NzDuplicate column names found: FSparse pandas data (column ) not supported.)_resolve_columns_of_interestr   	is_unique
ValueErrorlist$_get_columns_to_convert_given_schema_get_index_level_valuesr   r   r   	is_sparsert   r   rT   	enumerater   r=   pd
RangeIndex_get_range_index_descriptor)r   schemar   r   r   r   r   r   convert_fieldsrn   r   r   index_column_namesr   index_leveldescr	all_namesr2   r2   r3   _get_columns_to_convert  sR   








r   c                 C   s<  g }g }g }g }g }g }|j D ]}	z| |	 }
d}W nC ty\   zt| |	}
W n ttfy8   td|	 dw |du rEtd|	 d|du rXt|
tjjrXtd|	 dd}Y nw t	|
rjt
d|	 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, ...).
    Fzname 'zF' present in the specified schema is not found in the columns or indexzd' present in the specified schema corresponds to the index, but 'preserve_index=False' was specifiedNz' 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   r<   _get_index_level
IndexErrorr   r=   r   r   r   r   rt   fieldr   )r   r   r   r   r   r   r   r   r   rn   r   is_indexr   r   r2   r2   r3   r     s^   












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).
    r   )r   r   _is_generated_index_nameintra   get_level_values)r   rn   keyr2   r2   r3   r      s   r   c                 C   s,   zt |  | W S  ty   t|  Y S w N)r   r   rt   rT   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)kindrn   r   r   r   )r   rn   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   r{   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   r{   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   arrayrR   is_extension_array_dtyper=   r   Seriesheadget_datetimetz_typerQ   r   _ndarray_to_arrow_typer   r   )r   r   r   r   r   r   _r   r   r   rD   r   r   type_r	   rh   r2   r2   r3   dataframe_to_types4  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 d| j	 f7  _|d }~ww |sR|j
dkrRtd| d|j
 d|S )	NT)rR   r   safezConversion failed for column z with type r   zField z( was non-nullable but pandas column had z null values)nullablerR   r   r   ArrowInvalidArrowNotImplementedErrorArrowTypeErrorargsrn   rQ   
null_countr   )r   r   field_nullabler   rY   r   )r   r2   r3   convert_columnp  s,   

z+dataframe_to_arrays.<locals>.convert_columnc                 S   s$   t | tjo| jjot| jjtjS r   )	r=   rK   ndarrayflags
contiguous
issubclassrQ   rR   integer)arrr2   r2   r3   _can_definitely_zero_copy  s
   z6dataframe_to_arrays.<locals>._can_definitely_zero_copyc                    s   g | ]	\}} ||qS r2   r2   )r{   r   f)r   r2   r3   r|     s    z'dataframe_to_arrays.<locals>.<listcomp>c                 S   s   g | ]}|j qS r2   rR   )r{   xr2   r2   r3   r|     s    r   r   r   r   r   r   r   )r   ra   r   r   	cpu_countr   r   r   ThreadPoolExecutorr   r   submitr   r=   FuturerY   r   r   r   rh   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_futrD   fieldsrn   r   pandas_metadatarh   n_rowsr   r   r   r   r2   )r   r   r3   dataframe_to_arraysW  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   )rQ   rR   rK   rP   r   is_datetimetzrC   re   r   	timestampfrom_numpy_dtype)r   rQ   r   rC   re   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)rQ   copy)r  klassrQ   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_codesrK   datetime_datarQ   make_datetimetz	is_ge_v21r   r   view
make_blockDatetimeTZBlockra   rS   r   r"  )itemr   extension_columnsreturn_block_int	block_arrr  r  re   r   rQ   r  rn   pandas_dtyper2   r2   r3   _reconstruct_block  sF   
r5  c                 C   s&   t  rd} tj|}t j| |dS )NnsrC   )r   is_v1r   r   string_to_tzinfodatetimetz_type)re   rC   r2   r2   r3   r*    s   r*  Fc                    sp  g }g }i }|j j}|s;|d ur;|d }|dg }|di }|d }	t||}t||	||\}}
t|||| |ntj|j	}
t|g || |t
| t|||}|j tj| ||t }t rd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}n||}||_|S )Nr   r   r   r   r   )create_dataframe_from_blocksc                    s   g | ]
}t | d dqS )F)r1  r5  r{   r/  r   ext_columns_dtypesr2   r3   r|   ;  s    z&table_to_dataframe.<locals>.<listcomp>)r   r   )BlockManager)	DataFramec                    s   g | ]}t | qS r2   r<  r=  r>  r2   r3   r|   H  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.internalsr;  r   r#  r@  pandasrA  r+  	_from_mgraxes)optionstablerb   ignore_metadatatypes_mapperall_columnsr   r   r  r   r   r   rY   r;  blocksr   r@  rA  rN  mgrr2   r>  r3   table_to_dataframe  s\   




rV  >   r
   r   r   r   r   r   r:   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_categoricalNro   rn   rq   r"  )na_value)r   extension_dtyper   rR   rn   r=   r   BaseExtensionTypeto_pandas_dtypeNotImplementedErrorr<   _pandas_supported_numpy_typesr4  r   StringDtyperD   is_dictionaryr   rS   uses_string_dtype	is_stringis_large_stringis_string_viewrK   nan)rP  columns_metadatarR  rO  rb   rW  ext_columnsr   typr4  col_metarn   rQ   r2   r2   r3   rD  b  st   









rD  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 )rn   Nro   r2   r   r2   r2   r3   	<genexpr>  s
    
z:_check_data_column_metadata_consistency.<locals>.<genexpr>)all)rS  r2   r2   r3   rF    s   rF  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 ro   rn   )r&  r   r   r2   r2   r3   
<dictcomp>  s    z-_deserialize_column_index.<locals>.<dictcomp>c                    s   g | ]}  ||qS r2   r&  rz   columns_name_dictr2   r3   r|     s    z-_deserialize_column_index.<locals>.<listcomp>r   c                 S   s   g | ]}|d  qS r   r2   )r{   	col_indexr2   r2   r3   r|     r}   r   r   rn   r   )r   ra   r   r   
MultiIndexfrom_tuplesr   r   astliteral_evalIndex"_reconstruct_columns_from_metadata)block_tablerS  r   columns_valuesr   r2   rn  r3   rG    s&   

rG  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 rk  rm  r   r2   r2   r3   rl    s    z&_reconstruct_index.<locals>.<dictcomp>r   r   rn   r   r   r   )r   rn   zUnrecognized index kind: r   rq  r   r   )r=   rT   _extract_index_levelr   r   r   ra   r   r   rr  from_arraysrv  rE  )rP  r   rS  rR  field_name_to_metadataindex_arraysindex_namesresult_tabler   r   
index_namer   r   r2   r2   r3   rC    sL   



	rC  c           
      C   sl   || d }t ||}| j|}|dkr|d d fS | |}|j|d}	d |	_||j|}||	|fS )Nrn   )rR  ) _backwards_compatible_index_namer   get_field_indexrf   	to_pandasrn   remove_column)
rP  r  ro   r|  rR  logical_namer  r   r   r   r2   r2   r3   rz    s   




rz  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)rn   patternr2   r2   r3   r   A  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 )NrH   zdatetime64[ns]rT   )
r   r8   r7   r   r   rI   r  floatingr9   r	   )_pandas_logical_type_mapr   rK   rN   r   r   object_r2   r2   r2   r3   get_pandas_logical_type_mapF  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<   rK   r  rQ   )rp   pandas_logical_type_mapr2   r2   r3   _pandas_type_to_numpy_typeY  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?|	|}nZ|	d	krht
j|d
 d d }|j|dd|}t  rg|t|
d
 }n1|	dkrxt 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 )rp   rq   N)r&  rT   rQ   )r{   r   rp  r2   r2   r3   r|     s    
z6_reconstruct_columns_from_metadata.<locals>.<listcomp>)	fillvaluer   r   r7   r   rh   r_   T)utcr9   c                 S   s   g | ]}t |qS r2   )r9   Decimalr   r2   r2   r3   r|     r   rT   r:   r   )r   rI   r   rq  )rQ   rn   )r   r   r`   r   operatormethodcallerr  rK   rN   r   r   r   r9  to_datetime
tz_convertrJ  as_unitr)  rv  rQ   r   astypera   rr  r   rn   )r   r   r   r   labelslevels_dtypes
new_levelsencoderr   r4  numpy_dtyperQ   rC   r2   r2   r3   rw  p  sH   





rw  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=   rT   )r{   idx_colr2   r2   r3   r|     s    
z%_add_any_metadata.<locals>.<listcomp>r   ro   rn   Noner  rp   r7   rh   r_   r6  r7  r  r   )r   )r   ra   r   r&  r  r=   rR   r   r   rB   rC   r  r  Arrayr   r   rn   r   r   Tabler{  )rP  r  modified_columnsmodified_fieldsr   r   n_index_levels	n_columnsr   rh  r  idxr   rh   metadata_tz	convertedtz_aware_typer  r   r  r2   r2   r3   rB    s^   


rB  c                 C   s$   t j|}| jdj|} | S )zB
    Make a datetime64 Series timezone-aware for the given tz
    r  )r   r   r9  dttz_localizer  )seriesrC   r2   r2   r3   make_tz_aware  s
   
r  r   )r   NT)NNT)NFN)?rt  collections.abcr   
concurrentr   concurrent.futures.threadr  r   r9   	itertoolsr   r   r  r  r   numpyrK   ImportErrorr   r   pyarrow.libr   r   r   r   rJ   r  r4   r@   rO   rZ   rj   ry   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r5  r*  rV  r]  rD  rF  rG  rC  rz  r  r   r  r  rw  rB  r  r2   r2   r2   r3   <module>   s|   1
m'B>	
#
d
E
@S
!6
WA