MCPcopy
hub / github.com/pandas-dev/pandas / _convert_index

Function _convert_index

pandas/io/pytables.py:5125–5186  ·  view source on GitHub ↗
(name: str, index: Index, encoding: str, errors: str)

Source from the content-addressed store, hash-verified

5123
5124
5125def _convert_index(name: str, index: Index, encoding: str, errors: str) -> IndexCol:
5126 assert isinstance(name, str)
5127
5128 index_name = index.name
5129 # error: Argument 1 to "_get_data_and_dtype_name" has incompatible type "Index";
5130 # expected "Union[ExtensionArray, ndarray]"
5131 converted, dtype_name = _get_data_and_dtype_name(index) # type: ignore[arg-type]
5132 kind = _dtype_to_kind(dtype_name)
5133 atom = DataIndexableCol._get_atom(converted)
5134
5135 if (
5136 lib.is_np_dtype(index.dtype, "iu")
5137 or needs_i8_conversion(index.dtype)
5138 or is_bool_dtype(index.dtype)
5139 ):
5140 # Includes Index, RangeIndex, DatetimeIndex, TimedeltaIndex, PeriodIndex,
5141 # in which case "kind" is "integer", "integer", "datetime64",
5142 # "timedelta64", and "integer", respectively.
5143 return IndexCol(
5144 name,
5145 values=converted,
5146 kind=kind,
5147 typ=atom,
5148 freq=getattr(index, "freq", None),
5149 tz=getattr(index, "tz", None),
5150 index_name=index_name,
5151 )
5152
5153 if isinstance(index, MultiIndex):
5154 raise TypeError("MultiIndex not supported here!")
5155
5156 inferred_type = lib.infer_dtype(index, skipna=False)
5157 # we won't get inferred_type of "datetime64" or "timedelta64" as these
5158 # would go through the DatetimeIndex/TimedeltaIndex paths above
5159
5160 values = np.asarray(index)
5161
5162 if inferred_type == "date":
5163 converted = np.asarray([v.toordinal() for v in values], dtype=np.int32)
5164 return IndexCol(
5165 name, converted, "date", _tables().Time32Col(), index_name=index_name
5166 )
5167 elif inferred_type == "string":
5168 converted = _convert_string_array(values, encoding, errors)
5169 itemsize = converted.dtype.itemsize
5170 return IndexCol(
5171 name,
5172 converted,
5173 "string",
5174 _tables().StringCol(itemsize),
5175 index_name=index_name,
5176 )
5177
5178 elif inferred_type in ["integer", "floating"]:
5179 return IndexCol(
5180 name, values=converted, kind=kind, typ=atom, index_name=index_name
5181 )
5182 else:

Callers 3

write_indexMethod · 0.85
write_multi_indexMethod · 0.85
_create_axesMethod · 0.85

Calls 8

needs_i8_conversionFunction · 0.90
is_bool_dtypeFunction · 0.90
_get_data_and_dtype_nameFunction · 0.85
_dtype_to_kindFunction · 0.85
IndexColClass · 0.85
_tablesFunction · 0.85
_convert_string_arrayFunction · 0.85
_get_atomMethod · 0.80

Tested by

no test coverage detected