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

Method _ensure_valid_index

pandas/core/frame.py:4933–4954  ·  view source on GitHub ↗

Ensure that if we don't have an index, that we can create one from the passed value.

(self, value)

Source from the content-addressed store, hash-verified

4931 ) from ii_err
4932
4933 def _ensure_valid_index(self, value) -> None:
4934 """
4935 Ensure that if we don't have an index, that we can create one from the
4936 passed value.
4937 """
4938 # GH5632, make sure that we are a Series convertible
4939 if not len(self.index) and is_list_like(value) and len(value):
4940 if not isinstance(value, DataFrame):
4941 try:
4942 value = Series(value)
4943 except (ValueError, NotImplementedError, TypeError) as err:
4944 raise ValueError(
4945 "Cannot set a frame with no defined index "
4946 "and a value that cannot be converted to a Series"
4947 ) from err
4948
4949 # GH31368 preserve name of index
4950 index_copy = value.index.copy()
4951 if self.index.name is not None:
4952 index_copy.name = self.index.name
4953
4954 self._mgr = self._mgr.reindex_axis(index_copy, axis=1, fill_value=np.nan)
4955
4956 def _box_col_values(self, values: SingleBlockManager, loc: int) -> Series:
4957 """

Callers 2

_set_item_frame_valueMethod · 0.95
_sanitize_columnMethod · 0.95

Calls 3

SeriesClass · 0.90
reindex_axisMethod · 0.80
copyMethod · 0.45

Tested by

no test coverage detected