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

Function _reconstruct_block

python/pyarrow/pandas_compat.py:718–784  ·  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

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