Skip to content

Commit 48a5853

Browse files
authored
REF: preserve Index dtype in BlockManager._combine (#41354)
1 parent 3cce96f commit 48a5853

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

pandas/core/internals/array_manager.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,6 @@ def apply(
274274
else:
275275
new_axes = self._axes
276276

277-
if len(result_arrays) == 0:
278-
return self.make_empty(new_axes)
279-
280277
# error: Argument 1 to "ArrayManager" has incompatible type "List[ndarray]";
281278
# expected "List[Union[ndarray, ExtensionArray]]"
282279
return type(self)(result_arrays, new_axes) # type: ignore[arg-type]
@@ -487,7 +484,7 @@ def _get_data_subset(self: T, predicate: Callable) -> T:
487484
indices = [i for i, arr in enumerate(self.arrays) if predicate(arr)]
488485
arrays = [self.arrays[i] for i in indices]
489486
# TODO copy?
490-
new_axes = [self._axes[0], self._axes[1][np.array(indices, dtype="int64")]]
487+
new_axes = [self._axes[0], self._axes[1][np.array(indices, dtype="intp")]]
491488
return type(self)(arrays, new_axes, verify_integrity=False)
492489

493490
def get_bool_data(self: T, copy: bool = False) -> T:
@@ -696,7 +693,6 @@ def _equal_values(self, other) -> bool:
696693
return True
697694

698695
# TODO
699-
# equals
700696
# to_dict
701697

702698

pandas/core/internals/managers.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,6 @@ def apply(
345345
if ignore_failures:
346346
return self._combine(result_blocks)
347347

348-
if len(result_blocks) == 0:
349-
return self.make_empty(self.axes)
350-
351348
return type(self).from_blocks(result_blocks, self.axes)
352349

353350
def where(self: T, other, cond, align: bool, errors: str) -> T:
@@ -532,6 +529,13 @@ def _combine(
532529
) -> T:
533530
""" return a new manager with the blocks """
534531
if len(blocks) == 0:
532+
if self.ndim == 2:
533+
# retain our own Index dtype
534+
if index is not None:
535+
axes = [self.items[:0], index]
536+
else:
537+
axes = [self.items[:0]] + self.axes[1:]
538+
return self.make_empty(axes)
535539
return self.make_empty()
536540

537541
# FIXME: optimization potential
@@ -1233,7 +1237,7 @@ def grouped_reduce(self: T, func: Callable, ignore_failures: bool = False) -> T:
12331237
index = Index(range(result_blocks[0].values.shape[-1]))
12341238

12351239
if ignore_failures:
1236-
return self._combine(result_blocks, index=index)
1240+
return self._combine(result_blocks, copy=False, index=index)
12371241

12381242
return type(self).from_blocks(result_blocks, [self.axes[0], index])
12391243

@@ -1270,7 +1274,7 @@ def reduce(
12701274
new_mgr = self._combine(res_blocks, copy=False, index=index)
12711275
else:
12721276
indexer = []
1273-
new_mgr = type(self).from_blocks([], [Index([]), index])
1277+
new_mgr = type(self).from_blocks([], [self.items[:0], index])
12741278
else:
12751279
indexer = np.arange(self.shape[0])
12761280
new_mgr = type(self).from_blocks(res_blocks, [self.items, index])

pandas/tests/generic/test_generic.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def test_rename(self):
8585

8686
# multiple axes at once
8787

88-
def test_get_numeric_data(self, using_array_manager):
88+
def test_get_numeric_data(self):
8989

9090
n = 4
9191
kwargs = {
@@ -100,9 +100,9 @@ def test_get_numeric_data(self, using_array_manager):
100100
# non-inclusion
101101
result = o._get_bool_data()
102102
expected = self._construct(n, value="empty", **kwargs)
103-
if using_array_manager and isinstance(o, DataFrame):
104-
# INFO(ArrayManager) preserve the dtype of the columns Index
105-
expected.columns = expected.columns.astype("int64")
103+
if isinstance(o, DataFrame):
104+
# preserve columns dtype
105+
expected.columns = o.columns[:0]
106106
self._compare(result, expected)
107107

108108
# get the bool data

0 commit comments

Comments
 (0)