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

Function arrays_to_mgr

pandas/core/internals/construction.py:96–152  ·  view source on GitHub ↗

Segregate Series based on type and coerce into matrices. Needs to handle a lot of exceptional cases.

(
    arrays,
    columns: Index,
    index,
    *,
    dtype: DtypeObj | None = None,
    verify_integrity: bool = True,
    consolidate: bool = True,
)

Source from the content-addressed store, hash-verified

94
95
96def arrays_to_mgr(
97 arrays,
98 columns: Index,
99 index,
100 *,
101 dtype: DtypeObj | None = None,
102 verify_integrity: bool = True,
103 consolidate: bool = True,
104) -> Manager:
105 """
106 Segregate Series based on type and coerce into matrices.
107
108 Needs to handle a lot of exceptional cases.
109 """
110 if verify_integrity:
111 # figure out the index, if necessary
112 if index is None:
113 index = _extract_index(arrays)
114 else:
115 index = ensure_index(index)
116
117 # don't force copy because getting jammed in an ndarray anyway
118 arrays, refs = _homogenize(arrays, index, dtype)
119 # _homogenize ensures
120 # - all(len(x) == len(index) for x in arrays)
121 # - all(x.ndim == 1 for x in arrays)
122 # - all(isinstance(x, (np.ndarray, ExtensionArray)) for x in arrays)
123 # - all(type(x) is not NumpyExtensionArray for x in arrays)
124
125 else:
126 index = ensure_index(index)
127 arrays = [extract_array(x, extract_numpy=True) for x in arrays]
128 # with _from_arrays, the passed arrays should never be Series objects
129 refs = [None] * len(arrays)
130
131 # Reached via DataFrame._from_arrays; we do minimal validation here
132 for arr in arrays:
133 if (
134 not isinstance(arr, (np.ndarray, ExtensionArray))
135 or arr.ndim != 1
136 or len(arr) != len(index)
137 ):
138 raise ValueError(
139 "Arrays must be 1-dimensional np.ndarray or ExtensionArray "
140 "with length matching len(index)"
141 )
142
143 columns = ensure_index(columns)
144 if len(columns) != len(arrays):
145 raise ValueError("len(arrays) must match len(columns)")
146
147 # from BlockManager perspective
148 axes = [columns, index]
149
150 return create_block_manager_from_column_arrays(
151 arrays, axes, consolidate=consolidate, refs=refs
152 )
153

Callers 6

__init__Method · 0.90
from_recordsMethod · 0.90
_from_arraysMethod · 0.90
rec_array_to_mgrFunction · 0.85
ndarray_to_mgrFunction · 0.85
dict_to_mgrFunction · 0.85

Calls 5

extract_arrayFunction · 0.90
_extract_indexFunction · 0.85
ensure_indexFunction · 0.85
_homogenizeFunction · 0.85

Tested by

no test coverage detected