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

Function _reconstruct_index

python/pyarrow/pandas_compat.py:998–1048  ·  view source on GitHub ↗
(table, index_descriptors, all_columns, types_mapper=None)

Source from the content-addressed store, hash-verified

996
997
998def _reconstruct_index(table, index_descriptors, all_columns, types_mapper=None):
999 # 0. 'field_name' is the name of the column in the arrow Table
1000 # 1. 'name' is the user-facing name of the column, that is, it came from
1001 # pandas
1002 # 2. 'field_name' and 'name' differ for index columns
1003 # 3. We fall back on c['name'] for backwards compatibility
1004 field_name_to_metadata = {
1005 c.get('field_name', c['name']): c
1006 for c in all_columns
1007 }
1008
1009 # Build up a list of index columns and names while removing those columns
1010 # from the original table
1011 index_arrays = []
1012 index_names = []
1013 result_table = table
1014 for descr in index_descriptors:
1015 if isinstance(descr, str):
1016 result_table, index_level, index_name = _extract_index_level(
1017 table, result_table, descr, field_name_to_metadata, types_mapper)
1018 if index_level is None:
1019 # ARROW-1883: the serialized index column was not found
1020 continue
1021 elif descr['kind'] == 'range':
1022 index_name = descr['name']
1023 index_level = _pandas_api.pd.RangeIndex(descr['start'],
1024 descr['stop'],
1025 step=descr['step'],
1026 name=index_name)
1027 if len(index_level) != len(table):
1028 # Possibly the result of munged metadata
1029 continue
1030 else:
1031 raise ValueError(f"Unrecognized index kind: {descr['kind']}")
1032 index_arrays.append(index_level)
1033 index_names.append(index_name)
1034
1035 pd = _pandas_api.pd
1036
1037 # Reconstruct the row index
1038 if len(index_arrays) > 1:
1039 index = pd.MultiIndex.from_arrays(index_arrays, names=index_names)
1040 elif len(index_arrays) == 1:
1041 index = index_arrays[0]
1042 if not isinstance(index, pd.Index):
1043 # Box anything that wasn't boxed above
1044 index = pd.Index(index, name=index_names[0])
1045 else:
1046 index = pd.RangeIndex(table.num_rows)
1047
1048 return result_table, index
1049
1050
1051def _extract_index_level(table, result_table, field_name,

Callers 1

table_to_dataframeFunction · 0.85

Calls 4

_extract_index_levelFunction · 0.85
lenFunction · 0.85
getMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected