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

Function _reconstruct_index

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

Source from the content-addressed store, hash-verified

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