Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pandas/api/internals.py: 60%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import numpy as np
3from pandas._typing import ArrayLike
5from pandas import (
6 DataFrame,
7 Index,
8)
9from pandas.core.internals.api import _make_block
10from pandas.core.internals.managers import BlockManager as _BlockManager
13def create_dataframe_from_blocks(
14 blocks: list[tuple[ArrayLike, np.ndarray]], index: Index, columns: Index
15) -> DataFrame:
16 """
17 Low-level function to create a DataFrame from arrays as they are
18 representing the block structure of the resulting DataFrame.
20 Attention: this is an advanced, low-level function that should only be
21 used if you know that the below-mentioned assumptions are guaranteed.
22 If passing data that do not follow those assumptions, subsequent
23 operations on the resulting DataFrame might lead to strange errors.
24 For almost all use cases, you should use the standard pd.DataFrame(..)
25 constructor instead. If you are planning to use this function, let us
26 know by opening an issue at https://github.com/pandas-dev/pandas/issues.
28 Assumptions:
30 - The block arrays are either a 2D numpy array or a pandas ExtensionArray
31 - In case of a numpy array, it is assumed to already be in the expected
32 shape for Blocks (2D, (cols, rows), i.e. transposed compared to the
33 DataFrame columns).
34 - All arrays are taken as is (no type inference) and expected to have the
35 correct size.
36 - The placement arrays have the correct length (equalling the number of
37 columns that its equivalent block array represents), and all placement
38 arrays together form a complete set of 0 to n_columns - 1.
40 Parameters
41 ----------
42 blocks : list of tuples of (block_array, block_placement)
43 This should be a list of tuples existing of (block_array, block_placement),
44 where:
46 - block_array is a 2D numpy array or a 1D ExtensionArray, following the
47 requirements listed above.
48 - block_placement is a 1D integer numpy array
49 index : Index
50 The Index object for the `index` of the resulting DataFrame.
51 columns : Index
52 The Index object for the `columns` of the resulting DataFrame.
54 Returns
55 -------
56 DataFrame
57 """
58 block_objs = [_make_block(*block) for block in blocks]
59 axes = [columns, index]
60 mgr = _BlockManager(block_objs, axes)
61 return DataFrame._from_mgr(mgr, mgr.axes)