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