Skip to content

Commit fc64ca8

Browse files
BUG: Fixed block placment from reindex (pandas-dev#24149)
Closes pandas-dev#24147
1 parent 67faf6c commit fc64ca8

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,7 @@ Datetimelike
12981298
- Bug in the :class:`Series` repr with period-dtype data missing a space before the data (:issue:`23601`)
12991299
- Bug in :func:`date_range` when decrementing a start date to a past end date by a negative frequency (:issue:`23270`)
13001300
- Bug in :meth:`Series.min` which would return ``NaN`` instead of ``NaT`` when called on a series of ``NaT`` (:issue:`23282`)
1301+
- Bug in :meth:`Series.combine_first` not properly aligning categoricals, so that missing values in ``self`` where not filled by valid values from ``other`` (:issue:`24147`)
13011302
- Bug in :func:`DataFrame.combine` with datetimelike values raising a TypeError (:issue:`23079`)
13021303
- Bug in :func:`date_range` with frequency of ``Day`` or higher where dates sufficiently far in the future could wrap around to the past instead of raising ``OutOfBoundsDatetime`` (:issue:`14187`)
13031304
- Bug in :class:`PeriodIndex` with attribute ``freq.n`` greater than 1 where adding a :class:`DateOffset` object would return incorrect results (:issue:`23215`)

pandas/_libs/internals.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ cdef class BlockPlacement:
6464

6565
return '%s(%r)' % (self.__class__.__name__, v)
6666

67-
__repr__ = __str__
67+
def __repr__(self):
68+
return str(self)
6869

6970
def __len__(self):
7071
cdef:

pandas/core/internals/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1887,7 +1887,7 @@ def take_nd(self, indexer, axis=0, new_mgr_locs=None, fill_tuple=None):
18871887
allow_fill=True)
18881888

18891889
# if we are a 1-dim object, then always place at 0
1890-
if self.ndim == 1:
1890+
if self.ndim == 1 and new_mgr_locs is None:
18911891
new_mgr_locs = [0]
18921892
else:
18931893
if new_mgr_locs is None:

pandas/tests/extension/base/methods.py

+9
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ def test_combine_add(self, data_repeated):
164164
orig_data1._from_sequence([a + val for a in list(orig_data1)]))
165165
self.assert_series_equal(result, expected)
166166

167+
@pytest.mark.xfail(reason="GH-24147", strict=True)
168+
def test_combine_first(self, data):
169+
# https://github.com/pandas-dev/pandas/issues/24147
170+
a = pd.Series(data[:3])
171+
b = pd.Series(data[2:5], index=[2, 3, 4])
172+
result = a.combine_first(b)
173+
expected = pd.Series(data[:5])
174+
self.assert_series_equal(result, expected)
175+
167176
@pytest.mark.parametrize('frame', [True, False])
168177
@pytest.mark.parametrize('periods, indices', [
169178
(-2, [2, 3, 4, -1, -1]),

pandas/tests/internals/test_internals.py

+9
Original file line numberDiff line numberDiff line change
@@ -1283,3 +1283,12 @@ def test_validate_ndim():
12831283

12841284
with pytest.raises(ValueError, match=msg):
12851285
make_block(values, placement, ndim=2)
1286+
1287+
1288+
def test_block_shape():
1289+
idx = pd.Index([0, 1, 2, 3, 4])
1290+
a = pd.Series([1, 2, 3]).reindex(idx)
1291+
b = pd.Series(pd.Categorical([1, 2, 3])).reindex(idx)
1292+
1293+
assert (a._data.blocks[0].mgr_locs.indexer ==
1294+
b._data.blocks[0].mgr_locs.indexer)

0 commit comments

Comments
 (0)