Skip to content

Commit a8d992a

Browse files
authored
REF: remove create_block_manager_from_arrays (#43138)
1 parent f483080 commit a8d992a

File tree

3 files changed

+3
-131
lines changed

3 files changed

+3
-131
lines changed

pandas/core/internals/__init__.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
from pandas.core.internals.api import (
2-
create_block_manager_from_arrays,
3-
make_block,
4-
)
1+
from pandas.core.internals.api import make_block
52
from pandas.core.internals.array_manager import (
63
ArrayManager,
74
SingleArrayManager,
@@ -39,8 +36,7 @@
3936
"SingleBlockManager",
4037
"SingleArrayManager",
4138
"concatenate_managers",
42-
# those two are preserved here for downstream compatibility (GH-33892)
43-
"create_block_manager_from_arrays",
39+
# this is preserved here for downstream compatibility (GH-33892)
4440
"create_block_manager_from_blocks",
4541
]
4642

pandas/core/internals/api.py

+1-124
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,10 @@
88
"""
99
from __future__ import annotations
1010

11-
from collections import defaultdict
12-
from typing import DefaultDict
13-
1411
import numpy as np
1512

1613
from pandas._libs.internals import BlockPlacement
17-
from pandas._typing import (
18-
ArrayLike,
19-
Dtype,
20-
)
14+
from pandas._typing import Dtype
2115

2216
from pandas.core.dtypes.common import (
2317
is_datetime64tz_dtype,
@@ -26,24 +20,14 @@
2620

2721
from pandas.core.arrays import DatetimeArray
2822
from pandas.core.construction import extract_array
29-
from pandas.core.indexes.api import Index
3023
from pandas.core.internals.blocks import (
3124
Block,
32-
CategoricalBlock,
3325
DatetimeTZBlock,
34-
ExtensionBlock,
3526
check_ndim,
3627
ensure_block_shape,
3728
extract_pandas_array,
3829
get_block_type,
3930
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,
4731
)
4832

4933

@@ -102,110 +86,3 @@ def maybe_infer_ndim(values, placement: BlockPlacement, ndim: int | None) -> int
10286
else:
10387
ndim = values.ndim
10488
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

pandas/tests/internals/test_api.py

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def test_namespace():
3939
"SingleBlockManager",
4040
"SingleArrayManager",
4141
"concatenate_managers",
42-
"create_block_manager_from_arrays",
4342
"create_block_manager_from_blocks",
4443
]
4544

0 commit comments

Comments
 (0)