diff --git a/pandas/core/frame.py b/pandas/core/frame.py index b9e43b1cd9b05..d1ba85c50d91d 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1889,8 +1889,41 @@ def to_records( return np.rec.fromarrays(arrays, dtype={"names": names, "formats": formats}) @classmethod - def _from_arrays(cls, arrays, columns, index, dtype=None) -> "DataFrame": - mgr = arrays_to_mgr(arrays, columns, index, columns, dtype=dtype) + def _from_arrays( + cls, arrays, columns, index, dtype=None, verify_integrity=True + ) -> "DataFrame": + """ + Create DataFrame from a list of arrays corresponding to the columns. + + Parameters + ---------- + arrays : list-like of arrays + Each array in the list corresponds to one column, in order. + columns : list-like, Index + The column names for the resulting DataFrame. + index : list-like, Index + The rows labels for the resulting DataFrame. + dtype : dtype, optional + Optional dtype to enforce for all arrays. + verify_integrity : bool, default True + Validate and homogenize all input. If set to False, it is assumed + that all elements of `arrays` are actual arrays how they will be + stored in a block (numpy ndarray or ExtensionArray), have the same + length as and are aligned with the index, and that `columns` and + `index` are ensured to be an Index object. + + Returns + ------- + DataFrame + """ + mgr = arrays_to_mgr( + arrays, + columns, + index, + columns, + dtype=dtype, + verify_integrity=verify_integrity, + ) return cls(mgr) @deprecate_kwarg(old_arg_name="fname", new_arg_name="path") diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index c4416472d451c..3e0fb8455884a 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -53,23 +53,26 @@ # BlockManager Interface -def arrays_to_mgr(arrays, arr_names, index, columns, dtype=None): +def arrays_to_mgr(arrays, arr_names, index, columns, dtype=None, verify_integrity=True): """ Segregate Series based on type and coerce into matrices. Needs to handle a lot of exceptional cases. """ - # figure out the index, if necessary - if index is None: - index = extract_index(arrays) - else: - index = ensure_index(index) + if verify_integrity: + # figure out the index, if necessary + if index is None: + index = extract_index(arrays) + else: + index = ensure_index(index) - # don't force copy because getting jammed in an ndarray anyway - arrays = _homogenize(arrays, index, dtype) + # don't force copy because getting jammed in an ndarray anyway + arrays = _homogenize(arrays, index, dtype) + + columns = ensure_index(columns) # from BlockManager perspective - axes = [ensure_index(columns), index] + axes = [columns, index] return create_block_manager_from_arrays(arrays, arr_names, axes)