Skip to content

PERF: allow to skip validation/sanitization in DataFrame._from_arrays #32858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
21 changes: 12 additions & 9 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down