
    y-Ph                        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	 d dlZn# e$ r dZY nw xY wd dlZd dlmZmZmZ i ai ai ad Zd Zd	 Zd
 Zd Zd Z 	 d.dZ!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)d Z*d Z+d.dZ,	 	 d/dZ-d Z.d0dZ/d Z0	 d1d Z1h d!Z2d" Z3d# Z4d$ Z5d.d%Z6	 d.d&Z7d' Z8d( Z9d) Z:d* Z;d+ Z<d, Z=d- Z>dS )2    N)Sequence)futures)deepcopy)zip_longest)_pandas_api	frombytesis_threading_enabledc            	      "   t           st                               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     U/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyarrow/pandas_compat.pyget_logical_type_mapr7   .   sw        "
FNG"
Ff"
 Ff"
 Fw	"

 Fw"
 Fw"
 Fw"
 F"
 F"
 F"
 F"I"
 Fy"
 F	"
 F"
 F"
  F!"
" F#"
$ FF)7F	)"
 "
 	 	 	, r5   c                    t                      }	 || j                 S # t          $ r t          | t          j        j                  rY dS t          | t          j        j                  r)d                    t          | j
                            cY S t          | t          j        j                  r| j        dndcY S t          j                            |           rY dS Y dS 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types
is_decimal)
arrow_typelogical_type_maps     r6   rD   rD   K   s    +--
.. 	 	 	j"&"788 	 ==
BFO44 	$$%5j6K%L%LMMMMM
BF$899 	#-=#<<<*LLLX  ,, 	99xx	s"    )C#	AC#*C#=C#"C#c                     t           st                               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_r4   r5   r6   get_numpy_logical_type_maprT   \   s    " &&HfGVHgHgHgHgIxIxIxJ	J	VGXIw(
 	 	 	  #"r5   c                 L   t                      }	 || j        j                 S # t          $ rw t	          | j        d          rY dS t          | j                                      d          rt          | j                  cY S t          j        |           }|dk    rY dS |cY S w xY w)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9%&7&=&BCC 
 
 
$*D11 	 <<  &''22<@@ 	0(./////():;;X99
s!   " B#;B#B#B#"B#c                 h   | j         }t          |          dk    rKt          | d|           }|J t          |j                  |j        d}t          |j        j                   }nJt          |d          r)dt          j	        
                    |j                  i}d}nd }t          |          }||fS )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    LE
5zzZvuf--!$/22|
 
 TZ-..			 $ 7 7 A AB)U8##r5   c                    t          |          }t          |           \  }}|dk    r|j        |j        d}d}|st	          |t
                    rt          j        |          sJt	          |t                    s5t          d
                    |t          |          j                            t	          |t                    s$J 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
    r<   )	precisionscaler=   Nz6Column name must be a string. Got column {} of type {}name
field_namepandas_type
numpy_typero   )rD   rq   rs   rt   r@   floatrP   isnanrZ   	TypeErrorrC   rX   __name__)rm   rv   rJ   rw   logical_typestring_dtypeextra_metadatas          r6   get_column_metadatar      s   " $J//L#;F#C#C L.y  #-%
 
   	D%(( 	-/Xd^^ 	4%% 	 DKKd4jj) 
 
 	
 j#&&==D,<,<(=(==== #""  r5   c           	      |   |d |D             }t          d |D                       }t          |          }	|d|	|z
           }
||	|z
  d         }g }t          | |||
          D ]/\  }}}}t          ||||          }|                    |           0g }|durSg }t          |||          D ]\  }}}t	          |t
                    r|j        4t	          |j        t                    s|                    |j                   t          |t          |j                  ||          }|                    |           t          |          dk    r t          j
        d| dt          d	
           g }t          |j        d|j        g          }t          |j        d|j        j        g          }t          ||          D ]*\  }}t          ||          }|                    |           +ng x}x}}dt          j        ||||z   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                 ,    g | ]}t          |          S r4   )rZ   ).0rv   s     r6   
<listcomp>z&construct_metadata.<locals>.<listcomp>   s    AAADc$iiAAAr5   c                 <    g | ]}t          |t                    |S r4   )r@   dict)r   descrs     r6   r   z&construct_metadata.<locals>.<listcomp>   s=     'G 'G 'G.8.E.E'Gu 'G 'G 'Gr5   )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)ri   zipr   appendr@   r   rv   rZ   _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_namesnum_serialized_index_levelsntypesdf_typesindex_typescolumn_metadatacolrv   rw   rJ   ro   index_column_metadatanon_str_index_nameslevel
descriptorr   r   r   s                            r6   construct_metadatar      s   * ! BALAAA"% 'G 'G:K 'G 'G 'G #H #H ZZF:f:::;H!<<==>KO-01C\1CX.O .O ) ))T:z&s2<2<> > > 	x((((U"" -0{1B.D .D 	3 	3)E:z*d++  z%jS.I.I%#**5:666*,UZ88%%	  H "((2222"##a''M09L 0 0 0 	+ + + + X
|<<
Gbjo->??vu-- 	, 	,KE43E4@@H!!(++++	, FHGG1N 	4:.,&)>>$>  *1	
 	
 	 	 6&>> r5   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.r   r   r   encodingUTF-8ru   )rq   r`   r   r   r   )r   rv   r   r   rx   s        r6   r   r   %  s    #;E#B#B L.-e44K+@A	' 	' 	' 	' i!!!!$g."""  r5   c                    t          | t                    r| S t          | t                    r|                     d          S t          | t                    r/t          t	          t          t          |                               S t          | t                    rt          d          | )t          | t                    rt          j        |           r| 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 level)r@   rZ   r   decodetuplemapr   r   r|   rz   rP   r{   rv   s    r6   r   r   9  s    2 $ 
	D%	 	  {{6"""	D%	 	  54d;;<<===	D(	#	# ?@@@	*T511bhtnnt99r5   c                 t    | j         | j         |vrt          | j                   S d                    |          S )zReturn the name of an index level or a default name if `index.name` is
    None or is already a column name.

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

    Returns
    -------
    name : str
    Nz__index_level_{:d}__)rv   r   rC   )indexir   s      r6   _index_level_namer   `  s<     z%*L"@"@&uz222%,,Q///r5   c                    t          | ||          }| j        j        s4t          d                    t          | j                                      |t          | ||          S g }g }|durt          | j                  ng }g }g }|D ]}	| |	         }
t          |	          }	t          j        |
          r"t          d                    |	                    |                    |
           |                    d            |                    |	           |                    t          |	                     g }g }t          |          D ]\  }}t!          |||          }	t#          |t          j        j                  r|t)          |          }nA|                    |           |                    d            |	}|                    |	           |                    |           ||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_sparser|   r   rZ   	enumerater   r@   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   s  sH   *2vw??G: 
.55d2:6F6FGG
 
 	
 3BOOOL .<5-H-H))) 
 N - -h&t,, %% 	N?FFtLLN N N 	!!#&&&d###D!!!!!#d)),,,,#L11 
( 
(; a>>{KN$=>> 	,&/<<EE%%k222!!$'''E%%d+++  ''''"%77I |%79K|-?Q Qr5   c                    g }g }g }g }g }g }|j         D ]}		 | |	         }
d}n# t          $ r 	 t          | |	          }
n7# t          t          f$ r# t          d                    |	                    w xY w|du r"t          d                    |	                    |At          |
t          j        j	                  r"t          d                    |	                    d}Y nw xY wt          j
        |
          r"t          d                    |	                    |                    |	          }|                    |
           |                    |           |                    |	           |r?|                    |	           |                    |	           |                    |
           ||z   }||||||||fS )z
    Specialized version of _get_columns_to_convert in case a Schema is
    specified.
    In that case, the Schema is used as the single point of truth for the
    table structure (types, which columns are included, order of columns, ...).
    FzNname '{}' present in the specified schema is not found in the columns or indexzlname '{}' present in the specified schema corresponds to the index, but 'preserve_index=False' was specifiedNa  name '{}' is present in the schema, but it is a RangeIndex which will not be converted as a column in the Table, but saved as metadata-only not in columns. Specify 'preserve_index=True' to force it being added as a column, or remove it from the specified schemaTr   )r   r?   _get_index_level
IndexErrorrC   r   r@   r   r   r   r   r|   fieldr   )r   r   r   r   r   r   r   r   r   rv   r   is_indexr   r   s                 r6   r   r     s6    LNL (% (%	T(CHH 	 	 	<&r400j) < < <..4fTll< < <<
 &&   &t. . . !(sKN$=>> ) '
 (.vd||5 5 5 HHH-	0  %% 	N?FFtLLN N N T""!!#&&&e$$$D!!! 	%%%d+++$$T***$$$11I|\3E|-?Q Qs$   
#
C$?C$4A33A.C$#C$c                     |}|| j         j        vr3t          |          r$t          |t	          d          d                   }| j                             |          S )z_
    Get the index level of a DataFrame given 'name' (column name in an arrow
    Schema).
    __index_level_)r   r   _is_generated_index_nameintri   get_level_values)r   rv   keys      r6   r   r     s^    
 C28>!!&>t&D&D! $s+,,R/0118$$S)))r5   c                 n    	 t          j        |            | S # t          $ r t          |           cY S w xY wN)r   r   r|   rZ   r   s    r6   _level_namer      sI    
4   4yys    44c                     dt          | j                  t          j        | d          t          j        | d          t          j        | d          dS )Nrangestartstopstep)kindrv   r   r   r   )r   rv   r   get_rangeindex_attribute)r   s    r6   r   r   	  sS     EJ''5eWEE4UFCC4UFCC  r5   c                 x     t          t           d g                    } fdt          |          D             S )Nr   c                 :    g | ]}                     |          S r4   )r   )r   r   r   s     r6   r   z+_get_index_level_values.<locals>.<listcomp>  s'    888!E""1%%888r5   )ri   rh   r   )r   ns   ` r6   r   r     s>    GE8eW--..A8888uQxx8888r5   c                 r     ||t          d          ||j        }n| fd|D             }n j        }|S )NzJSchema and columns arguments are mutually exclusive, pass only one of themc                 &    g | ]}|j         v |S r4   )r   )r   cr   s     r6   r   z0_resolve_columns_of_interest.<locals>.<listcomp>   s     999bj1r5   )r   r   r   )r   r   r   s   `  r6   r   r     sa    g1 < = = 	=		,		9999g999*Nr5   c           
         t          | d ||          \  }}}}}}}	}g }
|	D ]}|j        }t          j        |          rt	          j        |d          j        }nt          j        |          rZt          |t          j	        j
                  r|                    d          n	|d d         }t	          j        |d          j        }nVt          ||j        d           \  }}t          j                            ||          }|t	          j        |d          j        }|
                    |           t#          |	| |||||
|          }||
|fS )NT)from_pandasr   r   )r   valuesr   is_categoricalr   arrayrX   is_extension_array_dtyper@   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   '  st    ""dNG
D
DYE  %f-- 
	;HQD1116EE1&99 	;!+;>("* "* 5AFF1III/0!u HU555:EE/FFMFEF11&%@@E}555:U!Bm=N2D  H
 eX%%r5      Tc           
        " t          | |||          \  }}}}	}
}}}|Ht          |           t          | j                  }}||dz  k    r|dk    rt          j                    }nd}t                      sd}fd"d }|dk    r"fdt          ||          D             }ng }t          j        |          5 }t          ||          D ]_\  }} ||j	                  r |
                     "||                     5|
                    |                    "||                     `	 d d d            n# 1 swxY w Y   t          |          D ]6\  }}t          |t          j                  r|                                ||<   7d |D             }|Tg }t          ||          D ]-\  }}|
                    t          j        ||                     .t          j        |          }t%          || |||
|||          }|j        rt)          |j                  nt+                      }|                    |           |                    |          }d }t          |          dk    rn	 |
d         d	         }|d
k    rH|
d         d         }|
d         d         } |
d         d         }!t          t1          || |!                    }n# t2          $ r Y nw xY w|||fS )Nd   r   c                    |d}d }n|j         }|j        }	 t          j        | |d          }n_# t          j        t          j        t          j        f$ r6}|xj        d                    | j	        | j
                  fz  c_        |d }~ww xY w|s@|j        dk    r5t          d                    t          |          |j                            |S )NT)rX   r   safez0Conversion failed for column {!s} with type {!s}r   z>Field {} was non-nullable but pandas column had {} null values)nullablerX   r   r   ArrowInvalidArrowNotImplementedErrorArrowTypeErrorargsrC   rv   rW   
null_countr   rZ   )r   r   field_nullabler   r_   er  s         r6   convert_columnz+dataframe_to_arrays.<locals>.convert_columnc  s    =!NEE"^NJE	Xc4dKKKFF+!# 	 	 	 FFIsx336 6FFG	  	M&"3a"7"7 228&U9?9J3L 3LM M M s   1 &B1BBc                     t          | t          j                  o/| j        j        o#t          | j        j        t          j                  S r   )	r@   rP   ndarrayflags
contiguous
issubclassrW   rX   integer)arrs    r6   _can_definitely_zero_copyz6dataframe_to_arrays.<locals>._can_definitely_zero_copyy  s;    3
++ 7	$739>2:66	8r5   c                 .    g | ]\  }} ||          S r4   r4   )r   r   fr  s      r6   r   z'dataframe_to_arrays.<locals>.<listcomp>  s?     G G Ga !.A&& G G Gr5   c                     g | ]	}|j         
S r4   rX   )r   xs     r6   r   z'dataframe_to_arrays.<locals>.<listcomp>  s    $$$QV$$$r5   r   r   r   r   r   r   r   )r   ri   r   r   	cpu_countr	   r   r   ThreadPoolExecutorr   r   submitr   r@   Futurer_   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  arraysexecutorr   r  r   	maybe_futrH   fieldsrv   r   pandas_metadataro   n_rowsr   r   r   r   r  s#        `                            @r6   dataframe_to_arraysr'  J  s    /r6>/68 8Y 2wwBJu53;5199|~~HHH!!     ,8 8 8
 1}}G G G G!"4nEEG G G '11 	IX.?? I I1,,QX66 IMM..A"6"67777MM(//.!Q"G"GHHHH	I	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I &f-- 	/ 	/LAy)W^44 /%,,..q	$$V$$$E~y%00 	1 	1KD%MM"(4//00006""(Bm=N2D  O -3OGx(((HOOO$$$!!(++F F
6{{a	$Q'/Dw)!,W5(+F3(+F3U5$5566 	 	 	D	 66!!s&   9A1D77D;>D;*AK 
KKc                     | j         j        t          j        k    r| |fS t	          j        |          r&|$|j        }|j        }t          j	        ||          }n|t          j
        | j                   }| |fS r   )rW   rX   rP   rV   r   is_datetimetzrG   unitr   	timestampfrom_numpy_dtype)r   rW   r   rG   r*  s        r6   r   r     sw    |BM))u} '' 2EMXzT2&&	#FL115=r5   c                    ddl mc m} |                     dd          }| d         }d| v r0t          j                            || d         | d                   }nd| v rt          j        |j	                  \  }}	t          || d                   }
t	          j                    r6t          j                            |                    d	          |
d
          }n|}|r |                    |||j        |
          }|S nnd| v rh| d         }t#          |          dk    sJ ||d                  }||         }t%          |d          st'          d          |                    |          }n|}|r|                    ||          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
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   r4  )itemr   extension_columnsreturn_block_int	block_arrr/  r  r*  r   rW   r.  rv   pandas_dtypes                 r6   _reconstruct_blockrG    s   8 )(((((((($''I[!It*55$|"4O 6 % % 
t		"9?33ad:&677 "" 
	.&&w''u5 '  CC C 	Y.2.B.3 ( 5 5 	
 
t		:9~~""""y|$(.|%566 	; : ; ; ;))#.. si888I~r5   c                     t          j                    rd} t          j                            |          }t          j        | |          S )NnsrG   )r   is_v1r   r   string_to_tzinfodatetimetz_type)r*  rG   s     r6   r<  r<    sC     		 	 	$	$B&t3333r5   Fc           	      h   g }g }|j         j        }|sa|_|d         }|                    dg           }|d         }t          ||          }t	          ||||          \  }}	t          |||| |          n7t          j                            |j	                  }	t          |g || |          t          |           t          |||          }
|j        t          j                            | ||t!                                                              }t          j                    r%ddlm} fd|D             } |||	|
          }|S ddlm} dd	lm} fd
|D             }|
|	g} |||          }t          j                    r|                    ||j                  }n ||          }|S )Nr   r   r   r   )create_dataframe_from_blocksc                 6    g | ]}t          |d           S )F)rC  rG  r   rA  r   ext_columns_dtypess     r6   r   z&table_to_dataframe.<locals>.<listcomp>-  sG     
 
 
  l$6UL L L
 
 
r5   )r   r   )BlockManager)	DataFramec                 2    g | ]}t          |          S r4   rQ  rR  s     r6   r   z&table_to_dataframe.<locals>.<listcomp>8  s6     
 
 
 t\3EFF
 
 
r5   )r   r%  r8  _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.internalsrO  r5  rT  pandasrU  r=  	_from_mgraxes)optionstablerj   ignore_metadatatypes_mapperall_columnsr   r%  r   r   r   r_   rO  blocksr   rT  rU  rc  mgrr   rS  s                      @@r6   table_to_dataframerk    sN    KNl2O 
:%i0(,,-=rBB+O<!%99)%1B*5|E Eu2;gzC C ))%.9922|Wj
 
 ,K888'{NKKG%LV##GUJ$();)@)@)B)B$C$CE EF EEEEEE
 
 
 
 
 
 
 

 *)&wOOO	666666$$$$$$
 
 
 
 

 
 
 l64(( "" 	 $$S#(33BB3B	r5   >   r   r   r   r   r   r   r=   r   r   r   r   r   r   c                    |d         }|pg }i }t           j        |S |r(| j        D ] }|j        } ||          }	|	
|	||j        <   !| j        D ]\}|j        }|j        |vrJt          |t          j                  r0	 |                                }	|	||j        <   L# t          $ r Y Xw xY w]|D ]}
	 |
d         }n# t          $ r |
d         }Y nw xY w|
d         }||vr|t          vrt          j        |          }	t          |	t           j                  rt          |	t           j        j                  rV|s||v r	 t          j                            | j                            |          j                  rn# t          $ r Y nw xY wt%          |	d          r|	||<   t          j                    r|s| j        D ]}|j        |vrt          j                            |j                  sHt          j                            |j                  s$t          j                            |j                  r;|j        |vr2t           j                            t.          j                  ||j        <   |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_categoricalNrw   rv   ry   r4  )na_value)r   extension_dtyper   rX   rv   r@   r   BaseExtensionTypeto_pandas_dtypeNotImplementedErrorr?   _pandas_supported_numpy_typesrF  r   StringDtyperH   is_dictionaryr   rY   uses_string_dtype	is_stringis_large_stringis_string_viewrP   nan)re  columns_metadatarg  rd  rj   rm  ext_columnsr   typrF  col_metarv   rW   s                r6   rY  rY  O  s    %%=>!rJK "*  7\ 	7 	7E*C'<,,L'*6EJ'  7 7j:[((ZR=Q-R-R(7"2244 +7EJ'' '    % 5 5	$L)DD 	$ 	$ 	$F#DDD	$&{""u4Q'Q'Q '3E::L,(CDD 5lKN,FGG  . !1C1C 811%,2D2DT2J2J2OPP %$%#   <)9:: 5(4K% $&& V/E V\ 	V 	VEz,,""5:.. -8++EJ77 - 8**5:66 - *J..*5.*D*Dbf*D*U*UEJ's6   ;B
B'&B'0B99CC;<E99
FFc                 <    t          d | D                       sJ d S )Nc              3   F   K   | ]}|d          du rd|v p	|d          duV  dS )rv   Nrw   r4   r   r   s     r6   	<genexpr>z:_check_data_column_metadata_consistency.<locals>.<genexpr>  sW         
6d		0|q0JQvYd5J     r5   )all)rh  s    r6   r[  r[    sF    
            r5   c                    |r d |D             fd| j         D             }n| j         }t          |          dk    rVt          j        j                            t          t          t          j	        |                    d |D                       }n1t          j        
                    ||r|d         d         nd           }t          |          dk    rt          ||          }|S )	Nc           	      n    i | ]2}|                     d t          |d                             |d         3S rw   rv   )r8  r   r  s     r6   
<dictcomp>z-_deserialize_column_index.<locals>.<dictcomp>  sI     
 
 
 EE, 7&	 B BCCQvY
 
 
r5   c                 <    g | ]}                     ||          S r4   r8  )r   rv   columns_name_dicts     r6   r   z-_deserialize_column_index.<locals>.<listcomp>  s7     
 
 
26!!$--
 
 
r5   r   c                     g | ]
}|d          S r   r4   )r   	col_indexs     r6   r   z-_deserialize_column_index.<locals>.<listcomp>  s    EEE9V$EEEr5   r   r   rv   r   )r   ri   r   r   
MultiIndexfrom_tuplesr   r   astliteral_evalIndex"_reconstruct_columns_from_metadata)block_tablerh  r   columns_valuesr   r  s        @r6   r\  r\    s#    	2
 
 
 
 

 
 
 
:E:R
 
 
 %1 >Q .+77S%~6677EEnEEE 8 
 

 .&&n!V!26!:!:RV ' 
 

 >Q4WnMMNr5   c                 Z   d |D             }g }g }| }|D ]}t          |t                    rt          | ||||          \  }}	}
|	1n|d         dk    r_|d         }
t          j                            |d         |d         |d         |
          }	t          |	          t          |           k    rn(t          d	                    |d                             |	                    |	           |	                    |
           t          j        }t          |          d
k    r|j
                            ||          }nht          |          d
k    r;|d         }t          ||j                  s|                    ||d                   }n|                    | j                  }||fS )Nc                 H    i | ]}|                     d |d                   | S r  r  r  s     r6   r  z&_reconstruct_index.<locals>.<dictcomp>  s<        	
lAfI&&  r5   r   r   rv   r   r   r   )r   rv   zUnrecognized index kind: {}r   r  r   r   )r@   rZ   _extract_index_levelr   r   r   ri   r   rC   r   r  from_arraysr  rZ  )re  r   rh  rg  field_name_to_metadataindex_arraysindex_namesresult_tabler   r   
index_namer   r   s                r6   rX  rX    s       LKL" ' 'eS!! 	54H|U,BL5R 5R1L+z" # 6]g%%vJ%.33E'N49&M9>v9C 4 E EK ;3u::-- . :$fU6]335 5 5K(((:&&&&	B <1)),k)JJ	\		a		Q%** 	9HHUQH88Een--r5   c                 P   ||         d         }t          ||          }| j                            |          }|dk    r|d d fS |                     |          }|                    |          }	d |	_        |                    |j                            |                    }||	|fS )Nrv   )rg  ) _backwards_compatible_index_namer   get_field_indexrm   	to_pandasrv   remove_column)
re  r  rw   r  rg  logical_namer  r   r   r   s
             r6   r  r    s    )*5f=L1*lKKJ$$Z00ABwwT4''
,,q//C--\-::KK--++J77 L j00r5   c                 4    | |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  s     r6   r  r    s(    $ <$<X$F$Ftr5   c                 4    d}t          j        ||           d uS )Nz^__index_level_\d+__$)rematch)rv   patterns     r6   r   r   /  s    &G8GT""$..r5   c                      t           sWt                               ddddt          j        dt          j        t          j        t          j        t          j        d
           t           S )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  4  s^     $  ''#(*Yx
zZ)
 )
 	 	 	 $#r5   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   )r  r?   rP   r  rW   )rx   pandas_logical_type_maps     r6   _pandas_type_to_numpy_typer  G  sj     :;;%&{33 % % %k!!:x$$$$$	%s    AA
Ac                    t           j        }t          | dd          p| g}t          | dd          pdg}d t          ||i           D             }g }t	          j        dd          }|D ]\  }}	}
t          |	          }|t          j        k    r|	                    |          }n"|	dk    rt          j                            |d	         d
         d                   }|                    |d                              |          }t          j                    r-|                    t          j        |          d	                   }n|	dk    r*t           j                            d |D                       }nP|j        dk    r%|
dk    rd|	v s|	dv r|                    |           ?|j        |k    r|                    |          }|j        |
k    r|	dk    r|                    |
          }|                    |           t-          |          dk    r|                    ||| j                  S |                    |d	         |d	         j        | j                  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   Nrk   c           	          g | ]D\  }}||                     d t          |j                            |                     dd          fES )rx   ry   N)r8  rZ   rW   )r   r   r  s      r6   r   z6_reconstruct_columns_from_metadata.<locals>.<listcomp>{  s^        E9 
	mS-=-=>>	|T	*	*	,  r5   )	fillvaluer   r   r:   r   ro   rf   T)utcr<   c                 6    g | ]}t          j        |          S r4   )r<   Decimal)r   r   s     r6   r   z6_reconstruct_columns_from_metadata.<locals>.<listcomp>  s"    )L)L)L'/!*<*<)L)L)Lr5   rZ   r=   r   )r   rN   r   r  )rW   rv   )r   r   rh   r   operatormethodcallerr  rP   rS   r   r   r   rL  to_datetime
tz_convertr_  as_unitr;  r  rW   r   astyperi   r  r   rv   )r   r   r   r   labelslevels_dtypes
new_levelsencoderr   rF  numpy_dtyperW   rG   s                r6   r  r  ^  s   , 
B Wh--:'FWgt,,6F  !,Nb!
 !
 !
  M J#Hg66G,9 '! '!(|[*<88 BIIIg&&EE\))((q!*-j9; ;BNN5dN33>>rBBE#%% B b&6u&=&=a&@AAY&&N(()L)Le)L)L)LMMEEK5  [H%<%<L((L<Q,Q,Q e$$$[E!!LL''E;+%%,,*F*FLL--E%    
:}}Zw}}EEExx
1Z]-@w|xTTTr5   c                    i }i }| j         }|d         }d |D             }t          |          }t          |d                   |z
  }t          |d                   D ]3\  }}	|	                    d          }
|
s|	d         }
||k    r|||z
           }
|
d}
|                    |
          }|dk    r|	d         d	k    r| |         }t          |j        t          j        j	                  s|	d
         }|s|                    d          }|r||j        j
        k    rs|                                }t          j        d|          }t          j                            ||          }t          j        ||         j        |          ||<   |||<   5t          |          dk    rg }g }t#          t          | j                             D ]x}||v r7|                    ||                    |                    ||                    =|                    | |                    |                    | j         |                    yt          j                            |t          j         |                    S | S )Nr   c                 <    g | ]}t          |t                    |S r4   )r@   rZ   )r   idx_cols     r6   r   z%_add_any_metadata.<locals>.<listcomp>  s6     2 2 2"7C002W 2 2 2r5   r   rw   rv   Noner  rx   r:   ro   rf   rI  rJ  r  r   )r   )r   ri   r   r8  r  r@   rX   r   r   rF   rG   r  r+  Arrayr   r   rv   r   r   Tabler  )re  r%  modified_columnsmodified_fieldsr   r   n_index_levels	n_columnsr   r~  r  idxr   ro   metadata_tz	convertedtz_aware_typer  r   r$  s                       r6   rW  rW    s   O\F#O4M2 2M 2 2 2M''NOI.//.@I !!;<< : :8<<-- 	"'HI~~(Y7!$$X.."99&,66Cj!#(BF,@AA #J/ &ll:66 :;#(+#=#= #I$&L+$F$F$FM$&H$8$8>K %9 %M %MM ,.8F3K4D4A,C ,COC(,9$S)
q  s5<(()) 	/ 	/A$$$/2333oa01111uQx(((el1o....x##GBIf4E4E#FFFr5   c                     t           j                            |          }| j                            d          j                            |          } | S )zB
    Make a datetime64 Series timezone-aware for the given tz
    r  )r   r   rL  dttz_localizer  )seriesrG   s     r6   make_tz_awarer    sC     
	 	 	$	$Bi##E**

2 Mr5   r   )r   NT)NNT)NFN)?r  collections.abcr   
concurrentr   concurrent.futures.threadr1  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   rG  r<  rk  rs  rY  r[  r\  rX  r  r  r   r  r  r  rW  r  r4   r5   r6   <module>r     s  & 


 $ $ $ $ $ $       !              ! ! ! ! ! !   				    	BBB     D D D D D D D D D D      :  "# # #,  "$ $ $&- - -d +/\ \ \ \~  ($ $ $N0 0 0&?Q ?Q ?QD;Q ;Q ;Q|
* 
* 
*    9 9 9
   &  &  &  &F IM!b" b" b" b"J  &B B B BJ4 4 4 JN4 4 4 4r! ! ! P P Pf    B3 3 3 3n ?C1 1 1 1&  0/ / /
$ $ $&% % %.TU TU TUn: : :B    s   ; AA