MCPcopy Create free account
hub / github.com/apache/arrow / _reconstruct_block

Function _reconstruct_block

python/pyarrow/pandas_compat.py:723–789  ·  view source on GitHub ↗

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 co

(item, columns=None, extension_columns=None, return_block=True)

Source from the content-addressed store, hash-verified

721
722
723def _reconstruct_block(item, columns=None, extension_columns=None, return_block=True):
724 """
725 Construct a pandas Block from the `item` dictionary coming from pyarrow's
726 serialization or returned by arrow::python::ConvertTableToPandas.
727
728 This function takes care of converting dictionary types to pandas
729 categorical, Timestamp-with-timezones to the proper pandas Block, and
730 conversion to pandas ExtensionBlock
731
732 Parameters
733 ----------
734 item : dict
735 For basic types, this is a dictionary in the form of
736 {'block': np.ndarray of values, 'placement': pandas block placement}.
737 Additional keys are present for other types (dictionary, timezone,
738 object).
739 columns :
740 Column names of the table being constructed, used for extension types
741 extension_columns : dict
742 Dictionary of {column_name: pandas_dtype} that includes all columns
743 and corresponding dtypes that will be converted to a pandas
744 ExtensionBlock.
745
746 Returns
747 -------
748 pandas Block
749
750 """
751 import pandas.core.internals as _int
752
753 block_arr = item.get('block', None)
754 placement = item['placement']
755 if 'dictionary' in item:
756 arr = _pandas_api.categorical_type.from_codes(
757 block_arr, categories=item['dictionary'],
758 ordered=item['ordered'])
759 elif 'timezone' in item:
760 unit, _ = np.datetime_data(block_arr.dtype)
761 dtype = make_datetimetz(unit, item['timezone'])
762 if _pandas_api.is_ge_v21():
763 arr = _pandas_api.pd.array(
764 block_arr.view("int64"), dtype=dtype, copy=False
765 )
766 else:
767 arr = block_arr
768 if return_block:
769 block = _int.make_block(block_arr, placement=placement,
770 klass=_int.DatetimeTZBlock,
771 dtype=dtype)
772 return block
773 elif 'py_array' in item:
774 # create ExtensionBlock
775 arr = item['py_array']
776 assert len(placement) == 1
777 name = columns[placement[0]]
778 pandas_dtype = extension_columns[name]
779 if not hasattr(pandas_dtype, '__from_arrow__'):
780 raise ValueError("This column does not support to be converted "

Callers 1

table_to_dataframeFunction · 0.85

Calls 5

make_datetimetzFunction · 0.85
lenFunction · 0.85
getMethod · 0.45
arrayMethod · 0.45
viewMethod · 0.45

Tested by

no test coverage detected