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

Function _reconstruct_block

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

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

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