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

Function ndarray_to_mgr

pandas/core/internals/construction.py:193–354  ·  view source on GitHub ↗
(
    values, index, columns, dtype: DtypeObj | None, copy: bool
)

Source from the content-addressed store, hash-verified

191
192
193def ndarray_to_mgr(
194 values, index, columns, dtype: DtypeObj | None, copy: bool
195) -> Manager:
196 # used in DataFrame.__init__
197 # input must be an ndarray, list, Series, Index, ExtensionArray
198 infer_object = not isinstance(values, (ABCSeries, Index, ExtensionArray))
199
200 if isinstance(values, ABCSeries):
201 if columns is None:
202 if values.name is not None:
203 columns = Index([values.name])
204 if index is None:
205 index = values.index
206 else:
207 values = values.reindex(index)
208
209 # zero len case (GH #2234)
210 if not len(values) and columns is not None and len(columns):
211 values = np.empty((0, 1), dtype=object)
212
213 vdtype = getattr(values, "dtype", None)
214 refs = None
215 if is_1d_only_ea_dtype(vdtype) or is_1d_only_ea_dtype(dtype):
216 # GH#19157
217
218 if isinstance(values, (np.ndarray, ExtensionArray)) and values.ndim > 1:
219 # GH#12513 an EA dtype passed with a 2D array, split into
220 # multiple EAs that view the values
221 # error: No overload variant of "__getitem__" of "ExtensionArray"
222 # matches argument type "Tuple[slice, int]"
223 values = [
224 values[:, n] # type: ignore[call-overload]
225 for n in range(values.shape[1])
226 ]
227 else:
228 values = [values]
229
230 # Handle copy semantics: already copy 1d-only EA. Other arrays will
231 # be copied when consolidating the blocks
232 if copy:
233 values = [
234 (x.copy(deep=True) if isinstance(x, Index) else x.copy())
235 if isinstance(x, (ExtensionArray, Index, ABCSeries))
236 and is_1d_only_ea_dtype(x.dtype)
237 else x
238 for x in values
239 ]
240
241 if columns is None:
242 columns = Index(range(len(values)))
243 else:
244 columns = ensure_index(columns)
245
246 return arrays_to_mgr(values, columns, index, dtype=dtype, consolidate=copy)
247
248 if isinstance(values, (ABCSeries, Index)):
249 if not copy and (dtype is None or astype_is_view(values.dtype, dtype)):
250 refs = values._references

Callers 1

__init__Method · 0.90

Calls 15

construct_array_typeMethod · 0.95
is_1d_only_ea_dtypeFunction · 0.90
astype_is_viewFunction · 0.90
extract_arrayFunction · 0.90
sanitize_arrayFunction · 0.90
is_object_dtypeFunction · 0.90
new_block_2dFunction · 0.90
ensure_block_shapeFunction · 0.90
using_string_dtypeFunction · 0.90
StringDtypeClass · 0.90
new_blockFunction · 0.90

Tested by

no test coverage detected