� ���g�v��.�ddlZddlZddlmZddlmZddlmZddlm Z m Z m Z m Z m Z mZddlZddlZddlmZddlZddlmZe rddlmZmZee��Zd �Zd ed ej fd �Z!d ej"d ej fd�Z#d ed ej$fd�Z%d ed ej&fd�Z'd ed ej fd�Z(de)fd�Z*de+e,de,d e,fd�Z-Gd�d��Z.Gd�de.��Z Gd�de ��Z/Gd�de/��Z0e1ee1e)fZ2Gd�d e/��Z3e d!e/e+e/e+e+e/��Z4Gd"�d#e ��Z5dJd$e+e d%e,d e fd&�Z6d'e d e+efd(�Z7d)�Z8d*ej9d+e,d e:fd,�Z;d*ej9d ej<fd-�Z=d.ej>d ej>fd/�Z?d0e d efd1�Z@e8 dKd*ej<d3ej>d4e:d5e:d eej<ejAej9ejBejCff d6���ZDe8 dKd*ej<d7d8d4e:d5e:d ej<f d9���ZEe8d*ej<d7d8fd:���ZFGd;�d<eG��ZHd'ej d=d>fd?�ZId'ej d@ej&fdA�ZJd'ej fdB�ZKd'ej d@ej&fdC�ZLd'ej fdD�ZMd'ej dEe ej<gdffdF�ZNdLd'e dHe,d eej fdI�ZOdS)M�N)�Iterator)�partial)�groupby)� TYPE_CHECKING�Any�Callable�Optional�TypeVar�Union�)� get_logger)�Features� FeatureTypec����fd�}|S)Nc����j|j�|jndz|_|j�dd��|_t�d��r �j|_|S)N�� pyarrow.Table�Table�__annotations__)�__doc__�replace�hasattrr)�fn�arrow_table_methods ��^/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/datasets/table.py�wrapperz1inject_arrow_table_documentation.<locals>.wrappers^���'�/���AW�2�:�:�]_�`�� ��Z�'�'���A�A�� � �%�'8� 9� 9� D�!3�!C�B� �� ��)rrs` r� inject_arrow_table_documentationrs#�������� �Nr�filename�returnc��tj|��}tj�|��}|���}|S�N)�pa� input_stream�ipc� open_stream�read_all)r �in_memory_stream� opened_stream�pa_tables r� _in_memory_arrow_table_from_filer,"s>����x�0�0���F�&�&�'7�8�8�M��%�%�'�'�H� �Or�bufferc��tj|��}tj�|��}|���}|Sr#)r$� BufferReaderr&r'r()r-�streamr*�tables r�"_in_memory_arrow_table_from_bufferr2)s<�� �_�V� $� $�F��F�&�&�v�.�.�M� � "� "� $� $�E� �Lrc�h�tj|��}tj�|��Sr#)r$� memory_mapr&r')r �memory_mapped_streams r�,_memory_mapped_record_batch_reader_from_filer60s)���=��2�2�� �6� � �2� 3� 3�3rc��tj|��5}tj�|��j}ddd��n #1swxYwY|S)z� Infer arrow table schema from file without loading whole file into memory. Usefull especially while having very big files. N)r$r4r&r'�schema)r r5r8s r�read_schema_from_filer95s��� ��x� � �A�$8���#�#�$8�9�9�@��A�A�A�A�A�A�A�A�A�A�A����A�A�A�A� �Ms�%A�A � A c�L�t|��}|���}|Sr#)r6r()r r*r+s r�$_memory_mapped_arrow_table_from_filer;?s%��@��J�J�M��%�%�'�'�H� �Or�memoc ���|j}|�|��}||t|��<|j���D])\}}t ||t j||�����*|S)z!deepcopy a regular class instance)� __class__�__new__�id�__dict__�items�setattr�copy�deepcopy)�xr<�cls�result�k�vs r� _deepcopyrKEsp�� �+�C� �[�[�� � �F��D��A���K�� � � �"�"�3�3���1����4�=��D�1�1�2�2�2�2� �Mr�arrrFc���dt|��dz }}||kr�|||cxkr ||kr�nn�|||z |||z z||||z zz}|||cxkr||dzkrnn|S|||kr|dz|}}n||}}||kr|||cxkr ||k�~ntd|�dt|��r|dnd�d����)aa Return the position i of a sorted array so that arr[i] <= x < arr[i+1] Args: arr (`List[int]`): non-empty sorted list of integers x (`int`): query Returns: `int`: the position i so that arr[i] <= x < arr[i+1] Raises: `IndexError`: if the array is empty or if the query is outside the array values rr zInvalid query 'z ' for size ������none�.)�len� IndexError)rLrF�i�jrIs r�_interpolation_searchrUOs5�� �c�#�h�h��l�q�A� �a�%�%�C��F�a�(�(�(�(�#�a�&�(�(�(�(�(� �!�a�%�A��A��J�'�C��F�S��V�O�<� =�� �q�6�Q� #� #� #� #��Q��U�� #� #� #� #� #��H� ��V�a�Z�Z��q�5�!�q�A�A��a�q�A� �a�%�%�C��F�a�(�(�(�(�#�a�&�(�(�(�(� �W�q�W�W��C���5T�S��W�W�f�W�W�W� X� X�Xrc�|�eZdZdejfd�Zdeeee j fdejfd�Z d dejfd�Z dS) �IndexedTableMixinr1c���|j|_d�|���D��|_t jdgd�|jD��ztj���|_dS)Nc�8�g|]}t|��dk�|��S�r�rQ)�.0� recordbatchs r� <listcomp>z.IndexedTableMixin.__init__.<locals>.<listcomp>ls2��/ �/ �/ �'��[�AQ�AQ�TU�AU�AU�K�AU�AU�AUrrc�,�g|]}t|����Srr[)r\�bs rr^z.IndexedTableMixin.__init__.<locals>.<listcomp>os��4S�4S�4S��S��V�V�4S�4S�4Sr)�dtype)r8�_schema� to_batches�_batches�np�cumsum�int64�_offsets)�selfr1s r�__init__zIndexedTableMixin.__init__jso��"'�,�� �/ �/ �+0�+;�+;�+=�+=�/ �/ �/ �� �%'�I�q�c�4S�4S�T�]�4S�4S�4S�.S�[]�[c�$d�$d�$d�� � � r�indicesr!c����t|��std���tj�j|d���dz }t j��fd�t||��D���j ���S)a' Create a pa.Table by gathering the records at the records at the specified indices. Should be faster than pa.concat_tables(table.fast_slice(int(i) % table.num_rows, 1) for i in indices) since NumPy can compute the binary searches in parallel, highly optimized C zIndices must be non-empty�right)�sider c�t��g|]4\}}�j|�|�j|z d����5S)r )rd�slicerh)r\� batch_idxrSris �rr^z1IndexedTableMixin.fast_gather.<locals>.<listcomp>{sP��� � � � �I�q�� �i�(�.�.�q�4�=��3K�/K�Q�O�O� � � r�r8) rQ� ValueErrorre� searchsortedrhr$r� from_batches�ziprb)rirk� batch_indicess` r� fast_gatherzIndexedTableMixin.fast_gatherqs���� �7�|�|� :��8�9�9� 9���� �w�W�M�M�M�PQ�Q� ��x�$�$� � � � �$'� �w�$?�$?� � � ��<� %� � � rrNc���|dkrtd���||jdks|�,|dkr&tj�g|j���St |j|��}|�||z|jdkr<|j|d�}|d�||j|z ��|d<n�t |j||zdz ��}|j||dz�}|d�d||z|j|z ��|d<|d�||j|z ��|d<tj�||j���S)aJ Slice the Table using interpolation search. The behavior is the same as `pyarrow.Table.slice` but it's significantly faster. Interpolation search is used to find the start and end indexes of the batches we want to keep. The batches to keep are then concatenated to form the sliced Table. rzOffset must be non-negativerNNrrr ) rRrhr$rrurbrUrdrp)ri�offset�lengthrS�batchesrTs r� fast_slicezIndexedTableMixin.fast_slice�s[�� �A�:�:��:�;�;� ;� �t�}�R�(� (� (�V�-?�F�a�K�K��8�(�(��D�L�(�A�A� A� !�$�-�� 8� 8�� �>�V�f�_�� �b�0A�A�A��m�A�B�B�'�G� ���)�)�&�4�=��3C�*C�D�D�G�A�J�J�%�d�m�V�f�_�q�5H�I�I�A��m�A��A��I�.�G�!�"�+�+�+�A�v�����q�AQ�/Q�R�R�G�B�K� ���)�)�&�4�=��3C�*C�D�D�G�A�J��x�$�$�W�T�\�$�B�B�Br�rN) �__name__� __module__� __qualname__r$rrjr �list�intre�ndarrayrxr}rrrrWrWis�������e�b�h�e�e�e�e� �5��c��B�J�)>�#?� �B�H� � � � �"C�C�2�8�C�C�C�C�C�CrrWc���eZdZdZdejf�fd� Zdefd�Zd�Z d�Z d�Z d �Z d �Z d �Zd �Zd,deefd�Zd�Zd�Zd�Zed���Zed���Zed���Zed���Zed���Zed���Zed���Zd�Zd�Zd�Z d�Z!d�Z"d�Z#d �Z$d!�Z%d"�Z&d#�Z'd$�Z(d%�Z)d&�Z*d'�Z+d(�Z,d)�Z-d*�Z.d+�Z/�xZ0S)-ra� Wraps a pyarrow Table by using composition. This is the base class for `InMemoryTable`, `MemoryMappedTable` and `ConcatenationTable`. It implements all the basic attributes/methods of the pyarrow Table class except the Table transforms: `slice, filter, flatten, combine_chunks, cast, add_column, append_column, remove_column, set_column, rename_columns` and `drop`. The implementation of these methods differs for the subclasses. r1c�X��t���|��||_dSr#)�superrjr1)rir1r>s �rrjzTable.__init__�s&��� ����������� � � rr<c��|j|t|j��<t|j��|t|j��<t ||��Sr#)r1r@r�rdrK)rir<s r� __deepcopy__zTable.__deepcopy__�sF�� $�z��R�� �^�^��"&�t�}�"5�"5��R�� � � ����t�$�$�$rc�&�|jj|i|��S)a� Perform validation checks. An exception is raised if validation fails. By default only cheap validation checks are run. Pass `full=True` for thorough validation checks (potentially `O(n)`). Args: full (`bool`, defaults to `False`): If `True`, run expensive checks, otherwise cheap checks only. Raises: `pa.lib.ArrowInvalid`: if validation fails )r1�validate�ri�args�kwargss rr�zTable.validate�s��#�t�z�"�D�3�F�3�3�3rc�p�td�|D����}d�|D��}|jj|i|��S)aN Check if contents of two tables are equal. Args: other ([`~datasets.table.Table`]): Table to compare against. check_metadata `bool`, defaults to `False`): Whether schema metadata equality should be checked as well. Returns: `bool` c3�RK�|]"}t|t��r|jn|V��#dSr#�� isinstancerr1)r\�args r� <genexpr>zTable.equals.<locals>.<genexpr>�s7����R�R�c�*�S�%�"8�"8�A�S�Y�Y�c�R�R�R�R�R�Rrc�R�i|]$\}}|t|t��r|jn|��%Srr�)r\rIrJs r� <dictcomp>z Table.equals.<locals>.<dictcomp>�s3��O�O�O���1�!� �1�e� 4� 4�;�Q�W�W�!�O�O�Or)�tupler1�equalsr�s rr�z Table.equals�sM���R�R�T�R�R�R�R�R��O�O��O�O�O�� �t�z� �$�1�&�1�1�1rc�&�|jj|i|��S)ak Convert Table to list of (contiguous) `RecordBatch` objects. Args: max_chunksize (`int`, defaults to `None`): Maximum size for `RecordBatch` chunks. Individual chunks may be smaller depending on the chunk layout of individual columns. Returns: `List[pyarrow.RecordBatch]` )r1rcr�s rrczTable.to_batches�s��%�t�z�$�d�5�f�5�5�5rc�&�|jj|i|��S)zf Convert the Table to a `dict` or `OrderedDict`. Returns: `dict` )r1� to_pydictr�s rr�zTable.to_pydict����$�t�z�#�T�4�V�4�4�4rc�&�|jj|i|��S)zR Convert the Table to a list Returns: `list` )r1� to_pylistr�s rr�zTable.to_pylist�r�rc�&�|jj|i|��S)a� Convert to a pandas-compatible NumPy array or DataFrame, as appropriate. Args: memory_pool (`MemoryPool`, defaults to `None`): Arrow MemoryPool to use for allocations. Uses the default memory pool is not passed. strings_to_categorical (`bool`, defaults to `False`): Encode string (UTF8) and binary types to `pandas.Categorical`. categories (`list`, defaults to `empty`): List of fields that should be returned as `pandas.Categorical`. Only applies to table-like data structures. zero_copy_only (`bool`, defaults to `False`): Raise an `ArrowException` if this function call would require copying the underlying data. integer_object_nulls (`bool`, defaults to `False`): Cast integers with nulls to objects. date_as_object (`bool`, defaults to `True`): Cast dates to objects. If `False`, convert to `datetime64[ns]` dtype. timestamp_as_object (`bool`, defaults to `False`): Cast non-nanosecond timestamps (`np.datetime64`) to objects. This is useful if you have timestamps that don't fit in the normal date range of nanosecond timestamps (1678 CE-2262 CE). If `False`, all timestamps are converted to `datetime64[ns]` dtype. use_threads (`bool`, defaults to `True`): Whether to parallelize the conversion using multiple threads. deduplicate_objects (`bool`, defaults to `False`): Do not create multiple copies Python objects when created, to save on memory use. Conversion will be slower. ignore_metadata (`bool`, defaults to `False`): If `True`, do not use the 'pandas' metadata to reconstruct the DataFrame index, if present. safe (`bool`, defaults to `True`): For certain data types, a cast is needed in order to store the data in a pandas DataFrame or Series (e.g. timestamps are always stored as nanoseconds in pandas). This option controls whether it is a safe cast or not. split_blocks (`bool`, defaults to `False`): If `True`, generate one internal "block" for each column when creating a pandas.DataFrame from a `RecordBatch` or `Table`. While this can temporarily reduce memory note that various pandas operations can trigger "consolidation" which may balloon memory use. self_destruct (`bool`, defaults to `False`): EXPERIMENTAL: If `True`, attempt to deallocate the originating Arrow memory while converting the Arrow object to pandas. If you use the object after calling `to_pandas` with this option it will crash your program. types_mapper (`function`, defaults to `None`): A function mapping a pyarrow DataType to a pandas `ExtensionDtype`. This can be used to override the default pandas type for conversion of built-in pyarrow types or in absence of `pandas_metadata` in the Table schema. The function receives a pyarrow DataType and is expected to return a pandas `ExtensionDtype` or `None` if the default conversion should be used for that type. If you have a dictionary mapping, you can pass `dict.get` as function. Returns: `pandas.Series` or `pandas.DataFrame`: `pandas.Series` or `pandas.DataFrame` depending on type of object )r1� to_pandasr�s rr�zTable.to_pandas�s��x$�t�z�#�T�4�V�4�4�4rc�&�|jj|i|��Sr#)r1� to_stringr�s rr�zTable.to_string2s��#�t�z�#�T�4�V�4�4�4rN� max_chunksizec�8�|j�|���S)a� Convert the Table to a RecordBatchReader. Note that this method is zero-copy, it merely exposes the same data under a different API. Args: max_chunksize (`int`, defaults to `None`) Maximum size for RecordBatch chunks. Individual chunks may be smaller depending on the chunk layout of individual columns. Returns: `pyarrow.RecordBatchReader` �r�)r1� to_reader)rir�s rr�zTable.to_reader5s���z�#�#�-�#�@�@�@rc�&�|jj|i|��S)z� Select a schema field by its column name or numeric index. Args: i (`Union[int, str]`): The index or name of the field to retrieve. Returns: `pyarrow.Field` )r1�fieldr�s rr�z Table.fieldEs�� �t�z���0��0�0�0rc�&�|jj|i|��S)z� Select a column by its column name, or numeric index. Args: i (`Union[int, str]`): The index or name of the column to retrieve. Returns: `pyarrow.ChunkedArray` )r1�columnr�s rr�z Table.columnRs��!�t�z� �$�1�&�1�1�1rc�&�|jj|i|��S)zy Iterator over all columns in their numerical order. Yields: `pyarrow.ChunkedArray` )r1� itercolumnsr�s rr�zTable.itercolumns_s��&�t�z�%�t�6�v�6�6�6rc��|jjS)ze Schema of the table and its columns. Returns: `pyarrow.Schema` �r1r8�ris rr8z Table.schemahs���z� � rc��|jjS)zo List of all columns in numerical order. Returns: `List[pa.ChunkedArray]` )r1�columnsr�s rr�z Table.columnsrs���z�!�!rc��|jjS)zT Number of columns in this table. Returns: int )r1� num_columnsr�s rr�zTable.num_columns|s���z�%�%rc��|jjS)z� Number of rows in this table. Due to the definition of a table, all columns have the same number of rows. Returns: int )r1�num_rowsr�s rr�zTable.num_rows�s���z�"�"rc��|jjS)z� Dimensions of the table: (#rows, #columns). Returns: `(int, int)`: Number of rows and number of columns. )r1�shaper�s rr�z Table.shape�s���z��rc��|jjS)zN Total number of bytes consumed by the elements of the table. )r1�nbytesr�s rr�z Table.nbytes�s�� �z� � rc��|jjS)z/ Names of the table's columns. )r1� column_namesr�s rr�zTable.column_names�s�� �z�&�&rc�,�|�|��Sr#)r�)ri�others r�__eq__z Table.__eq__�s���{�{�5�!�!�!rc��|j|Sr#�r1)rirSs r� __getitem__zTable.__getitem__�s���z�!�}�rc�*�t|j��Sr#)rQr1r�s r�__len__z Table.__len__�s���4�:���rc�p�|j����d|jj��S�Nr)r1�__repr__rr>rr�s rr�zTable.__repr__�s+���z�"�"�$�$�,�,�_�d�n�>U�V�V�Vrc�p�|j����d|jj��Sr�)r1�__str__rr>rr�s rr�z Table.__str__�s+���z�!�!�#�#�+�+�O�T�^�=T�U�U�Urc��t���)�t Compute zero-copy slice of this Table. Args: offset (`int`, defaults to `0`): Offset from start of table to slice. length (`int`, defaults to `None`): Length of slice (default is until end of table starting from offset). Returns: `datasets.table.Table` ��NotImplementedErrorr�s rrpz Table.slice����"�#�#�#rc��t�����[ Select records from a Table. See `pyarrow.compute.filter` for full usage. r�r�s r�filterz Table.filter����"�#�#�#rc��t�����p Flatten this Table. Each column with a struct type is flattened into one column per struct field. Other columns are left unchanged. Args: memory_pool (`MemoryPool`, defaults to `None`): For memory allocations, if required, otherwise use default pool. Returns: `datasets.table.Table` r�r�s r�flattenz Table.flatten����"�#�#�#rc��t����� Make a new table by combining the chunks this table has. All the underlying chunks in the `ChunkedArray` of each column are concatenated into zero or one chunk. Args: memory_pool (`MemoryPool`, defaults to `None`): For memory allocations, if required, otherwise use default pool. Returns: `datasets.table.Table` r�r�s r�combine_chunkszTable.combine_chunks�r�rc��t�����] Cast table values to another schema. Args: target_schema (`Schema`): Schema to cast to, the names and order of fields must match. safe (`bool`, defaults to `True`): Check for overflows or other unsafe conversions. Returns: `datasets.table.Table` r�r�s r�castz Table.cast����"�#�#�#rc��t���)aP EXPERIMENTAL: Create shallow copy of table by replacing schema key-value metadata with the indicated new metadata (which may be None, which deletes any existing metadata Args: metadata (`dict`, defaults to `None`): Returns: `datasets.table.Table`: shallow_copy r�r�s r�replace_schema_metadatazTable.replace_schema_metadata�r�rc��t�����G Add column to Table at position. A new table is returned with the column added, the original table object is left unchanged. Args: i (`int`): Index to place the column at. field_ (`Union[str, pyarrow.Field]`): If a string is passed then the type is deduced from the column data. column (`Union[pyarrow.Array, List[pyarrow.Array]]`): Column data. Returns: `datasets.table.Table`: New table with the passed column added. r�r�s r� add_columnzTable.add_column ���&"�#�#�#rc��t���)a� Append column at end of columns. Args: field_ (`Union[str, pyarrow.Field]`): If a string is passed then the type is deduced from the column data. column (`Union[pyarrow.Array, List[pyarrow.Array]]`): Column data. Returns: `datasets.table.Table`: New table with the passed column added. r�r�s r� append_columnzTable.append_column r�rc��t���)z� Create new Table with the indicated column removed. Args: i (`int`): Index of column to remove. Returns: `datasets.table.Table`: New table without the column. r�r�s r� remove_columnzTable.remove_column0s��"�#�#�#rc��t���)a� Replace column in Table at position. Args: i (`int`): Index to place the column at. field_ (`Union[str, pyarrow.Field]`): If a string is passed then the type is deduced from the column data. column (`Union[pyarrow.Array, List[pyarrow.Array]]`): Column data. Returns: `datasets.table.Table`: New table with the passed column set. r�r�s r� set_columnzTable.set_column=s�� "�#�#�#rc��t�����J Create new table with columns renamed to provided names. r�r�s r�rename_columnszTable.rename_columnsOr�rc��t���)ai Drop one or more columns and return a new table. Args: columns (`List[str]`): List of field names referencing existing columns. Raises: `KeyError` : if any of the passed columns name are not existing. Returns: `datasets.table.Table`: New table without the columns. r�r�s r�dropz Table.dropUr�rc��t���)ae Select columns of the table. Returns a new table with the specified columns, and metadata preserved. Args: columns (:obj:`Union[List[str], List[int]]`): The column names or integer indices to select. Returns: `datasets.table.Table`: table with only a subset of the columns r�r�s r�selectz Table.selecter�rr#)1rr�r�rr$rrj�dictr�r�r�rcr�r�r�r�r r�r�r�r�r��propertyr8r�r�r�r�r�r�r�r�r�r�r�rpr�r�r�r�r�r�r�r�r�r�r�r�� __classcell__�r>s@rrr�s�������� � ��b�h�������%��%�%�%�%�4�4�4� 2�2�2�" 6� 6� 6�5�5�5�5�5�5�<5�<5�<5�|5�5�5�A�A�x��}�A�A�A�A� 1� 1� 1� 2� 2� 2�7�7�7��!�!��X�!��"�"��X�"��&�&��X�&�� #� #��X� #�� � ��X� ��!�!��X�!� �'�'��X�'� "�"�"�������W�W�W�V�V�V�$�$�$� $�$�$� $� $� $�$�$�$� $� $� $� $� $� $�$�$�$�*$�$�$� $� $� $�$�$�$�$$�$�$� $�$�$� $� $� $� $� $� $� $rrc��eZdZdZdS)� TableBlockz� `TableBlock` is the allowed class inside a `ConcanetationTable`. Only `MemoryMappedTable` and `InMemoryTable` are `TableBlock`. This is because we don't want a `ConcanetationTable` made out of other `ConcanetationTables`. N)rr�r�rrrrr�r�us��������  �Drr�c��eZdZdZedefd���Zedejfd���Z ed���Z ed���Z ed���Z ed ���Z ed ���Zdd �Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd S)� InMemoryTablea_ The table is said in-memory when it is loaded into the user's RAM. Pickling it does copy all the data using memory. Its implementation is simple and uses the underlying pyarrow Table methods directly. This is different from the `MemoryMapped` table, for which pickling doesn't copy all the data in memory. For a `MemoryMapped`, unpickling instead reloads the table from the disk. `InMemoryTable` must be used when data fit in memory, while `MemoryMapped` are reserved for data bigger than memory or when you want the memory footprint of your application to stay low. r c�6�t|��}||��Sr#)r,)rGr r1s r� from_filezInMemoryTable.from_file�s��0��:�:���s�5�z�z�rr-c�6�t|��}||��Sr#)r2)rGr-r1s r� from_bufferzInMemoryTable.from_buffer�s��2�6�:�:���s�5�z�z�rc�@�|tjj|i|����S)aG Convert pandas.DataFrame to an Arrow Table. The column types in the resulting Arrow Table are inferred from the dtypes of the pandas.Series in the DataFrame. In the case of non-object Series, the NumPy dtype is translated to its Arrow equivalent. In the case of `object`, we need to guess the datatype by looking at the Python objects in this Series. Be aware that Series of the `object` dtype don't carry enough information to always lead to a meaningful Arrow type. In the case that we cannot infer a type, e.g. because the DataFrame is of length 0 or the Series only contains `None/nan` objects, the type is set to null. This behavior can be avoided by constructing an explicit schema and passing it to this function. Args: df (`pandas.DataFrame`): schema (`pyarrow.Schema`, *optional*): The expected schema of the Arrow Table. This can be used to indicate the type of columns if we cannot infer it automatically. If passed, the output will have exactly this schema. Columns specified in the schema that are not found in the DataFrame columns or its index will raise an error. Additional columns or index levels in the DataFrame which are not specified in the schema will be ignored. preserve_index (`bool`, *optional*): Whether to store the index as an additional column in the resulting `Table`. The default of None will store the index as a column, except for RangeIndex which is stored as metadata only. Use `preserve_index=True` to force it to be stored as a column. nthreads (`int`, defaults to `None` (may use up to system CPU count threads)) If greater than 1, convert columns to Arrow in parallel using indicated number of threads. columns (`List[str]`, *optional*): List of column to be converted. If `None`, use all columns. safe (`bool`, defaults to `True`): Check for overflows or other unsafe conversions, Returns: `datasets.table.Table`: Examples: ```python >>> import pandas as pd >>> import pyarrow as pa >>> df = pd.DataFrame({ ... 'int': [1, 2], ... 'str': ['a', 'b'] ... }) >>> pa.Table.from_pandas(df) <pyarrow.lib.Table object at 0x7f05d1fb1b40> ``` )r$r� from_pandas�rGr�r�s rrzInMemoryTable.from_pandas�s'��p�s�2�8�'��8��8�8�9�9�9rc�@�|tjj|i|����S)a� Construct a Table from Arrow arrays. Args: arrays (`List[Union[pyarrow.Array, pyarrow.ChunkedArray]]`): Equal-length arrays that should form the table. names (`List[str]`, *optional*): Names for the table columns. If not passed, schema must be passed. schema (`Schema`, defaults to `None`): Schema for the created table. If not passed, names must be passed. metadata (`Union[dict, Mapping]`, defaults to `None`): Optional metadata for the schema (if inferred). Returns: `datasets.table.Table` )r$r� from_arraysrs rrzInMemoryTable.from_arrays�s&��$�s�2�8�'��8��8�8�9�9�9rc�@�|tjj|i|����S)a� Construct a Table from Arrow arrays or columns. Args: mapping (`Union[dict, Mapping]`): A mapping of strings to Arrays or Python lists. schema (`Schema`, defaults to `None`): If not passed, will be inferred from the Mapping values metadata (`Union[dict, Mapping]`, defaults to `None`): Optional metadata for the schema (if inferred). Returns: `datasets.table.Table` )r$r� from_pydictrs rrzInMemoryTable.from_pydict�s&�� �s�2�8�'��8��8�8�9�9�9rc�H�|tjj|g|�Ri|����S)a� Construct a Table from list of rows / dictionaries. Args: mapping (`List[dict]`): A mapping of strings to row values. schema (`Schema`, defaults to `None`): If not passed, will be inferred from the Mapping values metadata (`Union[dict, Mapping]`, defaults to `None`): Optional metadata for the schema (if inferred). Returns: `datasets.table.Table` )r$r� from_pylist)rG�mappingr�r�s rr zInMemoryTable.from_pylist�s1�� �s�2�8�'��A�$�A�A�A�&�A�A�B�B�Brc�@�|tjj|i|����S)a� Construct a Table from a sequence or iterator of Arrow `RecordBatches`. Args: batches (`Union[Sequence[pyarrow.RecordBatch], Iterator[pyarrow.RecordBatch]]`): Sequence of `RecordBatch` to be converted, all schemas must be equal. schema (`Schema`, defaults to `None`): If not passed, will be inferred from the first `RecordBatch`. Returns: `datasets.table.Table`: )r$rrurs rruzInMemoryTable.from_batches s&���s�2�8�(�$�9�&�9�9�:�:�:rrNc�J�t|�||�����S)r��rzr{)r�r})rirzr{s rrpzInMemoryTable.slices"���T�_�_�F�6�_�J�J�K�K�Krc�@�t|jj|i|����Sr�)r�r1r�r�s rr�zInMemoryTable.filter+s'���.�T�Z�.��?��?�?�@�@�@rc�F�tt|jg|�Ri|����Sr�)r�� table_flattenr1r�s rr�zInMemoryTable.flatten1s,���]�4�:�G��G�G�G��G�G�H�H�Hrc�@�t|jj|i|����Sr�)r�r1r�r�s rr�zInMemoryTable.combine_chunks?s'���6�T�Z�6��G��G�G�H�H�Hrc�F�tt|jg|�Ri|����Sr�)r�� table_castr1r�s rr�zInMemoryTable.castOs,���Z�� �D�T�D�D�D�V�D�D�E�E�Erc�@�t|jj|i|����S)�T EXPERIMENTAL: Create shallow copy of table by replacing schema key-value metadata with the indicated new metadata (which may be `None`, which deletes any existing metadata). Args: metadata (`dict`, defaults to `None`): Returns: `datasets.table.Table`: shallow_copy )r�r1r�r�s rr�z%InMemoryTable.replace_schema_metadata^s'���?�T�Z�?��P��P�P�Q�Q�Qrc�@�t|jj|i|����Sr�)r�r1r�r�s rr�zInMemoryTable.add_columnls'��&�2�T�Z�2�D�C�F�C�C�D�D�Drc�@�t|jj|i|����S�� Append column at end of columns. Args: field_ (`Union[str, pyarrow.Field]`): If a string is passed then the type is deduced from the column data. column (`Union[pyarrow.Array, List[pyarrow.Array]]`): Column data. Returns: `datasets.table.Table`: New table with the passed column added. )r�r1r�r�s rr�zInMemoryTable.append_column�s'���5�T�Z�5�t�F�v�F�F�G�G�Grc�@�t|jj|i|����S)�� Create new Table with the indicated column removed. Args: i (`int`): Index of column to remove. Returns: `datasets.table.Table`: New table without the column. )r�r1r�r�s rr�zInMemoryTable.remove_column�s'���5�T�Z�5�t�F�v�F�F�G�G�Grc�@�t|jj|i|����S��� Replace column in Table at position. Args: i (`int`): Index to place the column at. field_ (`Union[str, pyarrow.Field]`): If a string is passed then the type is deduced from the column data. column (`Union[pyarrow.Array, List[pyarrow.Array]]`): Column data. Returns: `datasets.table.Table`: New table with the passed column set. )r�r1r�r�s rr�zInMemoryTable.set_column�s'��"�2�T�Z�2�D�C�F�C�C�D�D�Drc�@�t|jj|i|����Sr�)r�r1r�r�s rr�zInMemoryTable.rename_columns�s'���6�T�Z�6��G��G�G�H�H�Hrc�@�t|jj|i|����S)�y Drop one or more columns and return a new table. Args: columns (`List[str]`): List of field names referencing existing columns. Raises: `KeyError` : if any of the passed columns name are not existing. Returns: `datasets.table.Table`: New table without the columns. )r�r1r�r�s rr�zInMemoryTable.drop�s%���_�T�Z�_�d�=�f�=�=�>�>�>rc�@�t|jj|i|����S)� Select columns of the table. Returns a new table with the specified columns, and metadata preserved. Args: columns (:obj:`Union[List[str], List[int]]`): The column names or integer indices to select. Returns: :class:`datasets.table.Table`: New table with the specified columns, and metadata preserved. )r�r1r�r�s rr�zInMemoryTable.select�s'���.�T�Z�.��?��?�?�@�@�@rr~)rr�r�r� classmethod�strr�r$�Bufferrrrrr rurpr�r�r�r�r�r�r�r�r�r�r�r�rrrr�r�s������� � ��������[����������[���7:�7:��[�7:�r�:�:��[�:�&�:�:��[�:�"�C�C��[�C�"� ;� ;��[� ;�L�L�L�L�"A�A�A� I� I� I�I�I�I� F� F� F� R� R� R�E�E�E�*H�H�H�" H� H� H�E�E�E�&I�I�I� ?�?�?�" A� A� A� A� Arr�c �<��eZdZdZddejdedeee f�fd� Z e ddefd���Z d �Z d �Zeddejdeee d ejfd ���Zd e d ee fd�Zdd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z�xZS)�MemoryMappedTableaT The table is said memory mapped when it doesn't use the user's RAM but loads the data from the disk instead. Pickling it doesn't copy the data into memory. Instead, only the path to the memory mapped arrow file is pickled, as well as the list of transforms to "replay" when reloading the table from the disk. Its implementation requires to store an history of all the transforms that were applied to the underlying pyarrow Table, so that they can be "replayed" when reloading the Table from the disk. This is different from the `InMemoryTable` table, for which pickling does copy all the data in memory. `InMemoryTable` must be used when data fit in memory, while `MemoryMapped` are reserved for data bigger than memory or when you want the memory footprint of your application to stay low. Nr1�path�replaysc���t���|��tj�|��|_|�|ng|_dSr#)r�rj�osr*�abspathr+)rir1r*r+r>s �rrjzMemoryMappedTable.__init__�sD��� ����������G�O�O�D�)�)�� �07�0C�W�W��� � � rr c�f�t|��}|�||��}||||��Sr#)r;�_apply_replays)rGr r+r1s rr�zMemoryMappedTable.from_file�s8��4�X�>�>���"�"�5�'�2�2���s�5�(�G�,�,�,rc� �|j|jd�S)N�r*r+r2r�s r� __getstate__zMemoryMappedTable.__getstate__�s��� �d�l�;�;�;rc��|d}|d}t|��}|�||��}t�||||���dS)Nr*r+r2)r;r0r)rj)ri�stater*r+r1s r� __setstate__zMemoryMappedTable.__setstate__sZ���V�}��� �"��4�T�:�:���#�#�E�7�3�3���"�"�4��T�7�"�K�K�K�K�Krr!c��|�M|D]J\}}}|dkrt|g|�Ri|��}�|dkrt|g|�Ri|��}�4t||��|i|��}�K|S)Nr�r�)rr�getattr)r1r+�namer�r�s rr0z MemoryMappedTable._apply_replayss��� � �&-� B� B�"��d�F��6�>�>�&�u�>�t�>�>�>�v�>�>�E�E��Y�&�&�)�%�A�$�A�A�A�&�A�A�E�E�0�G�E�4�0�0�$�A�&�A�A�E�E�� r�replayc�b�tj|j��}|�|��|Sr#)rDrEr+�append)rir:r+s r�_append_replayz MemoryMappedTable._append_replays*���-�� �-�-�����v�����rrc��d||fif}|�|��}t|�||���|j|��S)r�rpr)r=r)r}r*)rirzr{r:r+s rrpzMemoryMappedTable.slicesO���F�F�+�R�0���%�%�f�-�-�� �����v��!N�!N�PT�PY�[b�c�c�crc���dtj|��tj|��f}|�|��}t|jj|i|��|j|��S)r�r�)rDrEr=r)r1r�r*�rir�r�r:r+s rr�zMemoryMappedTable.filter,sa���D�M�$�/�/���v�1F�1F�G���%�%�f�-�-�� �!2���!2�D�!C�F�!C�!C�T�Y�PW�X�X�Xrc���dtj|��tj|��f}|�|��}tt |jg|�Ri|��|j|��S)r�r�)rDrEr=r)rr1r*r@s rr�zMemoryMappedTable.flatten4sf���T�]�4�0�0�$�-��2G�2G�H���%�%�f�-�-�� ��t�z�!K�D�!K�!K�!K�F�!K�!K�T�Y�X_�`�`�`rc���dtj|��tj|��f}|�|��}t|jj|i|��|j|��S)a� Make a new table by combining the chunks this table has. All the underlying chunks in the ChunkedArray of each column are concatenated into zero or one chunk. Args: memory_pool (`MemoryPool`, defaults to `None`): For memory allocations, if required, otherwise use default pool. Returns: `datasets.table.Table` r�)rDrEr=r)r1r�r*r@s rr�z MemoryMappedTable.combine_chunksDsa��#�D�M�$�$7�$7���v�9N�9N�O���%�%�f�-�-�� �!:���!:�D�!K�F�!K�!K�T�Y�X_�`�`�`rc���dtj|��tj|��f}|�|��}tt |jg|�Ri|��|j|��S)a\ Cast table values to another schema Args: target_schema (`Schema`): Schema to cast to, the names and order of fields must match. safe (`bool`, defaults to `True`): Check for overflows or other unsafe conversions. Returns: `datasets.table.Table` r�)rDrEr=r)rr1r*r@s rr�zMemoryMappedTable.castVsf���$�-��-�-�t�}�V�/D�/D�E���%�%�f�-�-�� ��D�J�!H��!H�!H�!H��!H�!H�$�)�U\�]�]�]rc���dtj|��tj|��f}|�|��}t|jj|i|��|j|��S)aQ EXPERIMENTAL: Create shallow copy of table by replacing schema key-value metadata with the indicated new metadata (which may be None, which deletes any existing metadata. Args: metadata (`dict`, defaults to `None`): Returns: `datasets.table.Table`: shallow_copy r�)rDrEr=r)r1r�r*r@s rr�z)MemoryMappedTable.replace_schema_metadatagsd��,�T�]�4�-@�-@�$�-�PV�BW�BW�X���%�%�f�-�-�� �!C���!C�T�!T�V�!T�!T�VZ�V_�ah�i�i�irc���dtj|��tj|��f}|�|��}t|jj|i|��|j|��S)r�r�)rDrEr=r)r1r�r*r@s rr�zMemoryMappedTable.add_columnwsa��&�� �d� 3� 3�T�]�6�5J�5J�K���%�%�f�-�-�� �!6���!6��!G��!G�!G���T[�\�\�\rc���dtj|��tj|��f}|�|��}t|jj|i|��|j|��S)rr�)rDrEr=r)r1r�r*r@s rr�zMemoryMappedTable.append_column�sa��"�4�=��#6�#6�� �f�8M�8M�N���%�%�f�-�-�� �!9���!9�4�!J�6�!J�!J�D�I�W^�_�_�_rc���dtj|��tj|��f}|�|��}t|jj|i|��|j|��S)rr�)rDrEr=r)r1r�r*r@s rr�zMemoryMappedTable.remove_column�sa��"�4�=��#6�#6�� �f�8M�8M�N���%�%�f�-�-�� �!9���!9�4�!J�6�!J�!J�D�I�W^�_�_�_rc���dtj|��tj|��f}|�|��}t|jj|i|��|j|��S)rr�)rDrEr=r)r1r�r*r@s rr�zMemoryMappedTable.set_column�sa��"�� �d� 3� 3�T�]�6�5J�5J�K���%�%�f�-�-�� �!6���!6��!G��!G�!G���T[�\�\�\rc���dtj|��tj|��f}|�|��}t|jj|i|��|j|��S)r�r�)rDrEr=r)r1r�r*r@s rr�z MemoryMappedTable.rename_columns�sa��#�D�M�$�$7�$7���v�9N�9N�O���%�%�f�-�-�� �!:���!:�D�!K�F�!K�!K�T�Y�X_�`�`�`rc���dtj|��tj|��f}|�|��}t|jj|i|��|j|��S)r"r�)rDrEr=r)r1r�r*r@s rr�zMemoryMappedTable.drop�s^���$�-��-�-�t�}�V�/D�/D�E���%�%�f�-�-�� �����$�!A�&�!A�!A�4�9�g�V�V�Vrc���dtj|��tj|��f}|�|��}t|jj|i|��|j|��S)r$r�)rDrEr=r)r1r�r*r@s rr�zMemoryMappedTable.select�sa���D�M�$�/�/���v�1F�1F�G���%�%�f�-�-�� �!2���!2�D�!C�F�!C�!C�T�Y�PW�X�X�Xrr#r~) rr�r�rr$rr&r r��Replayrjr%r�r3r6� staticmethodr0r=rpr�r�r�r�r�r�r�r�r�r�r�r�r�r�s@rr)r)�s&���������(L�L�b�h�L�c�L�H�T�&�\�<R�L�L�L�L�L�L� �-�-��-�-�-��[�-� <�<�<�L�L�L�� � �b�h� ��$�v�,�1G� �SU�S[� � � ��\� ��V���V� ����� d�d�d�d�&Y�Y�Y�a�a�a� a�a�a�$^�^�^�"j�j�j� ]�]�]�.`�`�`�&`�`�`� ]�]�]�*a�a�a�W�W�W�&Y�Y�Y�Y�Y�Y�Yrr)�TableBlockContainerc���eZdZdZdejdeeef�fd� Zd�Z d�Z e d dee eejfde d ejfd ���Zedeeed ejfd ���Zed!dedee d efd ���Zeded efd���Zeded dfd���Zed dee ejefde d dfd���Zed���Zd"d�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z d�Z!d�Z"d�Z#d�Z$d�Z%�xZ&S)#�ConcatenationTableay The table comes from the concatenation of several tables called blocks. It enables concatenation on both axis 0 (append rows) and axis 1 (append columns). The underlying tables are called "blocks" and can be either `InMemoryTable` or `MemoryMappedTable` objects. This allows to combine tables that come from memory or that are memory mapped. When a `ConcatenationTable` is pickled, then each block is pickled: - the `InMemoryTable` objects are pickled by copying all the data in memory. - the MemoryMappedTable objects are pickled without copying the data into memory. Instead, only the path to the memory mapped arrow file is pickled, as well as the list of transforms to "replays" when reloading the table from the disk. Its implementation requires to store each block separately. The `blocks` attributes stores a list of list of blocks. The first axis concatenates the tables along the axis 0 (it appends rows), while the second axis concatenates tables along the axis 1 (it appends columns). If some columns are missing when concatenating on axis 0, they are filled with null values. This is done using `pyarrow.concat_tables(tables, promote=True)`. You can access the fully combined table by accessing the `ConcatenationTable.table` attribute, and the blocks by accessing the `ConcatenationTable.blocks` attribute. r1�blocksc����t���|��||_|D]<}|D]7}t|t��s t dt |���d�����8�=dS)Nz_The blocks of a ConcatenationTable must be InMemoryTable or MemoryMappedTable objects, but got rP)r�rjrQr�r�� TypeError� _short_str)rir1rQ� subtables�subtabler>s �rrjzConcatenationTable.__init__s���� ����������� � � � �I�%� � ��!�(�J�7�7��#�=�%/��%9�%9�=�=�=����� � � rc�*�|j|jjd�S)N)rQr8)rQr1r8r�s rr3zConcatenationTable.__getstate__!s���+���1B�C�C�Crc��|d}|d}|�|��}|�D|j|kr9tj�g|���}tj||gd���}t �|||���dS)NrQr8rr�default��promote_options)rQ)�*_concat_blocks_horizontally_and_verticallyr8r$rru� concat_tablesrPrj)rir5rQr8r1� empty_tables rr6zConcatenationTable.__setstate__$s����x����x����?�?��G�G�� � �%�,�&�"8�"8��(�/�/��6�/�B�B�K��$�e�[�%9�9�U�U�U�E��#�#�D�%��#�?�?�?�?�?rr�axisr!c�(�d�|D��}|dkrtj|d���S|dkrVt|��D]D\}}|dkr|}�t|j|j��D]\}}|�||��}��E|Std���)Nc�@�g|]}t|d��r|jn|��Sr�)rr1)r\r1s rr^z5ConcatenationTable._concat_blocks.<locals>.<listcomp>1s-��[�[�[�5�G�E�7�$;�$;�F�U�[�[��[�[�[rrrYrZr z'axis' must be either 0 or 1)r$r]� enumeratervr�r�r�rs)rQr_� pa_tablesrSr1r+r9�cols r�_concat_blocksz!ConcatenationTable._concat_blocks/s���[�[�TZ�[�[�[� � �1�9�9��#�I�y�I�I�I� I� �Q�Y�Y�%�i�0�0� E� E���5���6�6�$�H�H�%(��);�U�]�%K�%K�E�E� ��c�#+�#9�#9�$��#D�#D���E��O��;�<�<� <rc��g}t|��D]4\}}|s�|�|d���}|�|���5|�|d���S)Nr �r_r)rbrer<)rGrQ�pa_tables_to_concat_verticallyrS�tables�"pa_table_horizontally_concatenateds rr\z=ConcatenationTable._concat_blocks_horizontally_and_vertically@s|��)+�&�"�6�*�*� V� V�I�A�v�� ��14�1C�1C�F�QR�1C�1S�1S� .� *� 1� 1�2T� U� U� U� U��!�!�"@�q�!�I�I�IrNc�^��|�ag}t|d����D]K\}}|r2t��t|��|�����g}|t|��z }�LnH�fd�|D��}t d�|D����r!��d�|D��d���}|S)Nc�,�t|t��Sr#)r�r�)rFs r�<lambda>z2ConcatenationTable._merge_blocks.<locals>.<lambda>Ns��:�VW�Yf�Kg�Kg�r)�keyrgc�>��g|]}��|d�����S)r rg)� _merge_blocks)r\� row_blockrGs �rr^z4ConcatenationTable._merge_blocks.<locals>.<listcomp>Ss,���Z�Z�Z�i�S�.�.�y�q�.�A�A�Z�Z�Zrc3�<K�|]}t|��dkV��dS)r Nr[)r\rqs rr�z3ConcatenationTable._merge_blocks.<locals>.<genexpr>Ts-����F�F�9�3�y�>�>�Q�&�F�F�F�F�F�Frc��g|] }|D]}|��� Srr)r\rq�blocks rr^z4ConcatenationTable._merge_blocks.<locals>.<listcomp>Vs%��Q�Q�Q�y�y�Q�Q�e�U�Q�Q�Q�Qrr)rr�rer��allrp)rGrQr_� merged_blocks� is_in_memory� block_groups` rrpz ConcatenationTable._merge_blocksJs���� � ��M�-4�V�Ag�Ag�-h�-h�-h� 3� 3�)� �k��d�#0��1C�1C�D��DU�DU�\`�1C�1a�1a�#b�#b�"c�K���k�!2�!2�2� � � 3� [�Z�Z�Z�SY�Z�Z�Z�M��F�F� �F�F�F�F�F� � #� 1� 1�Q�Q�M�Q�Q�Q�XY�!2�!�!� ��rc��t|t��r|St|dt��r|�|d���S|�|��S)Nrrg)r�r�rp)rGrQs r�_consolidate_blocksz&ConcatenationTable._consolidate_blocksZs\�� �f�j� )� )� -��M� ��q� �:� .� .� -��$�$�V�!�$�4�4� 4��$�$�V�,�,� ,rc�V�|�|��}t|t��r|}||j|gg��St|dt��r/|�|d���}d�|D��}|||��S|�|��}|||��S)Nrrgc��g|]}|g��Srr)r\�ts rr^z2ConcatenationTable.from_blocks.<locals>.<listcomp>ks��*�*�*�a�q�c�*�*�*r)rzr�r�r1rer\)rGrQr1s r� from_blockszConcatenationTable.from_blockscs����(�(��0�0�� �f�j� )� )� &��E��3�u�{�e�W�I�.�.� .� ��q� �:� .� .� &��&�&�v�A�&�6�6�E�*�*�6�*�*�*�F��3�u�f�%�%� %��B�B�6�J�J�E��3�u�f�%�%� %rric ��� �dttjtfdtttfd�}dttdt dt ttttffd��dtttdtttdt ttttttff�fd � � ddtttdtttd t dtttf� fd � }||d ��}|d d�D]}||��}||||���}�|�|��S)a�Create `ConcatenationTable` from list of tables. Args: tables (list of `Table` or list of `pyarrow.Table`): List of tables. axis (`{0, 1}`, defaults to `0`, meaning over rows): Axis to concatenate over, where `0` means over rows (vertically) and `1` means over columns (horizontally). <Added version="1.6.0"/> r1r!c��t|tj��rt|��ggSt|t��rt j|j��S|ggSr#)r�r$rr�rPrDrErQr�s r� to_blocksz1ConcatenationTable.from_tables.<locals>.to_blockssY���%���*�*� !�&�u�-�-�.�/�/��E�#5�6�6� !��}�U�\�2�2�2���y� rrqr{c�H����fd��D��}��fd��D��}||fS)Nc�<��g|]}|�d�����SrZ�rp)r\r1r{s �rr^zLConcatenationTable.from_tables.<locals>._slice_row_block.<locals>.<listcomp>�s'���D�D�D��e�k�k�!�V�,�,�D�D�Drc �h��g|].}|��t�d���z ����/SrZ)rprQ)r\r1r{rqs ��rr^zLConcatenationTable.from_tables.<locals>._slice_row_block.<locals>.<listcomp>�s8���`�`�`�U����V�S��1��->�->��-G�H�H�`�`�`rr)rqr{�sliced� remainders`` r�_slice_row_blockz8ConcatenationTable.from_tables.<locals>._slice_row_block�sE����D�D�D�D�)�D�D�D�F�`�`�`�`�`�V_�`�`�`�I��9�$� $rrHrQc����t|��t|��}}gg}}|�r�|�r�t|dd��t|dd��kro|�|d���|dt|�d��d����\}|d<|�|��n�t|dd��t|dd��kro|�|d���|dt|�d��d����\}|d<|�|��nP|�|�d����|�|�d����|r|���|s|rt d���||fS)ah Make sure each row_block contain the same num_rows to be able to concatenate them on axis=1. To do so, we modify both blocks sets to have the same row_blocks boundaries. For example, if `result` has 2 row_blocks of 3 rows and `blocks` has 3 row_blocks of 2 rows, we modify both to have 4 row_blocks of size 2, 1, 1 and 2: [ x x x | x x x ] + [ y y | y y | y y ] ----------------------------- = [ x x | x | x | x x ] [ y y | y | y | y y ] rzQFailed to concatenate on axis=1 because tables don't have the same number of rows)r�rQr<�poprs)rHrQ� new_result� new_blocksr�r�s �r�_split_both_likez8ConcatenationTable.from_tables.<locals>._split_both_like�s����""�&�\�\�4��<�<�F�F�%'�� �J�� 5�V� 5��v�a�y��|�$�$�s�6�!�9�Q�<�'8�'8�8�8��%�%�f�Q�i�0�0�0�(8�(8����C�� � �ST� � �VW�HX�DY�DY�(Z�(Z�%�F�F�1�I��%�%�f�-�-�-�-�����1��&�&��V�A�Y�q�\�):�):�:�:��%�%�f�Q�i�0�0�0�(8�(8����C�� � �ST� � �VW�HX�DY�DY�(Z�(Z�%�F�F�1�I��%�%�f�-�-�-�-��%�%�f�j�j��m�m�4�4�4��%�%�f�j�j��m�m�4�4�4�� 5�V� 5�� v�� v� �!t�u�u�u��z�)� )rrr_c����|dkr|�|��nE|dkr?�||��\}}t|��D] \}}||�|���!|S)Nrr )�extendrb)rHrQr_rSrqr�s �r�_extend_blocksz6ConcatenationTable.from_tables.<locals>._extend_blocks�s}����q�y�y�� � �f�%�%�%�%�����!1�!1�&�&�!A�!A����$-�f�$5�$5�0�0�L�A�y��1�I�$�$�Y�/�/�/�/��Mrr NrgrZ)r r$rr�r�r�r�r~) rGrir_r�r�rQr1� table_blocksr�r�s @@r� from_tableszConcatenationTable.from_tablesqs����� !�U�2�8�U�?�3� !��T�*�=M�8N� !� !� !� !� %��Z�(8� %�#� %�%�PT�U_�P`�bf�gq�br�Pr�Js� %� %� %� %� # *���j�)�*�# *�48��j�9I�4J�# *� �4��Z�(�)�4��Z�0@�+A�A� B�# *�# *�# *�# *�# *�# *�LYZ� � ���j�)�*� �48��j�9I�4J� �RU� � �$�z�"� #� � � � � � ���6�!�9�%�%���A�B�B�Z� E� E�E�$�9�U�+�+�L�#�^�F�L�t�D�D�D�F�F����v�&�&�&rc#�bK�d}|jD]"}t|d��}||fV�||z }�#dS)Nr)rQrQ)rirzrir{s r�_sliceszConcatenationTable._slices�sR�������k� � �F�����^�^�F��6�"� "� "� "� �f� �F�F� � rc����|j������}���n |j�z �g}|jD]�}t |d��}�dkrnj|�kr�|z ��+|��zkr-|��fd�|D������z|z dc���a|���fd�|D����d\����t ||��S)r��r{Nrc�:��g|]}|������Srr�)r\r}rzs �rr^z,ConcatenationTable.slice.<locals>.<listcomp>�s#���?�?�?�1�q�w�w�v���?�?�?rc�<��g|]}|�������Srr�)r\r}r{rzs ��rr^z,ConcatenationTable.slice.<locals>.<listcomp>�s'���G�G�G�1�q�w�w�v�v�6�6�G�G�Gr)rr)r1rpr�rQrQr<rP)rirzr{r1rQri�n_rowss `` rrpzConcatenationTable.slice�s����� � � ��� �7�7��!�-���4�=�6�3I�����k� &� &�F�����^�^�F���{�{����6�!�!��&�����6�F�?�*�*�� � �?�?�?�?��?�?�?�@�@�@�!'�&��6�!9�1������ � �G�G�G�G�G��G�G�G�H�H�H�!%�����!�%��0�0�0rc���� �|jj|g��Ri���}g}t|j|j��D]A\\}}}|�||��� |���� fd�|D�����Bt||��S)r�c�2��g|]}|j�g��Ri�����Sr)r�)r\r}r�r��submasks ���rr^z-ConcatenationTable.filter.<locals>.<listcomp>�s5���N�N�N�!�8�1�8�G�=�d�=�=�=�f�=�=�N�N�Nr)r1r�rvr�rQrpr<rP) ri�maskr�r�r1rQrzr{rir�s `` @rr�zConcatenationTable.filter�s������"�� �!�$�8��8�8�8��8�8����(+�D�L�$�+�(F�(F� P� P� $� �V�V�f��j�j���0�0�G� �M�M�N�N�N�N�N�N�v�N�N�N� O� O� O� O�!�%��0�0�0rc����t|jg��Ri���}g}|jD]$}|���fd�|D�����%t ||��S)r�c�*��g|]}|j�i�����Sr)r��r\r}r�r�s ��rr^z.ConcatenationTable.flatten.<locals>.<listcomp>s*���F�F�F�!�9�1�9�d�5�f�5�5�F�F�Fr)rr1rQr<rP�rir�r�r1rQris `` rr�zConcatenationTable.flatten�sz�����d�j�:�4�:�:�:�6�:�:�����k� H� H�F� �M�M�F�F�F�F�F�v�F�F�F� G� G� G� G�!�%��0�0�0rc����|jj�i���}g}|jD]$}|���fd�|D�����%t ||��S)r�c�*��g|]}|j�i�����Sr)r�r�s ��rr^z5ConcatenationTable.combine_chunks.<locals>.<listcomp>s,���M�M�M��+�1�+�T�<�V�<�<�M�M�Mr)r1r�rQr<rPr�s `` rr�z!ConcatenationTable.combine_chunksst����*�� �)�4�:�6�:�:�����k� O� O�F� �M�M�M�M�M�M�M�f�M�M�M� N� N� N� N�!�%��0�0�0rc �$���ddlm}t|j|g|�Ri|��}|j|���g}|jD]�}g}t |��} |D]�} g} | jD]P�| �| � t�fd�t| ��D���������Q|�fd�| D����} | j } |�| j | g|�Ri|������|�|����t||��S)r�r �rc3�8�K�|]\}}|j�k�|V��dSr#�r9)r\rSr�r9s �rr�z*ConcatenationTable.cast.<locals>.<genexpr>3s6�����4o�4o�8�1�e�\a�\f�jn�\n�\n�Q�\n�\n�\n�\n�4o�4orc�6��i|]}|j�|j��Srr�)r\�subfield�target_featuress �rr�z+ConcatenationTable.cast.<locals>.<dictcomp>4s%���'p�'p�'p�Zb�� ��x�}�7U�'p�'p�'pr)�featuresrrr1�from_arrow_schemarQr�r�r<r��nextrb� arrow_schemar�rP)ri� target_schemar�r�rr1rQrU� new_tables�fieldsrV� subfields� subfeatures� subschemar9r�s @@rr�zConcatenationTable.castsk���� '�&�&�&�&�&��4�:�}�F�t�F�F�F�v�F�F��4�(�4�]�C�C������ &� &�I��J��-�(�(�F�%� M� M��� �$�1�r�r�D��$�$�V�Z�Z��4o�4o�4o�4o�y�QW�GX�GX�4o�4o�4o�0o�0o�%p�%p�q�q�q�q�&�h�'p�'p�'p�'p�fo�'p�'p�'p�q�q� �'�4� ��!�!�-�(�-� �"K�D�"K�"K�"K�F�"K�"K�L�L�L�L� �M�M�*� %� %� %� %�!�%��0�0�0rc����|jj�i���}g}|jD]$}|���fd�|D�����%t ||j��S)rc�*��g|]}|j�i�����Sr)r�r�s ��rr^z>ConcatenationTable.replace_schema_metadata.<locals>.<listcomp>Is,���V�V�V�!�4�1�4�d�E�f�E�E�V�V�Vr)r1r�rQr<rPr�s `` rr�z*ConcatenationTable.replace_schema_metadata:sv����3�� �2�D�C�F�C�C�����k� X� X�F� �M�M�V�V�V�V�V�v�V�V�V� W� W� W� W�!�%���5�5�5rc��t���r�r�r�s rr�zConcatenationTable.add_columnLr�rc��t���rr�r�s rr�z ConcatenationTable.append_columnas��"�#�#�#rc������|jj|g��Ri���}|jj|�g}|jD]%}|����fd�|D�����&t ||��S)rc�x��g|]6}�|jvr)|j|j����g��Ri���n|��7Sr)r�r��index)r\r}r�r�r9s ���rr^z4ConcatenationTable.remove_column.<locals>.<listcomp>�sk�������UY�\]�\j�Tj�Tj�O�A�O�A�N�$8�$8��$>�$>�P��P�P�P��P�P�P�pq���r)r1r�r�rQr<rP)rirSr�r�r1rQrir9s `` @rr�z ConcatenationTable.remove_columnrs������)�� �(��<�T�<�<�<�V�<�<���z�&�q�)�����k� � �F� �M�M�������#���� � � � � "�%��0�0�0rc��t���rr�r�s rr�zConcatenationTable.set_column�s��""�#�#�#rc�����|jj�g��Ri���}tt|jj������g}|jD]%}|����fd�|D�����&t||��S)r�c�T��g|]$}|j�fd�|jD��g��Ri�����%S)c� ��g|] }�|�� Srr)r\r9�namess �rr^z@ConcatenationTable.rename_columns.<locals>.<listcomp>.<listcomp>�s���"J�"J�"J�4�5��;�"J�"J�"Jr)r�r�)r\r}r�r�r�s ���rr^z5ConcatenationTable.rename_columns.<locals>.<listcomp>�sP���m�m�m�ab�!��!�"J�"J�"J�"J�1�>�"J�"J�"J�\�T�\�\�\�U[�\�\�m�m�mr)r1r�r�rvr�rQr<rP)rir�r�r�r1rQris ``` rr�z!ConcatenationTable.rename_columns�s������*�� �)�%�A�$�A�A�A�&�A�A���S���0�%�8�8�9�9�����k� � �F� �M�M�m�m�m�m�m�m�fl�m�m�m� � � � �"�%��0�0�0rc�����|jj�g��Ri���}g}|jD]%}|����fd�|D�����&t ||��S)r"c�L���g|]��j�fd��D��g��Ri����� S)c�&��g|] }|�jv� |��Sr�r��r\�cr}s �rr^z6ConcatenationTable.drop.<locals>.<listcomp>.<listcomp>�s%���"M�"M�"M���a�n�9L�9L�1�9L�9L�9Lr)r��r\r}r�r�r�s @���rr^z+ConcatenationTable.drop.<locals>.<listcomp>�sN����p�p�p�de�6�1�6�"M�"M�"M�"M�g�"M�"M�"M�_�PT�_�_�_�X^�_�_�p�p�pr)r1r�rQr<rP�rir�r�r�r1rQris ``` rr�zConcatenationTable.drop�s������ �� ���9�$�9�9�9�&�9�9�����k� r� r�F� �M�M�p�p�p�p�p�p�io�p�p�p� q� q� q� q�!�%��0�0�0rc�����|jj�g��Ri���}g}|jD]%}|����fd�|D�����&t ||��S)r$c�L���g|]��j�fd��D��g��Ri����� S)c�&��g|] }|�jv� |��Srr�r�s �rr^z8ConcatenationTable.select.<locals>.<listcomp>.<listcomp>�s%���$O�$O�$O�1�1���;N�;N�Q�;N�;N�;Nr)r�r�s @���rr^z-ConcatenationTable.select.<locals>.<listcomp>�sN����r�r�r�fg�8�1�8�$O�$O�$O�$O��$O�$O�$O�a�RV�a�a�a�Z`�a�a�r�r�rr)r1r�rQr<rPr�s ``` rr�zConcatenationTable.select�s������"�� �!�'�;�D�;�;�;�F�;�;�����k� t� t�F� �M�M�r�r�r�r�r�r�kq�r�r�r� s� s� s� s�!�%��0�0�0rrZr#r~)'rr�r�rr$rr�r�rjr3r6rMr r�rer%r\rNr rprzr~r�r�r�rpr�r�r�r�r�r�r�r�r�r�r�r�r�r�s@rrPrP�s����������2 �b�h� ��T�*�5E�0F� � � � � � �D�D�D� @� @� @��=�=�t�E�*�b�h�*>�$?�@�=��=�TV�T\�=�=�=��\�=� �J��T�*�EU�@V�J�[]�[c�J�J�J��[�J�� � �#6� �h�s�m� �Wj� � � ��[� ��-�)<�-�AT�-�-�-��[�-�� &�!4� &�9M� &� &� &��[� &��O'�O'��e�B�H�e�O�&<�!=�O'�S�O'�Qe�O'�O'�O'��[�O'�b����X��1�1�1�1�> 1� 1� 1�1�1�1�$1�1�1�(1�1�1�>6�6�6�$$�$�$�*$�$�$�"1�1�1�0$�$�$�& 1� 1� 1�1�1�1�*1�1�1�1�1�1�1rrPrir_c��t|��}t|��dkr|dSt�||���S)aH Concatenate tables. Args: tables (list of `Table`): List of tables to be concatenated. axis (`{0, 1}`, defaults to `0`, meaning over rows): Axis to concatenate over, where `0` means over rows (vertically) and `1` means over columns (horizontally). <Added version="1.6.0"/> Returns: `datasets.table.Table`: If the number of input tables is > 1, then the returned table is a `datasets.table.ConcatenationTable`. Otherwise if there's only one table, it is returned as is. r rrg)r�rQrPr�)rir_s rr]r]�sC��"�&�\�\�F� �6�{�{�a����a�y�� � )� )�&�t� )� <� <�<rr1c��t|t��r%g}|jD]}|D]}|t|��z }��|St|t��r|jgSgS)z� Get the cache files that are loaded by the table. Cache file are used when parts of the table come from the disk via memory mapping. Returns: `List[str]`: A list of paths to the cache files loaded by the table. )r�rPrQ�list_table_cache_filesr)r*)r1� cache_filesrUrVs rr�r��s����%�+�,�,� �� ��� @� @�I�%� @� @���5�h�?�?�?� � � @��� �E�,� -� -��� �|��� rc����fd�}|S)zVApply the function on each chunk of a `pyarrow.ChunkedArray`, or on the array directlyc�����t|tj��r'tj���fd�|jD����S�|g��Ri���S)Nc�(��g|]}�|g��Ri�����Srr)r\�chunkr��funcr�s ���rr^z=_wrap_for_chunked_arrays.<locals>.wrapper.<locals>.<listcomp>s3���$\�$\�$\�e�T�T�%�%A�$�%A�%A�%A�&�%A�%A�$\�$\�$\r)r�r$� ChunkedArray� chunked_array�chunks)�arrayr�r�r�s ``�rrz)_wrap_for_chunked_arrays.<locals>.wrappersg����� �e�R�_� -� -� 0��#�$\�$\�$\�$\�$\�$\�u�|�$\�$\�$\�]�]� ]��4��/��/�/�/��/�/� /rr)r�rs` r�_wrap_for_chunked_arraysr��s#���0�0�0�0�0� �Nrr�r{c���tjtj|���|�������p|jt |��kS)zICheck if all the sub-lists of a `pa.ListArray` have the specified length.)�pcru�equal� value_lengths�as_py� null_countrQ)r�r{s r�_are_list_values_of_lengthr� sM�� �6�"�(�5�.�.�0�0�&�9�9� :� :� @� @� B� B� d�e�FV�Z]�^c�Zd�Zd�Fd�drc �$�|j}|jdkr}tjt j|dd�|���tjt|��tj ������|dd�g��}|S)z7Add the null bitmap to the offsets of a `pa.ListArray`.rNrN) �offsetsr�r$� concat_arraysr��replace_with_mask�is_null�nullsrQ�int32)r�r�s r�%_combine_list_array_offsets_with_maskr�s���m�G� ��!����"��$�W�S�b�S�\�5�=�=�?�?�B�H�S�QV�Z�Z�Y[�Ya�Yc�Yc�Dd�Dd�e�e����� � � � �� �Nr�typec���t|tj��rt|j��St|tj��rtjd�|D����St|tj��r&tjt|j ����St|tj ��r,tjt|j ��|j ��S|S)zCConvert a (possibly nested) `pa.ExtensionType` to its storage type.c�f�g|].}tj|jt|j������/Sr)r$r�r9� _storage_typer��r\r�s rr^z!_storage_type.<locals>.<listcomp>"s0��\�\�\�e�"�(�5�:�}�U�Z�/H�/H�I�I�\�\�\r) r�r$� ExtensionTyper�� storage_type� StructType�struct�ListType�list_� value_type�FixedSizeListType� list_size�r�s rr�r�s����$��(�)�)�H��T�.�/�/�/� �D�"�-� (� (�H��y�\�\�W[�\�\�\�]�]�]� �D�"�+� &� &�H��x� �d�o�6�6�7�7�7� �D�"�.� /� /�H��x� �d�o�6�6���G�G�G� �Kr�valuec�z�t|��}t|��dkr|dd�dz|dd�z}|S)Ni� i�z ... i$���)r&rQ)r��outs rrTrT*s@�� �e�*�*�C� �3�x�x�$����%�4�%�j�9�$�s�5�6�6�{�2�� �JrT�pa_type�allow_primitive_to_str�allow_decimal_to_strc ��� �tt||���� t�tj��r�j�t|tj��r$|�� �|j����S�j |kr�Stj � �j ��r�tj � |��r�d�|D��d��j D��krb�j j dkr�S� �fd�|D��}tj �|t|���������S�n tj ��j ��s%tj ��j ���r4tj �|���rNt)�|j���r6�jdkr��j }t/|��}||kr6� �|���t1j�d|jd����� �|���nt1j�d|jd�����j}tj�� ||j��|j����� ��S�j�j|jz�jt=���z|jz�}tj�� ||j��|j��S�nTtj �|��rCt?���}tj �|� �j|j����Stj �|��rCt?���}tj!�|� �j|j����S�n�tj ��j ���rtj �|��r�|j�j jkr��j�j�j jz�jt=���z�j jz�}tj�� ||j��|j����� ��S�n�tj �|��r�tEj#t=���d z���jz�j jz}tj �|� �j|j������� ��Stj �|��r�tEj#t=���d z���jz�j jz}tj!�|� �j|j������� ��S�nftj �$|��r�|s\tj �%�j ��r8tMd tO�j ���d tO|���d |�d����|s[tj �(�j ��r7tMd tO�j ���d tO|���d|�����tj �|��rXtj ��j ��s4tMd tO�j ���d tO|���������)|��StMd tO�j ���d tO|�������)a�Improved version of `pa.Array.cast` It supports casting `pa.StructArray` objects to re-order the fields. It also let you control certain aspects of the casting, e.g. whether to disable casting primitives (`booleans`, `floats` or `ints`) or disable casting decimals to strings. Args: array (`pa.Array`): PyArrow array to cast pa_type (`pa.DataType`): Target PyArrow type allow_primitive_to_str (`bool`, defaults to `True`): Whether to allow casting primitives to strings. Defaults to `True`. allow_decimal_to_str (`bool`, defaults to `True`): Whether to allow casting decimals to strings. Defaults to `True`. Raises: `pa.ArrowInvalidError`: if the arrow data casting fails `TypeError`: if the target type is not supported according, e.g. - if a field is missing - if casting from primitives to strings and `allow_primitive_to_str` is `False` - if casting from decimals to strings and `allow_decimal_to_str` is `False` Returns: `List[pyarrow.Array]`: the casted array �r�r�c��h|] }|j�� Srr�r�s r� <setcomp>zarray_cast.<locals>.<setcomp>[s��,M�,M�,M�E�U�Z�,M�,M�,Mrc��h|] }|j�� Srr�r�s rrzarray_cast.<locals>.<setcomp>[s��Qu�Qu�Qu�af�RW�R\�Qu�Qu�Qurrc�b��g|]+}���|j��|j����,Sr)r�r9r�)r\r��_cr�s ��rr^zarray_cast.<locals>.<listcomp>^s5���S�S�S�%�b�b����U�Z�0�0�%�*�=�=�S�S�Sr)r�r�T��return_fixed_size_list�r�r zCouldn't cast array of type z to z( since allow_primitive_to_str is set to � z$ and allow_decimal_to_str is set to )*r� array_castr�r$�ExtensionArray�storager�� wrap_arrayr�r��types� is_struct� num_fields� StructArrayrr�r��is_list� is_large_list�is_fixed_size_listr�r�r�r�r�� list_slice�values�FixedSizeListArrayr�rzrQr�� ListArray�LargeListArrayre�arange� is_string� is_primitiverSrT� is_decimalr�) r�r�r�r��arrays� array_typer�� array_values� array_offsetsrs ` @rr r 1s����D ��4J�au� v� v� v�B��%��*�+�+��� ���'�2�+�,�,�H#��!�!�"�"�U�G�,@�"A�"A�B�B�B� ��w� � �� � �� � �E�J� '� '�D#� �8� � �g� &� &� b�,M�,M�W�,M�,M�,M�Qu�Qu�jo�jt�Qu�Qu�Qu�,u�,u��z�$��)�)�� �S�S�S�S�S�7�S�S�S�F��>�-�-�f�T�'�]�]�QV�Q^�Q^�Q`�Q`�-�a�a� a�� �� � �%�*� %� %�>#���)?�)?�� �)K�)K�>#� �8� &� &�w� /� /� f�)�%��1B�C�C� v��#�a�'�'�!&��J�#0��#<�#<�L�!�\�1�1� "��5�,� 7� 7�� "� �e�Q��8I�bf� g� g� g�� "��5�*� 5� 5��� "� �e�Q��8I�bf� g� g� g��#(�<�L��0�<�<���<��);�<�<�g�>O�V[�Vc�Vc�Ve�Ve�=����$)�<�� �w�'8�8�E�L�3�u�:�:�<U�Y`�Yj�;j�j�$�L��0�<�<�R�R� �g�N`�=a�=a�cj�ct�u�u�u�) v�*�X� � �g� &� &� f�A�%�H�H�M��<�+�+�M�2�2�e�l�G�L^�;_�;_�`�`� `� �X� #� #�G� ,� ,� f�A�%�H�H�M��$�0�0����5�<�QX�Qc�@d�@d�e�e� e� f� �� $� $�U�Z� 0� 0�#� �8� &� &�w� /� /� �� �E�J�$8�8�8�$�|��L�5�:�#7�7�5�<�#�e�*�*�;T�X]�Xb�Xl�:l�l� � ��,�8�8��B�|�W�%7�8�8�'�:K�RW�R_�R_�Ra�Ra�9���� 9��X� � �g� &� &� ��Y�s�5�z�z�A�~�6�6���E���I]�]�M��<�+�+�M�2�2�e�l�G�L^�;_�;_�fk�fs�fs�fu�fu�+�v�v� v� �X� #� #�G� ,� ,� ��Y�s�5�z�z�A�~�6�6���E���I]�]�M��$�0�0��r�r�%�,��0B�C�C�%�-�-�/�/�1��� � � �8� � �g� &� &� �)� �b�h�.C�.C�E�J�.O�.O� ��X�:�e�j�3I�3I�X�X�z�Za�Ob�Ob�X�X�>T�X�X�X����(� �B�H�,?�,?�� �,K�,K� ��Q�:�e�j�3I�3I�Q�Q�z�Za�Ob�Ob�Q�Q�:N�Q�Q���� �8� � �G� $� $� n�R�X�-=�-=�e�j�-I�-I� n��l�:�e�j�;Q�;Q�l�l�Wa�bi�Wj�Wj�l�l�m�m� m��z�z�'�"�"�"� �d�:�e�j�3I�3I�d�d�z�Za�Ob�Ob�d�d� e� e�er�featurerc��� � ����ddlm}m� m}t t ||���� t �tj��r�j �t|d��r|� ���Stj � �j���rEt |� ��rqt |jt ��rWt#|��������d��}� �fd�|���D��}t |t ��r�d��jD��x�t+|��kr�tjdgt/���z���� ���fd �|���D��}tj�|t5|������� ��S�n�tj ��j��s%tj ��j���r#t |t4��r�� �j|d ��}tj ��j��r|j�jjkr�St?���}tj �||��St ||��r�� �j|j��}tj ��j��r|j�jjkr�St?���}tj!�||��St |� ���r�|j"d k�r^tG�|j"���rF�j$d krΉj} tK| ��} | | krDtM�| ||����tOj(�d |j"d ����tM�| ||����ntOj(�d |j"d �����j} � | |j��}tj)�||j"�������S�j�j*|j"z�j*t/���z|j"z�} tj)�� | |j��|j"��S�n� �j|j��}tj ��j��r|j�jjkr�St?���}tj �||��S�n�tj �+�j���rht |t4��r�tYj-t/���dz���j*z�jj.z}tj �|� �j|d ���������St ||��r�tYj-t/���dz���j*z�jj.z}tj!�|� �j|j���������St |� ���r/|j"d kr�|j"�jj.kr��j�j*�jj.z�j*t/���z�jj.z�} � | |j��}tj)�||j"�������Sn�tYj-t/���dz���j*z�jj.z}tj �|� �j|j���������Stj ��j��rtM�||��||���St |� t t4t^f��stM�|��||���Stadtc�j���dtc|�������)abCast an array to the arrow type that corresponds to the requested feature type. For custom features like [`Audio`] or [`Image`], it takes into account the "cast_storage" methods they defined to enable casting from other arrow types. Args: array (`pa.Array`): The PyArrow array to cast. feature (`datasets.features.FeatureType`): The target feature type. allow_primitive_to_str (`bool`, defaults to `True`): Whether to allow casting primitives to strings. Defaults to `True`. allow_decimal_to_str (`bool`, defaults to `True`): Whether to allow casting decimals to strings. Defaults to `True`. Raises: `pa.ArrowInvalidError`: if the arrow data casting fails `TypeError`: if the target type is not supported according, e.g. - if a field is missing - if casting from primitives and `allow_primitive_to_str` is `False` - if casting from decimals and `allow_decimal_to_str` is `False` Returns: array (`pyarrow.Array`): the casted array r )� LargeList�Sequence�get_nested_typer�� cast_storager!c�*��i|]\}}|�|fi�����Srr)r\r9� subfeaturer$�sequence_kwargss ��rr�z)cast_array_to_feature.<locals>.<dictcomp>�s2���m�m�m�IY��z�t�X�X�j�D�D�O�D�D�m�m�mrc��h|] }|j�� Srr�r�s rrz(cast_array_to_feature.<locals>.<setcomp>�s��:^�:^�:^�%�5�:�:^�:^�:^rNc�`��g|]*\}}�|�vr��|��n�|����+Sr�r�)r\r9r(rr�� array_fields� null_arrays ����rr^z)cast_array_to_feature.<locals>.<listcomp>�sT������$�D�*���� �(<�(<�5�;�;�t�$�$�$�*�j�Y�Y���r�r�r�rrNTrrzCouldn't cast array of type � to )2�features.featuresr#r$r%r�cast_array_to_featurer�r$r r rr&r rr�r!r��varsrDr�rB�setr�rQrrr�r�rrrr�rrr{r�r�r�r r�rrrzrrerr�r�rSrT)r�r!r�r�r#r%r�casted_array_valuesr rr�rr$rr-r.r)s` @@@@@rr2r2�s@��������>H�G�G�G�G�G�G�G�G�G� ��5�1� � � �B� �%��*�+�+��� ���w��'�'�cx��#�#�E�*�*�*� �� � �E�J� '� '�`x� �g�x� (� (� n�Z����-N�-N� n�"�7�m�m�0�0�2�2�O�%�)�)�)�4�4�G�m�m�m�m�m�]d�]j�]j�]l�]l�m�m�m�G� �g�t� $� $� a�:^�:^�SX�S]�:^�:^�:^�*^�,�cf�gn�co�co�)o�)o���4�&�3�u�:�:�"5�6�6�J��������(/� � ������F��>�-�-�f�D��M�M�PU�P]�P]�P_�P_�-�`�`� `�� �� � �%�*� %� %�Sx���)?�)?�� �)K�)K�Sx� �g�t� $� $�< X�"$�"�U�\�7�1�:�">�">� ��x���� �+�+� T�0C�0H�E�L�L]�0]�0]�� �!F�e� L� L� ��|�/�/� �?R�S�S�S� ��� +� +�3 X�"$�"�U�\�7�?�"C�"C� ��x�%�%�e�j�1�1� Y�6I�6N�RW�R^�Rc�6c�6c�� �!F�e� L� L� ��(�4�4�]�DW�X�X�X� ��� *� *�* X��~��"�"�-�e�W�^�D�D�t��'�!�+�+�%*�Z� �'4�Z�'@�'@� �%��5�5�$.� %� ,�7M�5I� %�%�%�E� %'�M�%��G�N�cg�$h�$h�$h�E�$.� %� *�7M�5I� %�%�%�E�E�%'�M�%��G�N�cg�$h�$h�$h�E�',�|� �.0�b��w��.O�.O�+�!�4�@�@�/���e�m�m�o�o� A� � ��(-�|�!�L�7�>�9�U�\�C�PU�J�J�=V�Za�Zh�<h�h�(� � "�4�@�@���L�RY�Ra�Ab�Ab�dk�dr�s�s�s�?t�B')�b���w��&G�&G�#��8�#�#�E�J�/�/�X�4G�4L�PU�P\�Pa�4a�4a� �L�%J�%�$P�$P�M��<�3�3�M�CV�W�W�W�U* X�V �� $� $�U�Z� 0� 0�x� �g�t� $� $� x��Y�s�5�z�z�A�~�6�6���E���I]�]�M��<�+�+�M�2�2�e�l�G�TU�J�;W�;W�^c�^k�^k�^m�^m�+�n�n� n� ��� +� +� x��Y�s�5�z�z�A�~�6�6���E���I]�]�M��$�0�0��r�r�%�,���@�@�u�}�}���1��� ���� *� *� x��~��"�"��>�U�Z�%9�9�9�#(�<�� �u�z�';�;�u�|�c�RW�j�j�?X�\a�\f�\p�>p�p�$�L�+-�"�\�7�?�*K�*K�'��0�<�<�=P�RY�R`�gl�gt�gt�gv�gv�<�w�w�w� :�"$��3�u�:�:��>�!:�!:�U�\�!I�U�Z�Ma� a� ��|�/�/� �r�r�%�,�PW�P_�?`�?`�gl�gt�gt�gv�gv�/�w�w�w� �x���� �#�#�  �� � �O�G� $� $�#9�!5�  � � � � ��(�D�$��!>� ?� ?� �� � �G�I�I�#9�!5�  � � � � �g�J�u�z�4J�4J�g�g�R\�]d�Re�Re�g�g� h� h�hrc�>�����ddlm�t�t�tj��r�j�t�d��r�����Stj � �j ��r�t����r@t�j t��r&��fd��j ���D���t�t��rb��fd�����D��}tj�|t#����������S�ntj ��j ��r�t)���}t�t"��r5tj�|��j�d����St����r?�jdkr4tj�|��j�j ����S�n3tj ��j ��rCt)���}tj�|��j�j ����Stj ��j ��r�t����r��jdkr��j�j�j jz�jt;���z�j jz�}�|�j ��}tj�|�j����� ��St��tt"t>f��s�StAd tC�j ���d tC��������) aYEmbed data into an arrays's storage. For custom features like Audio or Image, it takes into account the "embed_storage" methods they define to embed external data (e.g. an image file) into an array. <Added version="2.4.0"/> Args: array (`pa.Array`): The PyArrow array in which to embed data. feature (`datasets.features.FeatureType`): Array features. Raises: `TypeError`: if the target type is not supported according, e.g. - if a field is missing Returns: array (`pyarrow.Array`): the casted array r )r$� embed_storagec�<��i|]\}}|�|�j�����S�r�r��r\r9r(r$r!s ��rr�z'embed_array_storage.<locals>.<dictcomp>as=������FV�d�J��h�h�z�'�.�A�A�A���rc�T��g|]$\}}���|��|����%Srr,)r\r9r(�_er�s ��rr^z'embed_array_storage.<locals>.<listcomp>es6���`�`�`�<L�D�*�b�b����T�*�*�J�7�7�`�`�`rr/rrNrzCouldn't embed array of type z with )"r�r$�embed_array_storager�r$r r rr7r rr�r!r�rBrrr�r�rr�rrr{rrrrzr�rQrr�rSrT)r�r!rr r�embedded_array_valuesr$r<s`` @@rr=r=@sa������,#�"�"�"�"�"� �B��%��*�+�+��� ���w��(�(�r��$�$�U�+�+�+� �� � �E�J� '� '�r� �g�x� (� (� �Z����-N�-N� ������Za�Zi�Zo�Zo�Zq�Zq����G� �g�t� $� $� a�`�`�`�`�`�PW�P]�P]�P_�P_�`�`�`�F��>�-�-�f�D��M�M�PU�P]�P]�P_�P_�-�`�`� `� a� �� � �%�*� %� %�r�>�e�D�D� � �g�t� $� $� Y��<�+�+�M�2�2�e�l�G�TU�J�;W�;W�X�X� X� �g�x� (� (� ^�W�^�r�-A�-A��<�+�+�M�2�2�e�l�G�O�;\�;\�]�]� ]�� �� � �� � +� +� r�>�e�D�D� �� �,�,�]�B�B�u�|�W�_�<]�<]�^�^�^� �� $� $�U�Z� 0� 0�r� �g�x� (� (� r�W�^�b�-@�-@� �<�� �u�z�3�3�u�|�c�%�j�j�7P�TY�T^�Th�6h�h��L�%'�B�|�W�_�$E�$E� !��(�4�4�5J�G�N�af�an�an�ap�ap�4�q�q� q� �g��$��e�<� =� =��� � �j�Z�� �5K�5K�j�j�U_�`g�Uh�Uh�j�j� k� k�krc�T��eZdZdZdeedeeddf�fd� Zd�Zd�Z�xZ S) � CastErrorzUWhen it's not possible to cast an Arrow table to a specific schema or set of features�table_column_names�requested_column_namesr!Nc�P��t��j|�||_||_dSr#)r�rjrArB)rirArBr�r>s �rrjzCastError.__init__�s-��������$���"4���&<��#�#�#rc�H�tt|j|j���dfS)N�rArBr)rr@rArBr�s r� __reduce__zCastError.__reduce__�s/��� �$�*A�Z^�Zu� � � � �� rc ���t|j��t|j��z }t|j��t|j��z }|rE|rCdt|���dt |���dt|���dt |���d� S|r#dt|���dt |���d�Sdt|���dt |���d�S)Nz there are z new columns (z) and z missing columns (z).�))r4rArBrQrT)ri� new_columns�missing_columnss r�detailszCastError.details�s-���$�1�2�2�S��9T�5U�5U�U� ��d�9�:�:�S��AX�=Y�=Y�Y�� � g�?� g�g��K� 0� 0�g�g� �;�@W�@W�g�g�_b�cr�_s�_s�g�g�HR�Sb�Hc�Hc�g�g�g� g� � g�Z��K� 0� 0�Z�Z� �;�@W�@W�Z�Z�Z� Z�f��O� 4� 4�f�f� �Sb�Hc�Hc�f�f�f� fr) rr�r�rr�r&rjrFrKr�r�s@rr@r@�s��������_�_�=�$�s�)�=�UY�Z]�U^�=�cg�=�=�=�=�=�=� ��� g�g�g�g�g�g�grr@r�rc�n��t�j��t|��krJtdt�j���dt|���d��jt |�������fd�|���D��}tj� ||j ���S)aCast a table to the arrow schema that corresponds to the requested features. Args: table (`pyarrow.Table`): PyArrow table to cast. features ([`Features`]): Target features. Returns: table (`pyarrow.Table`): the casted table �Couldn't cast r0�! because column names don't matchrEc�B��g|]\}}t�||����Sr)r2)r\r9r!r1s �rr^z*cast_table_to_features.<locals>.<listcomp>�s,��� `� `� `�m�d�G�#�E�$�K��9�9� `� `� `rrr) �sortedr�r@rTr8r�rBr$rrr�)r1r�rs` r�cast_table_to_featuresrQ�s�����e� �!�!�V�H�%5�%5�5�5�� v�j���6�6� v� v�j��>R�>R� v� v� v�$�1�#'��>�>� � � � � a� `� `� `�x�~�~�O_�O_� `� `� `�F� �8� � ��x�/D� � E� E�Err8c�����ddlm}|j���}t�j����t�j��ksJt dt�j���dt|���d��jt|���������fd�|� ��D��}tj � |����S) a0Cast a table to the arrow schema. Different from `cast_table_to_features`, this method can preserve nullability. Args: table (`pa.Table`): PyArrow table to cast. features ([`Features`]): Target features. Returns: `pa.Table`: the casted table r r�rMr0rNrEc ����g|]^\}}t|�vr�|n>tjdgt���z��|��j���|����_S)Nr�)r2r$r�rQr�r�)r\r9r!r8r1rAs ���rr^z(cast_table_to_schema.<locals>.<listcomp>�s������� �D�'� ��#5�5�5�E�$�K�K�2�8�T�F�S�QV�Z�Z�DW�^d�^j�^j�ko�^p�^p�^u�;v�;v�;v� � � ���rrr)r�rr�r4r�r�r@rTr8r�rBr$rr)r1r8rr�rrAs`` @r�cast_table_to_schemarT�s������#�"�"�"�"�"�)�x�)�&�1�1�H��U�/�0�0�� ��V�\�!2�!2� 2� 2�� v�j���6�6� v� v�j��>R�>R� v� v� v�$�1�#'��>�>� � � � � ������ &�^�^�-�-� ���F� �8� � ��v� � 6� 6�6rc�����ddlm}m�|j�j��}��fd�|���D��}t j�||j ���S)z�Embed external data into a table's storage. <Added version="2.4.0"/> Args: table (`pyarrow.Table`): PyArrow table in which to embed data. Returns: table (`pyarrow.Table`): the table with embedded data r )r�require_storage_embedc�h��g|].\}}�|��rt�||��n�|��/Sr)r=)r\r9r!rVr1s ��rr^z'embed_table_storage.<locals>.<listcomp>�sY������ �D�'�6K�5J�7�5S�5S�d��E�$�K��1�1�1�Y^�_c�Yd���rrr) r1rrVr�r8rBr$rrr�)r1rr�rrVs` @r�embed_table_storagerX�s�����C�B�B�B�B�B�B�B�)�x�)�%�,�7�7�H������%�^�^�-�-����F� �8� � ��x�/D� � E� E�Erc��|j|krt||��S|jj|jkr|�|j��S|S)aGImproved version of `pa.Table.cast`. It supports casting to feature types stored in the schema metadata. Args: table (`pyarrow.Table`): PyArrow table to cast. schema (`pyarrow.Schema`): Target PyArrow schema. Returns: table (`pyarrow.Table`): the casted table )r8rT�metadatar�r�s rrr�sN�� �|�v���#�E�6�2�2�2� �� �&�/� 1� 1��,�,�V�_�=�=�=�� rc��� �ddlm}|j|j��}t d�|���D�����r g}g}|jD]�|��j��}|�j}tj � �j ��rvt|d��r|� ��|krN|�|� ����|��fd��j D������|�|��|��j����tj�||���}n|� ��}|� d���� |� fd �|jD����� |�� jj��S) a�Improved version of `pa.Table.flatten`. It behaves as `pa.Table.flatten` in a sense it does 1-step flatten of the columns with a struct type into one column per struct field, but updates the metadata and skips decodable features unless the `decode` attribute of these features is set to False. Args: table (`pa.Table`): PyArrow table to flatten. Returns: `Table`: the flattened table r r�c3�fK�|],}t|d��o|���|kV��-dS)r�N)rr�)r\r(s rr�z table_flatten.<locals>.<genexpr> sC���� u� u�U_�7�:�y� )� )� P�j�.@�.@�.B�.B�j�.P� u� u� u� u� u� urr�c�2��g|]}�j�d|j����S)rPr�)r\r�r�s �rr^z!table_flatten.<locals>.<listcomp> s+���)e�)e�)e�h�U�Z�*I�*I�(�-�*I�*I�)e�)e�)er)r��)� max_depthc�"��i|] }|�|�� Srr)r\� column_name� flat_featuress �rr�z!table_flatten.<locals>.<dictcomp>" s ���q�q�q�+�k�=��+E�q�q�qr)r�rr�r8�anyrr�r9r$r rr�rr�r�r<rrr�r�r�rZ) r1rr�� flat_arrays�flat_column_namesr�r(� flat_tabler�rbs @@rrr�s�����#�"�"�"�"�"�)�x�)�%�,�7�7�H� � u� u�ck�cr�cr�ct�ct� u� u� u�u�u�%�� ����\� 5� 5�E��L�L���,�,�E�!�%�*�-�J��x�!�!�%�*�-�-� 5��J� �2�2� 5�6@�6H�6H�6J�6J�j�6X�6X��"�"�5�=�=�?�?�3�3�3�!�(�(�)e�)e�)e�)e�Z_�Zd�)e�)e�)e�f�f�f�f��"�"�5�)�)�)�!�(�(���4�4�4�4��X�)�)� �#�*� � � � � �]�]�_�_� ��$�$�q�$�1�1�M��H�q�q�q�q�Yc�Yp�q�q�q�r�r�M� � -� -�m�.H�.Q� R� R�Rr�functionc�����ddlm}m�|j|j��}���fd��|���D]\}}�|||���dS)z�Visit all arrays in a table and apply a function to them. Args: table (`pyarrow.Table`): PyArrow table to visit. function (`Callable[[pa.Array], None]`): Function to apply to each array. r )rr$c�>���t|tj��r|jD]}�|����dSt|tj��r|j}�|���tj�|j��r�t�d��s�t����r@t�j t��r&��fd��j � ��D����� ��D]$\}}�|� |��|���%dStj�|j��rVt�t��r�|j�d��dSt����r�|j�j ��dSdSdS)Nr&c�<��i|]\}}|�|�j�����Sr9r�r:s ��rr�z1table_visitor.<locals>._visit.<locals>.<dictcomp>= s@������,�D�*��h�h�z�'�.�I�I�I���rr)r�r$r�r�r r r rr�rr!r�rBr�rr�r)r�r!r�r9r(r$�_visitrgs ` ���rrkztable_visitor.<locals>._visit3 s����� �e�R�_� -� -� :��� '� '����u�g�&�&�&�&� '� '��%��!2�3�3� &�� �� �H�U�G� $� $� $��x�!�!�%�*�-�-� :�g�g�~�6V�6V� :��g�x�0�0��Z���QU�5V�5V�������07��0E�0E�0G�0G����G�)0� � ���:�:�$�D�*��F�5�;�;�t�,�,�j�9�9�9�9�:�:���!�!�%�*�-�-� :��g�t�,�,�:��F�5�<����4�4�4�4�4����2�2�:��F�5�<���9�9�9�9�9�  :� :�:�:rN)r�rr$r�r8rB)r1rgrr�r9r!r$rks ` @@r� table_visitorrl& s������-�,�,�,�,�,�,�,�)�x�)�%�,�7�7�H�:�:�:�:�:�:�:�,"���)�)�%�%� ��g���u�T�{�G�$�$�$�$�%�%rF� batch_sizec#��K�g}d}|�|���D�].}t|��dkr�|t|��z|kr(|�|��|t|��z }�U|t|��z|kr;|�|��tj�|��V�g}d}��||z }|�|�d|����tj�|��V�|�|t|��|z ��g}t|��|z }��0|s%|r%tj�|��V�dSdSdS)aMIterate over sub-tables of size `batch_size`. Args: table (`pyarrow.Table`): PyArrow table to iterate over. batch_size (`int`): Size of each sub-table to yield. drop_last_batch (`bool`, defaults to `False`): Drop the last batch if it is smaller than `batch_size`. rr�N)r�rQr<r$rrurp)r1rm�drop_last_batch� chunks_buffer�chunks_buffer_sizer��cropped_chunk_lengths r� table_iterrsM s������M������z��:�:�C�C�� �u�:�:��?�?� � �#�e�*�*� ,�z� 9� 9� � � �� '� '� '� �#�e�*�*� ,� � � �#�e�*�*� ,� � :� :� � � �� '� '� '��(�'�'� �6�6� 6� 6� 6��M�!"� � �#-�0B�#B� � � � ����Q�0D�!E�!E� F� F� F��(�'�'� �6�6� 6� 6� 6�"�[�[�)=�s�5�z�z�L`�?`�a�a�b�M�!$�U���.B�!B� � � �3�}�3��h�#�#�M�2�2�2�2�2�2�2�3�3�3�3rrZ)TT)F)PrDr-�collections.abcr� functoolsr� itertoolsr�typingrrrr r r �numpyre�pyarrowr$�pyarrow.compute�computer�� pyarrow.types� utils.loggingr r1rrr�loggerrr&rr,r'r2�RecordBatchStreamReaderr6�Schemar9r;r�rKr�r�rUrWr�r�r�rLr)rNrPr]r�r�r�boolr��Arrayr��DataTyper�rTrrr r r2r=rsr@rQrTrXrrrlrsrrr�<module>r�s�� � � � � � � � �$�$�$�$�$�$�������������I�I�I�I�I�I�I�I�I�I�I�I�I�I�I�I�������������������%�%�%�%�%�%��9�8�8�8�8�8�8�8�8� ��H� � ������s��r�x������r�y��R�X�����4�3�4�2�C]�4�4�4�4� �C��B�I������3��2�8����� �t�����Y�t�C�y�Y�S�Y�S�Y�Y�Y�Y�4.C�.C�.C�.C�.C�.C�.C�.C�bX$�X$�X$�X$�X$� �X$�X$�X$�v � � � � �� � � �XA�XA�XA�XA�XA�J�XA�XA�XA�x �s�E�4�� ��RY�RY�RY�RY�RY� �RY�RY�RY�r�g�3�Z��j�AQ�SW�X\�]g�Xh�Si�j�j��V1�V1�V1�V1�V1��V1�V1�V1�r=�=�$�u�+�=�S�=��=�=�=�=�.�%��D��I�����* � � �e�b�l�e�C�e�D�e�e�e�e� ��� �"�(� � � � � �� � �� � � � � ��c��c������mq�mf�mf� �8�mf� �k�mf�CG�mf�fj�mf� �2�8�R�*�B�L�"�.�"�J[� [�\�mf�mf�mf���mf�`�os�Zi�Zi� �8�Zi�+�Zi�EI�Zi�hl�Zi��X�Zi�Zi�Zi���Zi�z�=l�r�x�=l�-�=l�=l�=l���=l�@g�g�g�g�g� �g�g�g�2F�"�(�F�j�F�F�F�F�,7���7�"�)�7�7�7�7�@F�r�x�F�F�F�F�,�b�h��� �����,'S���'S�'S�'S�'S�T$%���$%�X�r�x�j�$�6F�-G�$%�$%�$%�$%�N 3� 3�e� 3�� 3��QS�QY�HZ� 3� 3� 3� 3� 3� 3r
Memory