|
8 | 8 | """
|
9 | 9 | from __future__ import annotations
|
10 | 10 |
|
11 |
| -from collections import defaultdict |
12 |
| -from typing import DefaultDict |
13 |
| - |
14 | 11 | import numpy as np
|
15 | 12 |
|
16 | 13 | from pandas._libs.internals import BlockPlacement
|
17 |
| -from pandas._typing import ( |
18 |
| - ArrayLike, |
19 |
| - Dtype, |
20 |
| -) |
| 14 | +from pandas._typing import Dtype |
21 | 15 |
|
22 | 16 | from pandas.core.dtypes.common import (
|
23 | 17 | is_datetime64tz_dtype,
|
|
26 | 20 |
|
27 | 21 | from pandas.core.arrays import DatetimeArray
|
28 | 22 | from pandas.core.construction import extract_array
|
29 |
| -from pandas.core.indexes.api import Index |
30 | 23 | from pandas.core.internals.blocks import (
|
31 | 24 | Block,
|
32 |
| - CategoricalBlock, |
33 | 25 | DatetimeTZBlock,
|
34 |
| - ExtensionBlock, |
35 | 26 | check_ndim,
|
36 | 27 | ensure_block_shape,
|
37 | 28 | extract_pandas_array,
|
38 | 29 | get_block_type,
|
39 | 30 | maybe_coerce_values,
|
40 |
| - new_block, |
41 |
| -) |
42 |
| -from pandas.core.internals.managers import ( |
43 |
| - BlockManager, |
44 |
| - construction_error, |
45 |
| - multi_blockify, |
46 |
| - simple_blockify, |
47 | 31 | )
|
48 | 32 |
|
49 | 33 |
|
@@ -102,110 +86,3 @@ def maybe_infer_ndim(values, placement: BlockPlacement, ndim: int | None) -> int
|
102 | 86 | else:
|
103 | 87 | ndim = values.ndim
|
104 | 88 | return ndim
|
105 |
| - |
106 |
| - |
107 |
| -def create_block_manager_from_arrays( |
108 |
| - arrays, |
109 |
| - names: Index, |
110 |
| - axes: list[Index], |
111 |
| - consolidate: bool = True, |
112 |
| -) -> BlockManager: |
113 |
| - # Assertions disabled for performance |
114 |
| - # assert isinstance(names, Index) |
115 |
| - # assert isinstance(axes, list) |
116 |
| - # assert all(isinstance(x, Index) for x in axes) |
117 |
| - |
118 |
| - arrays = [extract_array(x, extract_numpy=True) for x in arrays] |
119 |
| - |
120 |
| - try: |
121 |
| - blocks = _form_blocks(arrays, names, axes, consolidate) |
122 |
| - mgr = BlockManager(blocks, axes) |
123 |
| - except ValueError as e: |
124 |
| - raise construction_error(len(arrays), arrays[0].shape, axes, e) |
125 |
| - if consolidate: |
126 |
| - mgr._consolidate_inplace() |
127 |
| - return mgr |
128 |
| - |
129 |
| - |
130 |
| -def _form_blocks( |
131 |
| - arrays: list[ArrayLike], names: Index, axes: list[Index], consolidate: bool |
132 |
| -) -> list[Block]: |
133 |
| - # put "leftover" items in float bucket, where else? |
134 |
| - # generalize? |
135 |
| - items_dict: DefaultDict[str, list] = defaultdict(list) |
136 |
| - extra_locs = [] |
137 |
| - |
138 |
| - names_idx = names |
139 |
| - if names_idx.equals(axes[0]): |
140 |
| - names_indexer = np.arange(len(names_idx)) |
141 |
| - else: |
142 |
| - # Assertion disabled for performance |
143 |
| - # assert names_idx.intersection(axes[0]).is_unique |
144 |
| - names_indexer = names_idx.get_indexer_for(axes[0]) |
145 |
| - |
146 |
| - for i, name_idx in enumerate(names_indexer): |
147 |
| - if name_idx == -1: |
148 |
| - extra_locs.append(i) |
149 |
| - continue |
150 |
| - |
151 |
| - v = arrays[name_idx] |
152 |
| - |
153 |
| - block_type = get_block_type(v) |
154 |
| - items_dict[block_type.__name__].append((i, v)) |
155 |
| - |
156 |
| - blocks: list[Block] = [] |
157 |
| - if len(items_dict["NumericBlock"]): |
158 |
| - numeric_blocks = multi_blockify( |
159 |
| - items_dict["NumericBlock"], consolidate=consolidate |
160 |
| - ) |
161 |
| - blocks.extend(numeric_blocks) |
162 |
| - |
163 |
| - if len(items_dict["DatetimeLikeBlock"]): |
164 |
| - dtlike_blocks = multi_blockify( |
165 |
| - items_dict["DatetimeLikeBlock"], consolidate=consolidate |
166 |
| - ) |
167 |
| - blocks.extend(dtlike_blocks) |
168 |
| - |
169 |
| - if len(items_dict["DatetimeTZBlock"]): |
170 |
| - dttz_blocks = [ |
171 |
| - DatetimeTZBlock( |
172 |
| - ensure_block_shape(extract_array(array), 2), |
173 |
| - placement=BlockPlacement(i), |
174 |
| - ndim=2, |
175 |
| - ) |
176 |
| - for i, array in items_dict["DatetimeTZBlock"] |
177 |
| - ] |
178 |
| - blocks.extend(dttz_blocks) |
179 |
| - |
180 |
| - if len(items_dict["ObjectBlock"]) > 0: |
181 |
| - object_blocks = simple_blockify( |
182 |
| - items_dict["ObjectBlock"], np.object_, consolidate=consolidate |
183 |
| - ) |
184 |
| - blocks.extend(object_blocks) |
185 |
| - |
186 |
| - if len(items_dict["CategoricalBlock"]) > 0: |
187 |
| - cat_blocks = [ |
188 |
| - CategoricalBlock(array, placement=BlockPlacement(i), ndim=2) |
189 |
| - for i, array in items_dict["CategoricalBlock"] |
190 |
| - ] |
191 |
| - blocks.extend(cat_blocks) |
192 |
| - |
193 |
| - if len(items_dict["ExtensionBlock"]): |
194 |
| - external_blocks = [ |
195 |
| - ExtensionBlock(array, placement=BlockPlacement(i), ndim=2) |
196 |
| - for i, array in items_dict["ExtensionBlock"] |
197 |
| - ] |
198 |
| - |
199 |
| - blocks.extend(external_blocks) |
200 |
| - |
201 |
| - if len(extra_locs): |
202 |
| - shape = (len(extra_locs),) + tuple(len(x) for x in axes[1:]) |
203 |
| - |
204 |
| - # empty items -> dtype object |
205 |
| - block_values = np.empty(shape, dtype=object) |
206 |
| - block_values.fill(np.nan) |
207 |
| - |
208 |
| - na_block = new_block(block_values, placement=extra_locs, ndim=2) |
209 |
| - blocks.append(na_block) |
210 |
| - |
211 |
| - return blocks |
0 commit comments