
    ch                        S SK r S SKJr  S SKJr  S SKrS SKJr  S SKrS SK	J
r
  S SKrS SKrS SKrS SKr S SKrS SKrS SKJrJrJr  0 q0 q0 qS rS rS	 rS
 rS rS r  S+S jr!S r"S r#S r$S r%S r&S r'S r(S r)S r*S r+S+S jr,  S,S jr-S r.S-S jr/S r0 S.S jr11 Skr2S r3S  r4S! r5S+S" jr6 S+S# jr7S$ r8S% r9S& r:S' r;S( r<S) r=S* r>g! \ a    Sr Nf = f)/    N)Sequence)futures)deepcopy)zip_longest)_pandas_api	frombytesis_threading_enabledc            	         [         (       Gd0  [         R                  0 [        R                  R                  S_[        R                  R
                  S_[        R                  R                  S_[        R                  R                  S_[        R                  R                  S_[        R                  R                  S_[        R                  R                  S_[        R                  R                  S_[        R                  R                  S	_[        R                  R                  S
_[        R                  R                  S_[        R                  R                  S_[        R                  R                   S_[        R                  R"                  S_[        R                  R$                  S_[        R                  R&                  S_[        R                  R(                  S_[        R                  R*                  S[        R                  R,                  S[        R                  R.                  S0E5        [         $ )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     hC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\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                    [        5       n XR                     $ ! [         a    [        U [        R
                  R                  5      (       a   g[        U [        R
                  R                  5      (       a  S[        U R                  5       S3s $ [        U [        R
                  R                  5      (       a  U R                  b  Ss $ Ss $ [        R                  R                  U 5      (       a   g gf = f)Ncategoricalzlist[]
datetimetzdatetimedecimalobject)r7   idKeyError
isinstancer   r   DictionaryTypeListTypeget_logical_type
value_typeTimestampTypetztypes
is_decimal)
arrow_typelogical_type_maps     r6   rD   rD   K   s    +-.. 	j"&&"7"788 
BFFOO44+J,A,ABC1EE
BFF$8$899#-==#<<L*LXX  ,,	s(    3C:AC:8C:C:$C:9C:c                     [         (       d  [         R                  [        R                  S[        R                  S[        R
                  S[        R                  S[        R                  S[        R                  S[        R                  S[        R                  S[        R                  S	[        R                  S
[        R                  SSS[        R                  S[        R                  S05        [         $ )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_r4   r5   r6   get_numpy_logical_type_maprT   \   s    ""&&HHfGGVHHgHHgHHgHHgIIxIIxIIxJJ	JJ	VGGXIIw(
 	  #"r5   c                 b   [        5       n XR                  R                     $ ! [         a    [	        U R                  S5      (       a   g[        U R                  5      R                  S5      (       a  [        U R                  5      s $ [        R                  " U 5      nUS:X  a   gUs $ f = f)NrG   r;   
datetime64rN   r   )	rT   dtypetyper@   hasattrstr
startswithr   infer_dtype)pandas_collectionnumpy_logical_type_mapresults      r6   get_logical_type_from_numpyr`   r   s    79%&=&=&B&BCC 
$**D11  &&'22<@@(..//(():;X
s!   $ %B.>B.B.)B.-B.c                    U R                   n[        U5      S:X  aV  [        U SU 5      nUc   e[        UR                  5      UR
                  S.n[        UR                  R                   5      nXC4$ [        US5      (       a0  S[        R                  R                  UR                  5      0nSnXC4$ S n[        U5      nXC4$ )Ncategorycat)num_categoriesorderedrG   timezonedatetime64[ns])rW   rZ   getattrlen
categoriesre   codesrY   r   r   tzinfo_to_stringrG   )columnrW   catsmetadataphysical_dtypes        r6   get_extension_dtype_inforq      s    LLE
5zZvuf-!$//2||
 TZZ--. ## 
		 7 7 AB) ## U##r5   c                    [        U5      n[        U 5      u  pVUS:X  a  UR                  UR                  S.nSnUbi  [	        U[
        5      (       a  [        R                  " U5      (       d9  [	        U[        5      (       d$  [        SU S[        U5      R                   35      e[	        U[        5      (       d   [        [        U5      5      5       eUUUUU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>   z)Column name must be a string. Got column z	 of type name
field_namepandas_type
numpy_typero   )rD   rq   rs   rt   rA   floatrP   isnanrZ   	TypeErrorrX   __name__)rm   rv   rJ   rw   logical_typestring_dtypeextra_metadatas          r6   get_column_metadatar      s    " $J/L#;F#C Ly #--%%
   	D%((RXXd^^4%%7vYDz""#%
 	

 j#&&=D,<(==& #"" r5   c           	         Uc  U Vs/ s H  n[        U5      PM     nn[        U V	s/ s H  n	[        U	[        5      (       a  M  U	PM     sn	5      n
[        U5      nUSX-
   nXkU
-
  S n/ n[	        XX|5       H#  u  pnn[        XUUS9nUR                  U5        M%     / nUSLGaX  / n[	        X=U5       H  u  nnn[        U[        5      (       a  M  UR                  b:  [        UR                  [         5      (       d  UR                  UR                  5        [        U[        UR                  5      UUS9nUR                  U5        M     [        U5      S:  a  [        R                  " SU S3[        SS9  / n[        UR                  S	UR                  /5      n[        UR                  S
UR                  R                  /5      n[	        UU5       H#  u  nn[        UU5      nUR                  U5        M%     O/ =n=nnS[        R                   " UUUU-   S["        R$                  S.[&        R(                  S.5      R+                  S5      0$ s  snf s  sn	f )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
N)rv   rJ   rw   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)rZ   ri   rA   dictzipr   appendrv   _column_name_to_stringswarningswarnUserWarningrh   r   _get_simple_index_descriptorjsondumpsr   __version__r   r   encode)columns_to_convertdfcolumn_namesindex_levelsindex_descriptorspreserve_indexrH   column_field_namesrv   descrnum_serialized_index_levelsntypesdf_typesindex_typescolumn_metadatacolrw   rJ   ro   index_column_metadatanon_str_index_nameslevel
descriptorr   r   r   s                             r6   construct_metadatar      s   * ! 5AALDc$iLA"%:K 'G:K.8.E (-:K 'G #H ZF:f:;H!<<=>KO-01C1C.O):z&s2<2<> 	x(.O U" -01B.D)E:z*d++ zz%jS.I.I#**5::6*,UZZ8%%	H "((2!.D$ "#a'MM89L8M N0 0 	+ X

|<

Gbjjoo->?vu-KE43E4@H!!(+ . FHGG1N 	4::.,&)>>$>> *11	
 	 6&> q B'Gs   IIIc                     [        U 5      u  p#[        U 5      nSU;   a  [        R                  " S[        SS9  US:X  a  U(       a   eSS0nUUUUU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-8ru   )rq   r`   r   r   r   )r   rv   r   r   rx   s        r6   r   r   $  sn    #;E#B L-e4K+@A	' i!!!$g.""" r5   c                    [        U [        5      (       a  U $ [        U [        5      (       a  U R                  S5      $ [        U [        5      (       a"  [        [	        [        [        U 5      5      5      $ [        U [        5      (       a  [        S5      eU b0  [        U [        5      (       a  [        R                  " U 5      (       a  U $ [        U 5      $ )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 level)rA   rZ   r   decodetuplemapr   r   r|   rz   rP   r{   rv   s    r6   r   r   8  s    2 $	D%	 	 {{6""	D%	 	 54d;<==	D(	#	#?@@	*T511bhhtnnt9r5   c                 t    U R                   b%  U R                   U;  a  [        U R                   5      $ SUS S3$ )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
__index_level_d__)rv   r   )indexir   s      r6   _index_level_namer   _  s9     zz%**L"@&uzz22!uB''r5   c                 p   [        XU5      nU R                  R                  (       d!  [        S[	        U R                  5       35      eUb  [        XU5      $ / n/ nUSLa  [        U R                  5      O/ n/ n/ nU H  n	X	   n
[        U	5      n	[        R                  " U
5      (       a  [        SU	 S35      eUR                  U
5        UR                  S 5        UR                  U	5        UR                  [        U	5      5        M     / n/ n[        U5       H  u  p[        XU5      n	[!        U[        R"                  R$                  5      (       a  Uc  ['        U5      nO5UR                  U5        UR                  S 5        U	nUR                  U	5        UR                  U5        M     X\-   nUXEUXXx4$ )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_sparser|   r   rZ   	enumerater   rA   pd
RangeIndex_get_range_index_descriptor)r   schemar   r   r   r   r   r   convert_fieldsrv   r   r   index_column_namesr   index_levelr   	all_namess                    r6   _get_columns_to_convertr   r  s   *2w?G::,T"**-=,>?
 	
 3BOOL .<5-H) 
 Nh&t,  %%-dV3CDF F 	!!#&d#D!!!#d),  #L1 >{KNN$=$=>>&/<E%%k2!!$'E%%d+  ' 2 #7I |9K-?Q Qr5   c                    / n/ n/ n/ n/ n/ nUR                    H  n	 X	   n
Sn[        R                  " U
5      (       a  [        SU	 S35      eUR                  U	5      nUR                  U
5        UR                  U5        UR                  U	5        U(       d  M  UR                  U	5        UR                  U	5        UR                  U
5        M     X7-   nXX7XhXE4$ ! [         a     [        X	5      n
O#! [        [        4 a    [        SU	 S35      ef = fUSL a  [	        SU	 S35      eUc8  [        U
[        R                  R                  5      (       a  [	        SU	 S35      eSn GNFf = f)	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 specifiedz' 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   rA   r   r   r   r   r|   fieldr   )r   r   r   r   r   r   r   r   r   rv   r   is_indexr   r   s                 r6   r   r     s    LNL	(CH2   %%-dV3CDF F T"!!#&e$D!8%%d+$$T*$Q T 1I\-?Q QQ  	/&r0j) /TF #. ./ //
 & TF #   ! ! !(sKNN$=$=>> TF #' '( ( H-	s*   C
E*&C21E*2 DAE*)E*c                     UnXR                   R                  ;  a'  [        U5      (       a  [        U[	        S5      S 5      nU R                   R                  U5      $ )zS
Get the index level of a DataFrame given 'name' (column name in an arrow
Schema).
r   )r   r   _is_generated_index_nameintri   get_level_values)r   rv   keys      r6   r   r     sP    
 C88>>!&>t&D&D $s+,R0188$$S))r5   c                 j     [         R                  " U 5        U $ ! [         a    [        U 5      s $ f = fN)r   r   r|   rZ   r   s    r6   _level_namer     s1    

4 4ys    22c                     S[        U R                  5      [        R                  " U S5      [        R                  " U S5      [        R                  " U S5      S.$ )Nrangestartstopstep)kindrv   r   r   r   )r   rv   r   get_rangeindex_attribute)r   s    r6   r   r     sM     EJJ'55eWE44UFC44UFC r5   c                     [        [        U SU /5      5      n[        U5       Vs/ s H  o R                  U5      PM     sn$ s  snf )Nr   )ri   rh   r   r   )r   nr   s      r6   r   r     s<    GE8eW-.A/4Qx8x!""1%x888s   Ac                     Ub  Ub  [        S5      eUb  UR                  nU$ Ub%  U Vs/ s H  o3U R                  ;   d  M  UPM     nnU$ U R                  nU$ s  snf )NzJSchema and columns arguments are mutually exclusive, pass only one of them)r   r   r   )r   r   r   cs       r6   r   r     su    g1 < = 	=		,, N 
	%9gbjj1g9 N **N	 :s   AAc                    [        U S X5      u  nnnnnnn	n/ n
U	 GH/  nUR                  n[        R                  " U5      (       a   [        R
                  " USS9R                  nO[        R                  " U5      (       a_  [        U[        R                  R                  5      (       a  UR                  S5      OUS S n[        R
                  " USS9R                  nOY[        XR                  S 5      u  p[        R                  R                  X5      nUc  [        R
                  " USS9R                  nU
R!                  U5        GM2     [#        XXHUXUS9nX:U4$ )NT)from_pandasr   r   )r   valuesr   is_categoricalr   arrayrX   is_extension_array_dtyperA   r   Seriesheadget_datetimetz_typerW   r   _ndarray_to_arrow_typer   r   )r   r   r   r   r   r   _r   r   r   rH   r   r   type_r   ro   s                   r6   dataframe_to_typesr   &  s:    ""dN
DYE%%f--HHQD166E11&99!+;>>(("* "*AFF1I/0!u HHU5::E/FMFFF11&@E}5::U   "=N2DH
 X%%r5   c                   ^ [        XUU5      u  nnnn	n
nnnUcE  [        U 5      [        U R                  5      pXS-  :  a  US:  a  [        R                  " 5       nOSn[        5       (       d  SnU4S jnS nUS:X  a(  [        X5       VVs/ s H  u  nnU" UU5      PM     nnnO/ n[        R                  " U5       n[        X5       HY  u  nnU" UR                  5      (       a  UR                  U" UU5      5        M7  UR                  UR                  UUU5      5        M[     S S S 5        [        U5       H:  u  nn[        U[        R                  5      (       d  M'  UR                  5       UU'   M<     U Vs/ s H  nUR                   PM     nnUcT  / n[        UU5       H,  u  nnUR                  [        R"                  " UU5      5        M.     [        R$                  " U5      n['        XX{U
UUUS9nUR(                  (       a  [+        UR(                  5      O	[-        5       nUR/                  U5        UR1                  U5      nS n[        U5      S:X  a=   U
S   S   n U S:X  a.  U
S   S	   n!U
S   S
   n"U
S   S   n#[        [3        U!U"U#5      5      nUUU4$ s  snnf ! , (       d  f       GN= fs  snf ! [4         a     N.f = f)Nd      c                   > Uc  SnS nOUR                   nUR                  n [        R                  " XSTS9nU(       d,  UR                  S:  a  [        SU SUR                   S35      eU$ ! [        R                  [        R
                  [        R                  4 a7  nU=R                  SU R                   SU R                   34-  sl        UeS nAff = f)	NT)rX   r   safezConversion failed for column z with type r   zField z( was non-nullable but pandas column had z null values)nullablerX   r   r   ArrowInvalidArrowNotImplementedErrorArrowTypeErrorargsrv   rW   
null_countr   )r   r   field_nullabler   r_   er   s         r6   convert_column+dataframe_to_arrays.<locals>.convert_columnb  s    =!NE"^^NJJE	XXc4dKF &"3"3a"7veW -$$*$5$5#6lD E E ++!!# 	 FF/zSYYKPS SFG	s   A. .3C!2CCc                     [        U [        R                  5      =(       aK    U R                  R                  =(       a.    [        U R                  R                  [        R                  5      $ r   )	rA   rP   ndarrayflags
contiguous
issubclassrW   rX   integer)arrs    r6   _can_definitely_zero_copy6dataframe_to_arrays.<locals>._can_definitely_zero_copyw  sB    3

+ 7		$$7399>>2::6	8r5   r   r   r   r   r   r   r   )r   ri   r   r   	cpu_countr	   r   r   ThreadPoolExecutorr   r   submitr   rA   Futurer_   rX   r   r   r   ro   r   r   r   with_metadatar   r   )$r   r   r   nthreadsr   r   r   r   r   r   r   r   r   r   nrowsncolsr  r  r   farraysexecutorr   	maybe_futxrH   fieldsrv   r   pandas_metadataro   n_rowsr   r   r   r   s$        `                              r6   dataframe_to_arraysr#  I  s    /r>/68Y 2wBJJu3;519||~HH!!*8
 1}!"4EGEda !A&E 	 G ''1X.?1,QXX66MM.A"67MM(//.!Q"GH	 @ 2 &f-LAy)W^^44%,,.q	 . $$VQVVVE$~y%0KD%MM"((4/0 16"(=N2DO -3OOx(HOOO$!!(+F F
6{a	$Q'/Dw)!,W5(+F3(+F3U5$56 66!![G 21 %6  		s+   J  A)J&>J8<J= &
J5=
K
	K
c                 B   U R                   R                  [        R                  :w  a  X4$ [        R
                  " U5      (       a4  Uc1  UR                  nUR                  n[        R                  " XC5      nX4$ Uc   [        R                  " U R                   5      nX4$ r   )rW   rX   rP   rV   r   is_datetimetzrG   unitr   	timestampfrom_numpy_dtype)r   rW   r   rG   r&  s        r6   r   r     s    ||BMM)}  ''EMXXzzT&
 =	 
##FLL1=r5   c                    SSK Js  Jn  U R                  SS5      nU S   nSU ;   a%  [        R
                  R                  XPS   U S   S9nOSU ;   a  [        R                  " UR                  5      u  p[        XS   5      n
[        R                  " 5       (       a/  [        R                  R                  UR                  S	5      U
S
S9nO~UnU(       a  UR                  XVUR                   U
S9nU$ OWSU ;   aO  U S   n[#        U5      S:X  d   eXS      nX,   n[%        US5      (       d  ['        S5      eUR)                  U5      nOUnU(       a  UR                  XvS9$ Xv4$ )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
dictionaryre   )rj   re   rf   r   F)rW   copy)r+  klassrW   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_codesrP   datetime_datarW   make_datetimetz	is_ge_v21r   r   view
make_blockDatetimeTZBlockri   rY   r   r0  )itemr   extension_columnsreturn_block_int	block_arrr+  r  r&  r   rW   r*  rv   pandas_dtypes                 r6   _reconstruct_blockrC    s   8 )($'I[!It**55|"4O 6 % 
t	""9??3:&67  ""..&&w'u5 ' C C	.2.B.B.3 ( 5 	 
 
t	:9~"""|$(.|%566 : ; ;))#.s88~r5   c                     [         R                  " 5       (       a  Sn [        R                  R	                  U5      n[         R
                  " XS9$ )NnsrG   )r   is_v1r   r   string_to_tzinfodatetimetz_type)r&  rG   s     r6   r8  r8    s;    		 	 	$B&&t33r5   c           
      |   / n/ nUR                   R                  nU(       dF  UbC  US   nUR                  S/ 5      nUS   n[        X5      n[	        XXT5      u  p[        XX@U5      n
O7[        R                  R                  UR                  5      n	[        U/ X@U5      n
[        U5        [        XU5      nUR                  n[        R                  R                  XU[!        U
R#                  5       5      5      n[        R$                  " 5       (       a*  SSKJn  U Vs/ s H  n[+        XU
SS9PM     nnU" UXS9nU$ SS	KJn  SS
KJn  U Vs/ s H  n[+        XU
5      PM     nnX/nU" UU5      n[        R4                  " 5       (       a  UR7                  UUR8                  5      nU$ U" U5      nU$ s  snf s  snf )Nr   r   r   r   )create_dataframe_from_blocksF)r?  )r   r   )BlockManager)	DataFrame)r   r!  r4  _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.internalsrK  rC  r1  rL  pandasrM  r9  	_from_mgraxes)optionstablerj   ignore_metadatatypes_mapperall_columnsr   r!  r   r   ext_columns_dtypesr   r   r_   rK  r=  blocksr   rL  rM  rZ  mgrs                         r6   table_to_dataframerc    s    KNll22O:%i0(,,-=rB+O<!%9)%*5E2zC ))%..922|j
 ,K8'NKG%%LVV##GJ$();)@)@)B$CEFE
 
  $6UL 	 

 *&O	6$ 
 t3EF 	 
 64(  ""$$S#((3B 	 3B	+

s   F4F9>   r   r   r   r   r   r   r>   r   r   r   r   r   r   c                 L   US   nU=(       d    / n0 n[         R                  c  U$ U(       a:  U R                   H*  nUR                  nU" U5      n	U	c  M  XUR                  '   M,     U R                   Ha  nUR                  nUR                  U;  d  M!  [        U[        R                  5      (       d  MB   UR                  5       n	XUR                  '   Mc     U H  n
 U
S   nU
S   nX;  d  M  U[        ;  d  M!  [         R                  " U5      n	[        U	[         R                  5      (       d  MX  [        U	[         R                  R                  5      (       aY  U(       d  X;   a  M   [        R                  R!                  U R                  R#                  U5      R                  5      (       a  M   [%        U	S5      (       d  M  XU'   M     [         R&                  " 5       (       Ga  U(       d  U R                   H  nUR                  U;  d  M  [        R                  R)                  UR                  5      (       d^  [        R                  R+                  UR                  5      (       d0  [        R                  R-                  UR                  5      (       d  M  UR                  U;  d  M  [         R                  R                  [.        R0                  S9XgR                  '   M     U$ ! [         a     GM  f = f! [         a	    U
S   n GN.f = f! [         a     GNnf = f)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_categoricalrw   rv   ry   r0  )na_value)r   extension_dtyper   rX   rv   rA   r   BaseExtensionTypeto_pandas_dtypeNotImplementedErrorr@   _pandas_supported_numpy_typesrB  r   StringDtyperH   is_dictionaryr   rY   uses_string_dtype	is_stringis_large_stringis_string_viewrP   nan)r\  columns_metadatar^  r[  rj   re  ext_columnsr   typrB  col_metarv   rW   s                r6   rP  rP  M  s    %%=>!rJK ""* \\E**C',L'*6EJJ'	 " jj::[(ZR=Q=Q-R-R7"224 +7EJJ'  %	$L)D &"u4Q'Q '33E:L,(C(CDDlKNN,F,FGG .1C 8811%,,2D2DT2J2O2OPP$ Q <)9::(4%5 %: $$&&/E\\Ezz,""5::..88++EJJ7788**5::66**J.*5..*D*Dbff*D*UJJ' " Y '   	$F#D	$( $ s7   6K-K?*AL-
K<;K<?LL
L#"L#c                 6    [        S U  5       5      (       d   eg )Nc              3   d   #    U  H&  nUS    SL =(       a    SU;   =(       d    US    SLv   M(     g7f)rv   Nrw   r4   ).0r   s     r6   	<genexpr>:_check_data_column_metadata_consistency.<locals>.<genexpr>  s;      A 
6d		0|q0JQvYd5JJs   .0)all)r_  s    r6   rR  rR    s(    
      r5   c           
      h   U(       a]  U Vs0 s H%  nUR                  S[        US   5      5      US   _M'     nnU R                   Vs/ s H  oTR                  XU5      PM     nnOU R                  n[        U5      S:  a]  [        R
                  R                  R                  [        [        [        R                  U5      5      U Vs/ s H  owS   PM	     snS9nO,[        R
                  R                  Xb(       a  US   S   OS S9n[        U5      S:  a  [        X5      nU$ s  snf s  snf s  snf )Nrw   rv   r   r   r   r   )r4  r   r   ri   r   r   
MultiIndexfrom_tuplesr   r   astliteral_evalIndex"_reconstruct_columns_from_metadata)	block_tabler_  r   r   columns_name_dictrv   columns_values	col_indexr   s	            r6   rS  rS    s:    !
  EE, 7&	 BCQvYN  	 

 ;F:R:R
:R$!!$-:R 	 
 %11 >Q ..++77S%%~676DEnV$nE 8 

 ..&&n!26!:RV ' 

 >Q4WMN9

 Fs   ,D%D*	D/
c                    U Vs0 s H  nUR                  SUS   5      U_M     nn/ n/ nU nU H  n	[        U	[        5      (       a  [        XXU5      u  pnU
c  M-  OcU	S   S:X  aI  U	S   n[        R
                  R                  U	S   U	S   U	S   US9n
[        U
5      [        U 5      :w  a  M  O[        S	U	S    35      eUR                  U
5        UR                  U5        M     [        R
                  n[        U5      S
:  a  UR                  R                  XgS9nX4$ [        U5      S
:X  a4  US   n[        XR                  5      (       d  UR                  XS   S9nX4$ UR                  U R                  5      nX4$ s  snf )Nrw   rv   r   r   r   r   r   )r   rv   zUnrecognized index kind: r   r~  r   r   )r4  rA   rZ   _extract_index_levelr   r   r   ri   r   r   r  from_arraysr  rQ  )r\  r   r_  r^  r   field_name_to_metadataindex_arraysindex_namesresult_tabler   r   
index_namer   r   s                 r6   rO  rO    s    A 	
lAfI&)   LKL"eS!!4HUL5R1Lz" # 6]g%vJ%..33E'N49&M9>v9C 4 EK ;3u:- . 8vHIIK(:&' #* 
B <1)),)J  
\	a	Q%**HHUQH8E  enn-Ys    Fc                    X2   S   n[        X%5      nU R                  R                  U5      nUS:X  a  US S 4$ U R                  U5      nUR	                  US9n	S U	l        UR                  UR                  R                  U5      5      nXU4$ )Nrv   )r^  ) _backwards_compatible_index_namer   get_field_indexrm   	to_pandasrv   remove_column)
r\  r  rw   r  r^  logical_namer  r   r   r   s
             r6   r  r    s    )5f=L1*KJ$$Z0ABwT4''
,,q/C--\-:KK--++J7L j00r5   c                 2    X:X  a  [        U 5      (       a  gU$ )a  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$Fr5   c                 6    Sn[         R                  " X5      S L$ )Nz^__index_level_\d+__$)rematch)rv   patterns     r6   r   r   ,  s    &G88G"$..r5   c                      [         (       df  [         R                  SSSS[        R                  S[        R                  [        R
                  [        R                  [        R                  S.
5        [         $ )NrM   rg   rZ   )
r   r<   r;   r   r   rN   r  floatingr=   r   )_pandas_logical_type_mapr   rP   rS   r   r   object_r4   r5   r6   get_pandas_logical_type_mapr  1  sW     $# ''#(*YYxx

zzZZ)
 	 $#r5   c                     [        5       n X   $ ! [         a1    SU ;   a  [        R                  s $ [        R                  " U 5      s $ f = f)zGet 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@   rP   r  rW   )rx   pandas_logical_type_maps     r6   _pandas_type_to_numpy_typer  D  sK     :;%&33 %k!::xx$$	%s     AA
Ac                 h   [         R                  n[        U SS5      =(       d    U /n[        U SS5      =(       d    S/n[        X10 S9 VVs/ s H<  u  pVXVR	                  S[        UR                  5      5      UR	                  SS5      4PM>     nnn/ n[        R                  " SS5      n	U GH  u  pZn[        U
5      nU[        R                  :X  a  UR                  U	5      nGO2U
S	:X  a  [        R                  R                  US
   S   S   5      nUR!                  USS9R#                  U5      n[         R$                  " 5       (       a(  UR'                  [        R(                  " U5      S
   5      nOU
S:X  aE  [         R                  R+                  U Vs/ s H  n[,        R.                  " U5      PM     sn5      nOWUR                  S:X  a&  US:X  a   SU
;   d  U
S;   a  UR1                  U5        GMJ  UR                  U:w  a  UR3                  U5      nUR                  U:w  a  U
S	:w  a  UR3                  U5      nUR1                  U5        GM     [5        U5      S:  a  UR7                  XU R8                  S9$ UR+                  US
   US
   R                  U R:                  S9$ s  snnf s  snf )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   Nrk   )	fillvaluerx   ry   r   r   r;   r   ro   rf   T)utcr=   rZ   r>   r   )r   rN   r   r~  )rW   rv   )r   r   rh   r   r4  rZ   rW   operatormethodcallerr  rP   rS   r   r   r   rH  to_datetime
tz_convertrV  as_unitr7  r  r=   Decimalr   astyperi   r  r   rv   )r   r   r   r   labelsr   r  levels_dtypes
new_levelsencoderrB  numpy_dtyperW   rG   r   s                  r6   r  r  [  si   , 
B Wh-:'FWgt,6F !,b!
!
E 
mS-=>	|T	*	,!
   J##Hg6G,9([*<8 BIIIIg&E\)((q!*-j9;BNN5dN3>>rBE##%% b&6&6u&=a&@AY&NN((e)Le'//!*<e)LMEKK5 [H%<L(L<Q,Q e$[[E!LL'E;;+%,,*FLL-E% O -:R :}}Zw}}}EExx
1Z]-@-@w||xTTo: *Ms   AJ) J/
c                    0 n0 nU R                   nUS   nU Vs/ s H  n[        U[        5      (       d  M  UPM     nn[        U5      n[        US   5      U-
  n[	        US   5       GH@  u  pU
R                  S5      nU(       d  U
S   nX:  a  XYU-
     nUc  SnUR                  U5      nUS:w  d  MM  U
S   S:X  d  MX  X   n[        UR                  [        R                  R                  5      (       d  M  U
S	   nU(       d  M  UR                  S
5      nU(       d  M  XR                  R                  :w  d  M  UR                  5       n[        R                  " SUS9n[        R                  R                  UUS9n[        R                   " XL   R"                  U5      X<'   UX,'   GMC     [        U5      S:  a  / n/ n[%        [        U R                   5      5       Ha  n	X;   a(  UR'                  X)   5        UR'                  X9   5        M0  UR'                  X	   5        UR'                  U R                   U	   5        Mc     [        R(                  R+                  U[        R                   " U5      S9$ U $ s  snf )Nr   r   rw   rv   Noner  rx   r;   ro   rf   rE  rF  )rX   r   )r   )r   rA   rZ   ri   r   r4  r  rX   r   r   rF   rG   r  r'  Arrayr   r   rv   r   r   Tabler  )r\  r!  modified_columnsmodified_fieldsr   r   idx_coln_index_levels	n_columnsr   rv  r  idxr   ro   metadata_tz	convertedtz_aware_typer  r   r   s                        r6   rN  rN    s9   O\\F#O4M,9 2M"7C0 MM 2'NOI./.@I !!;<<<-'H~(Y7!$$X."9&,6j!#((BFF,@,@AA#J/&ll:6;;((++#= #I$&LL+$FM$&HH$8$8>K %9 %MM ,.88FK4D4D4A,CO(,9$)= =@ q s5<<()A$/23o01ux(ell1o. * xx##GBIIf4E#FFe2s
   I8I8c                     [         R                  R                  U5      nU R                  R	                  S5      R                  R                  U5      n U $ )z:
Make a datetime64 Series timezone-aware for the given tz
r  )r   r   rH  dttz_localizer  )seriesrG   s     r6   make_tz_awarer    sA     
	 	 	$Bii##E*R

2 Mr5   r   )r   NT)NNT)NFN)?r  collections.abcr   
concurrentr   concurrent.futures.threadr-  r   r=   	itertoolsr   r   r  r  r   numpyrP   ImportErrorr   r   pyarrow.libr   r   r	   r   rO   r  r7   rD   rT   r`   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   r#  r   rC  r8  rc  rk  rP  rR  rS  rO  r  r  r   r  r  r  rN  r  r4   r5   r6   <module>r     sQ  &  $  !   !   	   D D    :"#,"$&,b +/\~($N(&?QD;Q|
*9
 &F IM!a"H&BJ4 JN4r! PfB2l ?C1&0/
$&%.TUn:Ba&  	Bs   C CC