
    'e                        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 d dlZd dlmZmZ i ad Zd	 Zej8                  d
ej:                  dej<                  dej>                  dej@                  dejB                  dejD                  dejF                  dejH                  dejJ                  dejL                  dddejN                  dejP                  diZ)d Z*d Z+d Z,d Z-d Z.d Z/d Z0d  Z1d! Z2d" Z3d# Z4d$ Z5d% Z6d& Z7dAd'Z8	 	 dBd(Z9d) Z:dCd*Z;d+ Z<	 dDd,Z=ed-   ed.   z   ed/   z   d0d
gz   D  ch c]  }  e> ej~                  |              c} Z@dAd1ZAd2 ZBd3 ZCdAd4ZD	 dAd5ZEd6 ZFd7 ZGdd8d8ejN                  ejP                  ejN                  ej@                  ejL                  ej                  d9	ZId: ZJd; ZKd< ZLd= ZMd> ZNd? ZOd@ ZPyc c} w )E    N)Sequence)futures)deepcopy)zip_longest)sctypes)_pandas_api	frombytesc            	      |   t         s0t         j                  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     5lib/python3.11/site-packages/pyarrow/pandas_compat.pyget_logical_type_mapr7   +   s       "
FFNNG"
FFf"
 FFf"
 FFw	"

 FFw"
 FFw"
 FFw"
 FF"
 FF"
 FF"
 FF""I"
 FFy"
 FF	"
 FF"
 FF"
  FF!"
" FF#"
$ FFFF))7FF	)"
 	, r5   c                    t               }	 || j                     S # t        $ r t        | t        j
                  j                        rY yt        | t        j
                  j                        r&dj                  t        | j                              cY S t        | t        j
                  j                        r| j                  dcY S dcY S t        | t        j
                  j                        rY yY yw xY w)Ncategoricalzlist[{}]
datetimetzdatetimedecimalobject)r7   idKeyError
isinstancer   r   DictionaryTypeListTypeformatget_logical_type
value_typeTimestampTypetzDecimal128Type)
arrow_typelogical_type_maps     r6   rD   rD   H   s    +-
.. 	j"&&"7"78 
BFFOO4$$%5j6K6K%LMM
BFF$8$89#-==#<<L*L
BFF$9$9:	s(    -C5
AC52C5C5$C54C5r   r   r   r   r   r   r   r   r   r   r   zdatetime64[D]r   stringr   c                     	 t         | j                  j                     S # t        $ rS t	        | j                  d      rY yt        | j                        dk(  rY yt        j                  |       }|dk(  rY y|cY S w xY w)NrG   r:   datetime64[ns]rK   r   )_numpy_logical_type_mapdtypetyper?   hasattrstrr   infer_dtype)pandas_collectionresults     r6   get_logical_type_from_numpyrV   k   s{    &'8'>'>'C'CDD 	$**D1 &&'+;;#(():;X	s!    A; A;A;6A;:A;c                 ~   | j                   }t        |      dk(  rVt        | d|       }|J t        |j                        |j
                  d}t        |j                  j                         }||fS t        |d      r1dt        j                  j                  |j                        i}d}||fS d }t        |      }||fS )Ncategorycat)num_categoriesorderedrG   timezonerM   )rO   rR   getattrlen
categoriesr[   codesrQ   r   r   tzinfo_to_stringrG   )columnrO   catsmetadataphysical_dtypes        r6   get_extension_dtype_inforf   z   s    LLE
5zZvuf-!$//2||
 TZZ--. 8## 
	 7 7 AB) 8## U8##r5   c                 b   t        |      }t        |       \  }}|dk(  r|j                  |j                  d}d}|>t	        |t
              s.t        dj                  |t        |      j                              |)t	        |t
              sJ t        t        |                   ||d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=   z6Column name must be a string. Got column {} of type {}Nonename
field_namepandas_type
numpy_typerd   )
rD   rf   rh   ri   r@   rR   	TypeErrorrC   rP   __name__)rb   rl   rI   rm   logical_typestring_dtypeextra_metadatas          r6   get_column_metadataru      s    " $J/L#;F#C L.y #--%%
  
4 5DKKd4j))
 	
 J!< D  * 2f
#"" r5   c           	      T   t        |D cg c]  }t        |t              s| c}      }t        |      }	|d|	|z
   }
||	|z
  d }g }t        | ||
      D ]&  \  }}}t	        ||||      }|j                  |       ( g }|durMg }t        |||      D ]  \  }}}t        |t              r|j                  5t        |j                  t              s|j                  |j                         t	        |t        |j                        ||      }|j                  |        t        |      dkD  r t        j                  d| dt        d       g }t        |j                  d	|j                  g      }t        |j                  d
|j                  j                  g      }t        ||      D ]"  \  }}t        ||      }|j                  |       $ ng x}x}}dt        j                   ||||z   dt"        j$                  dt&        j(                  d      j+                  d      iS c c}w )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
    N)rl   rI   rm   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)r^   r@   dictzipru   appendrl   rR   _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descrnum_serialized_index_levelsntypesdf_typesindex_typescolumn_metadatacolsanitized_namerI   rd   index_column_metadatanon_str_index_nameslevel
descriptorr   rz   r{   rl   s                            r6   construct_metadatar      sf   $ #&:K 'G.8.E (- 'G #H ZF:f::;H!<<=>KO+./A/;X,G )'^Z&s2<2@B 	x() U" -0{1B.D 	3)E:z*d+ zz%jS.I#**5::6*,UZZ8%%	H "((2!	3$ "#a'MM89L8M N0 0 	+ X

|<

Gbjjoo->?vu- 	,KE43E4@H!!(+	, FHGG1N 	4::.,&)>>$>> *11	
 	 6&> m'Gs   H%c                     t        |       \  }}t        |       }d|v rt        j                  dt        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.rw   rx   r   encodingUTF-8rk   )rf   rV   r   r   r   )r   rl   rs   rt   rn   s        r6   r   r     sm    #;E#B L.-e4K+@A	' i!!$g.""" r5   c                    t        | t              r| S t        | t              r| j                  d      S t        | t              r"t        t	        t        t        |                   S t        | t              rt        d      | y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@   rR   r   decodetuplemapr   r   rp   rl   s    r6   r   r   $  sv    2 $	D%	 {{6""	D%	 54d;<==	D(	#?@@	t9r5   c                     | j                   #| j                   |vrt        | j                         S dj                  |      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
    z__index_level_{:d}__)rl   r   rC   )indexir   s      r6   _index_level_namer   K  s9     zz%**L"@&uzz22%,,Q//r5   c                 H   t        | ||      }| j                  j                  s-t        dj	                  t        | j                                    |t        | ||      S g }|durt        | j                        ng }g }g }|D ]t  }| |   }	t        |      }t        j                  |	      rt        dj	                  |            |j                  |	       |j                  d        |j                  |       v g }
g }t        |      D ]  \  }}t        |||      }t!        |t        j"                  j$                        r|t'        |      }n5|j                  |       |j                  d        |}|j                  |       |
j                  |        ||z   }||||
|||fS )Nz Duplicate column names found: {}F-Sparse pandas data (column {}) not supported.)_resolve_columns_of_interestr   	is_unique
ValueErrorrC   list$_get_columns_to_convert_given_schema_get_index_level_valuesr   r   r   	is_sparserp   r   	enumerater   r@   pd
RangeIndex_get_range_index_descriptor)r   schemar   r   r   r   r   convert_fieldsrl   r   r   index_column_namesr   index_levelr   	all_namess                   r6   _get_columns_to_convertr   ^  s   *2vw?G::.55d2::6FG
 	
 3BOOL .<5-H) 
 N 
"h&t,  %?FFtLN N 	!!#&d#D!
" #L1 
(; a>{KNN$=$=>&/<E%%k2!!$'E%%d+  '
( 11I |%79J,n> >r5   c                 &   g }g }g }g }g }g }|j                   D ]  }		 | |	   }
d}t        |	      }	t        j                  |
      rt        dj	                  |	            |j                  |	      }|j                  |
       |j                  |       |j                  |	       |s|j                  |	       |j                  |	       |j                  |
        ||z   }|||||||fS # t        $ r 	 t        | |	      }
n-# t        t        f$ r t        dj	                  |	            w xY w|du rt        dj	                  |	            |>t        |
t        j                  j                        rt        dj	                  |	            d}Y kw xY w)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 specifieda  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
IndexErrorrC   r   r@   r   r   r   r   r   rp   fieldr   )r   r   r   r   r   r   r   r   r   rl   r   is_indexr   r   s                 r6   r   r     s    LNL *%	T(CH2 't,  %?FFtLN N T"!!#&e$D!%%d+$$T*$U*%X 11I|%79J,n> >U  	<&r40j) <..4fTl< <<
 &   &t. . !(sKNN$=$=> '
 (.vd|5 5 H-	s*   C((	F2C?>F?*D))A#FFc                     |}|| j                   j                  vr"t        |      rt        |t	        d      d       }| j                   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intr^   get_level_values)r   rl   keys      r6   r   r     sO    
 C288>>!&>t&D $s+,R0188$$S))r5   c                 f    	 t        j                  |        | S # t        $ r t        |       cY S w xY wN)r   r   rp   rR   r   s    r6   _level_namer     s1    

4 4ys    00c                     dt        | j                        t        j                  | d      t        j                  | d      t        j                  | d      dS )Nrangestartstopstep)kindrl   r   r   r   )r   rl   r   get_rangeindex_attribute)r   s    r6   r   r     sM     EJJ'55eWE44UFC44UFC r5   c                     t        t        | d| g            }t        |      D cg c]  }| j                  |       c}S c c}w )Nrz   )r^   r]   r   r   )r   nr   s      r6   r   r     s:    GE8eW-.A/4Qx8!E""1%888s   A c                     ||t        d      ||j                  }|S | |D cg c]  }|| j                  v s| }}|S | j                  }|S c c}w )NzJSchema and columns arguments are mutually exclusive, pass only one of them)r   r{   r   )r   r   r   cs       r6   r   r     sv    g1 < = 	=		,, N 
	%9bjj199 N **N	 :s
   AAc           	         t        | d ||      \  }}}}}}}g }	|D ]%  }
|
j                  }t        j                  |      r"t	        j
                  |
d      j                  }nt        j                  |      r\t        |
t        j                  j                        r|
j                  d      n|
d d }t	        j
                  |d      j                  }n]t        ||
j                  d       \  }}t        j                  j                  ||      }|!t	        j
                  |
d      j                  }|	j!                  |       ( t#        || |||||	      }||	|fS )NT)from_pandasr   )r   valuesr   is_categoricalr   arrayrP   is_extension_array_dtyper@   r   Seriesheadget_datetimetz_typerO   r   _ndarray_to_arrow_typer   r   )r   r   r   r   r   _r   r   r   r   r   r   type_r   rd   s                  r6   dataframe_to_typesr     s=    ""dNG
DYE %%f-HHQD166E11&9!+;>>(("*AFF1I/0!u HHU5::E/FMFEFF11&%@E}5::U "Bm>5H
 eX%%r5   c           	      6   t        | |||      \  }}}}	}
}}|Dt        |       t        | j                        }}||dz  kD  r|dkD  rt        j                         }nd}fd}d }|dk(  r&t        ||      D cg c]  \  }} |||       }}}ng }t        j                  |      5 }t        ||      D ]R  \  }} ||j                        r|j                   |||             1|j                  |j                  |||             T 	 d d d        t        |      D ]3  \  }}t        |t        j                        s!|j                         ||<   5 |D cg c]  }|j                   }}|Vg }t        ||      D ]0  \  }}||nd}|j                  t        j                   ||             2 t        j"                  |      }t%        || ||
|	||      }|j&                  rt)        |j&                        n	t+               }|j-                  |       |j/                  |      }d }t        |      dk(  r<	 |	d   d   }|dk(  r.|	d   d	   } |	d   d
   }!|	d   d   }"t        t1        | |!|"            }|||fS c c}}w # 1 sw Y   qxY wc c}w # t2        $ r Y (w xY w)Nd      c                    |d}d }n|j                   }|j                  }	 t        j                  | |d      }|s=|j                  dkD  r.t        dj                  t        |      |j                              |S # t        j                  t        j
                  t        j                  f$ rA}|xj                  dj                  | j                  | j                        fz  c_        |d }~ww xY w)NT)rP   r   safez0Conversion failed for column {!s} with type {!s}r   z>Field {} was non-nullable but pandas column had {} null values)nullablerP   r   r   ArrowInvalidArrowNotImplementedErrorArrowTypeErrorargsrC   rl   rO   
null_countr   rR   )r   r   field_nullabler   rU   er   s         r6   convert_columnz+dataframe_to_arrays.<locals>.convert_columnI  s    =!NE"^^NJJE	XXc4dKF &"3"3a"7 228&U9?9J9J3LM M  ++!!# 	 FFIsxx36 6FG	s   A< <2C/.<C**C/c                     t        | t        j                        xrF | j                  j                  xr. t        | j                  j                  t        j                        S r   )	r@   npndarrayflags
contiguous
issubclassrO   rP   integer)arrs    r6   _can_definitely_zero_copyz6dataframe_to_arrays.<locals>._can_definitely_zero_copy_  sB    3

+ 7		$$7399>>2::6	8r5   rj   r   r   r   r   r   r   )r   r^   r   r   	cpu_countr   r   ThreadPoolExecutorr   r   submitr   r@   FuturerU   rP   r   r   r   rd   r   r   r   with_metadatar   r   )#r   r   r   nthreadsr   r   r   r   r   r   r   r   r   nrowsncolsr   r  r   farraysexecutorr   	maybe_futxr   fieldsrl   r   pandas_metadatard   n_rowsr   r   r   r   s#        `                             r6   dataframe_to_arraysr  4  s    /r6>/68Y 2wBJJu53;519||~HH,8
 1}!"4nEGa !A& G G ''1 	IX.? I1,QXX6MM.A"67MM(//.!Q"GH	I	I &f- 	/LAy)W^^4%,,.q		/ $$QVV$E$~y%0 	1KD%+4DMM"((4/0	1 6"(Bm>5O -3OOx(HOOO$!!(+F F
6{a	$Q'/Dw)!,W5(+F3(+F3U5$56 66!!]G	I 	I %8  		s+   :I4(A"I:J4;J :J	JJc                 4   | j                   j                  t        j                  k7  r| |fS t	        j
                  |      r4|2|j                  }|j                  }t        j                  ||      }| |fS |t        j                  | j                         }| |fS r   )rO   rP   r   
datetime64r   is_datetimetzrG   unitr   	timestampfrom_numpy_dtype)r   rO   r   rG   r  s        r6   r   r     s    ||BMM)u}  'EMXXzzT2&
 5=	 
##FLL15=r5   c                    ddl mc m} | j                  dd      }| d   }d| v r=t        j
                  j                  || d   | d         }|j                  ||      }|S d	| v rRt        j                  |j                        \  }}	t        || d	         }
|j                  |||j                  |

      }|S d| v r(|j                  t        j                  |      |      }|S d| v r_| d   }t        |      dk(  sJ ||d      }||   }t!        |d      st#        d      |j%                  |      }|j                  ||      }|S |j                  ||      }|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[   )r_   r[   )r  r\   )r  klassrO   r=   py_arrayr   __from_arrow__zGThis column does not support to be converted to a pandas ExtensionArray)pandas.core.internalscore	internalsgetr   categorical_type
from_codes
make_blockr   datetime_datarO   make_datetimetzDatetimeTZBlockpickleloadsr^   rQ   r   r   )itemr   extension_columns_int	block_arrr  rY   r  r  r   rO   r  rl   pandas_dtype
pd_ext_arrs                  r6   _reconstruct_blockr3    s   8 )($'I[!It**55$|"4O 6 % y90 L/ 
t	""9??3ad:&67	Y&*&:&:&+   -( L# 
T	Y 7*3   5  L 
t	:9~""y|$(.|%56 : ; ;!005

i@ L 	Y?Lr5   c                     t        j                         rd} t        j                  j	                  |      }t        j
                  | |      S )NnsrG   )r   is_v1r   r   string_to_tzinfodatetimetz_type)r  rG   s     r6   r)  r)    s:    		 	 	$B&&t33r5   c                 .   ddl m} ddlm} g }g }|j                  j
                  }	|sI|	G|	d   }|	j                  dg       }|	d   }
t        ||	      }t        ||
||      \  }}t        |||      }n6t        j                  j                  |j                        }t        |g |      }t        |       t        |||      }t!        | |||      }||g} |||      }t        j"                         r|j%                  ||j&                        }|S  ||      }|S )Nr   )BlockManager)	DataFramer   r   r   )r!  r;  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_blocks	is_ge_v21	_from_mgraxes)optionstabler_   ignore_metadatatypes_mapperr;  r<  all_columnsr   r  r   r   ext_columns_dtypesr   blocksrG  mgrr   s                     r6   table_to_dataframerP    s)    3 KNll22O:%i0(,,-=rB+O<!%9)%1B*5|Eu2;. ))%..925"lK+K8'{NKGguj:LMFUD
vt
$C  chh/ I s^Ir5   r   uintfloatr=   c                 P   i }t         j                  |S |D ]X  }	 |d   }|d   }|t        vst        j                  |      }t        |t         j                        sGt        |d      sT|||<   Z | j                  D ]I  }|j                  }	t        |	t        j                        s*	 |	j                         }|||j                  <   K |r7| j                  D ](  }|j                  }	 ||	      }||||j                  <   * |S # t        $ r |d   }Y w xY w# t        $ r Y w xY w)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.

    rm   rl   ro   r   )r   extension_dtyper?   _pandas_supported_numpy_typesr1  r@   rQ   r   rP   r   BaseExtensionTypeto_pandas_dtyperl   NotImplementedError)
rI  columns_metadatarK  ext_columnscol_metarl   rO   r1  r   typs
             r6   r@  r@    sO    K ""* % 5	$L)D &55 '33E:L,(C(CD<)9:(4K%5   7jjc2//07"224 +7EJJ'7 \\ 	7E**C',L'*6EJJ'		7 ?  	$F#D	$$ ' s#   D)DDD	D%$D%c                 ,    t        d | D              sJ y )Nc              3   H   K   | ]  }|d    du xr d|v xs |d    du  yw)rl   Nrm   r4   ).0r   s     r6   	<genexpr>z:_check_data_column_metadata_consistency.<locals>.<genexpr>Z  s<       
6d		0|q0JQvYd5JJs    ")all)rL  s    r6   rB  rB  U  s#    
     r5   c           
         | j                   D cg c]  }t        |t              rt        |      n|! }}|rO|D ci c]$  }|j	                  dt        |d               |d   & }}|D cg c]  }|j	                  ||       }}n|}t        |      dkD  rt        j                  nd }	|s t        j                  j                  |      }
nSt        j                  j                  j                  t        t        |	|            |D cg c]  }|d   	 c}xs d       }
t        |      dkD  rt!        |
|      }
t#        |
      }
|
S c c}w c c}w c c}w c c}w )Nrm   rl   r   c                     | fS r   r4   )r  s    r6   <lambda>z+_deserialize_column_index.<locals>.<lambda>q  s    ! r5   r{   r   )r   r@   r   r	   r$  r   r^   astliteral_evalr   r   Index
MultiIndexfrom_tuplesr   r   "_reconstruct_columns_from_metadata _flatten_single_level_multiindex)block_tablerL  r   r  column_stringsr   columns_name_dictrl   columns_valuesto_pairr   	col_indexs               r6   rC  rC  `  sf   *779 '1E&:ilA 9N 9 !
 EE, 7&	 BCQvYN
 

 ;I
26!!$-
 
 (
 #&n"5"9c~G
 ..&&~6..++77Wn-.6DE9V$EM 8 
 >Q4WnM /w7GNI9

& Fs   $E)E
*EE
c                    |D ci c]  }|j                  d|d         | }}g }g }| }|D ]  }	t        |	t              rt        | ||	||      \  }}
}|
o)|	d   dk(  rI|	d   }t        j
                  j                  |	d   |	d   |	d   |      }
t        |
      t        |       k7  rzt        d	j                  |	d               |j                  |
       |j                  |        t        j
                  }t        |      d
kD  r!|j                  j                  ||      }||fS t        |      d
k(  r5|d   }t        ||j                        s|j                  ||d         }||fS |j                  | j                        }||fS c c}w )Nrm   rl   r   r   r   r   r   )r   rl   zUnrecognized index kind: {}r   re  r   r   )r$  r@   rR   _extract_index_levelr   r   r   r^   r   rC   r   ri  from_arraysrh  rA  )rI  r   rL  rK  r   field_name_to_metadataindex_arraysindex_namesresult_tabler   r   
index_namer   r   s                 r6   r?  r?    s     	
lAfI&)  LKL" 'eS!4H|U,BL5R1L+z"6]g%vJ%..33E'N49&M9>v9C 4 EK ;3u:-:$fU6]35 5K(:&)', 
B <1)),k)J  
\	a	Q%*HHUQH8E  enn-[s   Fc                    ||   d   }t        ||      }| j                  j                  |      }|dk(  r|d d fS t        j                  }| j                  |      }	|	j                  |      j                  }
t        |
d      r&|
j                  j                  s|
j                         }
t        |	j                  t        j                  j                         rH|	j                  j"                  2t%        |j'                  |
d      |	j                  j"                        }n|j'                  |
|
j(                  d      }|j+                  |j                  j                  |            }|||fS )Nrl   )rK  r   F)copy)rO   r}  ) _backwards_compatible_index_namer   get_field_indexr   r   rb   	to_pandasr   rQ   r   	writeabler}  r@   rP   r   r   rF   rG   make_tz_awarer   rO   remove_column)rI  ry  rm   rv  rK  logical_namerz  r   r   r   r   r   s               r6   rt  rt    s%   )*5f=L1*lKJ$$Z0ABwT4''	B
,,q/C]]]5<<Fvw(>(> #((BFF001chhkk6M#BIIf5I$A388;;OiiflliG--++J7L j00r5   c                 (    | |k(  rt        |       ry|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  s     r6   r~  r~    s    $ <$<X$Fr5   c                 6    d}t        j                  ||       d uS )Nz^__index_level_\d+__$)rematch)rl   patterns     r6   r   r     s    &G88GT"$..r5   rM   )	r   r;   r:   r   r   rK   r   floatingr   c                     	 t         |    S # t        $ r. d| v rt        j                  cY S t        j                  |       cY S w xY 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   )_pandas_logical_type_mapr?   r   object_rO   )rn   s    r6   _pandas_type_to_numpy_typer    sB    %'44 %k!::xx$$	%s    AAAc                 d    t        | t        j                  j                        r| j                  S y r   )r@   r   r   ri  r`   )mis    r6   _get_multiindex_codesr    s"    "knn//0xxr5   c                    t         j                  }t        | dd      xs | g}t        |       xs( |D cg c]  }|j	                  t        |             c}}t        ||i       D cg c]=  \  }}||j                  dt        |j                              |j                  dd      f? }}}g }t        j                  dd      }	|D ]  \  }}
}t        |
      }|t        j                  k(  r|j                  |	      }|
dk(  rKt         j"                  j%                  |d	   d
   d         }|j'                  |d      j)                  |      }n |j                  |k7  r|j+                  |      }|j                  |k7  r|
dk7  r|j+                  |      }|j-                  |        |j/                  ||| j0                        S c c}w c c}}w )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`
    rz   N)	fillvaluern   ro   r   r   r:   r   rd   r\   T)utcre  )r   r   r]   r  r   r^   r   r$  rR   rO   operatormethodcallerr  r   bytes_r   r   r   r8  to_datetime
tz_convertastyper   ri  r{   )r   r   r   rz   r   labelsrr  levels_dtypes
new_levelsencoderr1  numpy_dtyperO   rG   s                 r6   rk  rk  #  s   , 
B Wh-:'F"7+ /50&+c%j!0F !,Nb!
 E9 
	mS-=>	|T	*	,M  J##Hg6G,9 !(|[*<8 BIIIIg&E<'((q!*-j9;BNN5dN3>>rBE[[E!LL'E;;+%,,*FLL-E% %!( ==V7===AAI0
s   !G)AG	c           	          |j                   }t        j                  j                  | ||t	        |j                                     }|D cg c]  }t        |||       c}S c c}w r   )r   r   r   table_to_blocksr   keysr3  )rH  rm  r_   r.  r   rU   r-  s          r6   rD  rD  d  sh     &&GVV##G[*$():)?)?)A$BDF   tW.?@      s   
A!c                 h   t         j                  }t        | |j                        r| j                  dk(  rw| j
                  \  }t        |       \  }|j                  }| j                  st        d      |j                  |D cg c]  }|dk7  r||   nd  c}|| j                  d         S | S c c}w )Nr   zFound non-unique column indexr|  r   )rO   rl   )r   r   r@   ri  nlevelsrz   r  rO   r   r   rh  r{   )r   r   rz   r  rO   _labels         r6   rl  rl  o  s    	B%'EMMQ,>,,'. <==xxDJK&v|VF^5KQ  
 	

 L	 Ls   B/c                    i }i }| j                   }|d   }|D cg c]  }t        |t              r| }}t        |      }t        |d         |z
  }t	        |d         D ],  \  }	}
|
j                  d      }|s|
d   }|	|k\  r||	|z
     }|d}|j                  |      }|dk7  sG|
d   dk(  sP| |   }t        |j                  t        j                  j                        s|
d	   }|s|j                  d
      }|s||j                  j                  k7  s|j                         }t        j                  d|      }t        j                  j                  ||      }t        j                   ||   j"                  |      ||<   |||<   / t        |      dkD  rg }g }t%        t        | j                               D ]a  }	|	|v r)|j'                  ||	          |j'                  ||	          0|j'                  | |	          |j'                  | j                   |	          c t        j(                  j+                  |t        j                   |            S | S c c}w )Nr   r   rm   rl   rj   r|  rn   r:   rd   r\   r5  r6  )rP   r   )r   )r   r@   rR   r^   r   r$  r  rP   r   r   rF   rG   r  r  Arrayr   r   rl   r   r   Tableru  )rI  r  modified_columnsmodified_fieldsr   r   idx_coln_index_levels	n_columnsr   r[  r  idxr   rd   metadata_tz	convertedtz_aware_typer  r   r  s                        r6   r>  r>    sX   O\\F#O4M,9 2"7C0  2M 2'NOI./.@I !!;< :8<<-'HI~(Y7!$$X."9&,6Cj!#((BFF,@,@A#J/&ll:6;#((++#= #I$&LL+$FM$&HH$8$8>K %9 %MM ,.88F3K4D4D4A,COC(,9$S)=:@ q s5<<() 	/A$$/23oa01uQx(ell1o.	/ xx##GBIIf4E#FFe2s   Ic                     t         j                  j                  |      }| j                  j	                  d      j                  j                  |      } | S )zB
    Make a datetime64 Series timezone-aware for the given tz
    r  )r   r   r8  dttz_localizer  )seriesrG   s     r6   r  r    sA     
	 	 	$Bii##E*R

2 Mr5   r   )r   NT)NN)NFN)Qrf  collections.abcr   
concurrentr   concurrent.futures.threadr}  r   	itertoolsr   r   r  r+  r  r   numpyr   numpy.core.numerictypesr   _np_sctypesr|   r   pyarrow.libr   r	   r   r7   rD   bool_r   r   r   r   r   r   r   r   r   r   str_r  rN   rV   rf   ru   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r3  r)  rP  rR   rO   rU  r@  rB  rC  r?  rt  r~  r   r  r  r  r  rk  rD  rl  r>  r  )r\  s   0r6   <module>r     s1  &  $  !  !    	   :  .  :$ HHfGGVHHgHHgHHgHHgIIxIIxIIxJJ	JJ	VGGXIIw $$&*ZSl($N0&=>@=>@
*9
&D IM!_"D&<~4 JN!P E"[%88;w;OO6"#! ! 6r%P3n ?C1<0/  "wwYYggxx

ZZ
 %,>BB (:B_!s   G'