Skip to content

Commit 79ffc96

Browse files
phofljreback
andauthored
BUG: align not adding levels only on one side if intersection is equal (#45564)
Co-authored-by: Jeff Reback <[email protected]>
1 parent 15a06d3 commit 79ffc96

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ Indexing
316316
^^^^^^^^
317317
- Bug in :meth:`loc.__getitem__` with a list of keys causing an internal inconsistency that could lead to a disconnect between ``frame.at[x, y]`` vs ``frame[y].loc[x]`` (:issue:`22372`)
318318
- Bug in :meth:`DataFrame.iloc` where indexing a single row on a :class:`DataFrame` with a single ExtensionDtype column gave a copy instead of a view on the underlying data (:issue:`45241`)
319+
- Bug in :meth:`Series.align` does not create :class:`MultiIndex` with union of levels when both MultiIndexes intersections are identical (:issue:`45224`)
319320
- Bug in setting a NA value (``None`` or ``np.nan``) into a :class:`Series` with int-based :class:`IntervalDtype` incorrectly casting to object dtype instead of a float-based :class:`IntervalDtype` (:issue:`45568`)
320321
- Bug in :meth:`Series.__setitem__` with a non-integer :class:`Index` when using an integer key to set a value that cannot be set inplace where a ``ValueError`` was raised instead of casting to a common dtype (:issue:`45070`)
321322
- Bug in :meth:`Series.__setitem__` when setting incompatible values into a ``PeriodDtype`` or ``IntervalDtype`` :class:`Series` raising when indexing with a boolean mask but coercing when indexing with otherwise-equivalent indexers; these now consistently coerce, along with :meth:`Series.mask` and :meth:`Series.where` (:issue:`45768`)

pandas/core/series.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -4502,7 +4502,9 @@ def _reindex_indexer(
45024502
) -> Series:
45034503
# Note: new_index is None iff indexer is None
45044504
# if not None, indexer is np.intp
4505-
if indexer is None:
4505+
if indexer is None and (
4506+
new_index is None or new_index.names == self.index.names
4507+
):
45064508
if copy:
45074509
return self.copy()
45084510
return self

pandas/tests/series/methods/test_align.py

+38
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,41 @@ def test_align_periodindex(join_type):
184184

185185
# TODO: assert something?
186186
ts.align(ts[::2], join=join_type)
187+
188+
189+
def test_align_left_fewer_levels():
190+
# GH#45224
191+
left = Series([2], index=pd.MultiIndex.from_tuples([(1, 3)], names=["a", "c"]))
192+
right = Series(
193+
[1], index=pd.MultiIndex.from_tuples([(1, 2, 3)], names=["a", "b", "c"])
194+
)
195+
result_left, result_right = left.align(right)
196+
197+
expected_right = Series(
198+
[1], index=pd.MultiIndex.from_tuples([(1, 3, 2)], names=["a", "c", "b"])
199+
)
200+
expected_left = Series(
201+
[2], index=pd.MultiIndex.from_tuples([(1, 3, 2)], names=["a", "c", "b"])
202+
)
203+
tm.assert_series_equal(result_left, expected_left)
204+
tm.assert_series_equal(result_right, expected_right)
205+
206+
207+
def test_align_left_different_named_levels():
208+
# GH#45224
209+
left = Series(
210+
[2], index=pd.MultiIndex.from_tuples([(1, 4, 3)], names=["a", "d", "c"])
211+
)
212+
right = Series(
213+
[1], index=pd.MultiIndex.from_tuples([(1, 2, 3)], names=["a", "b", "c"])
214+
)
215+
result_left, result_right = left.align(right)
216+
217+
expected_left = Series(
218+
[2], index=pd.MultiIndex.from_tuples([(1, 3, 4, 2)], names=["a", "c", "d", "b"])
219+
)
220+
expected_right = Series(
221+
[1], index=pd.MultiIndex.from_tuples([(1, 3, 4, 2)], names=["a", "c", "d", "b"])
222+
)
223+
tm.assert_series_equal(result_left, expected_left)
224+
tm.assert_series_equal(result_right, expected_right)

pandas/tests/series/test_arithmetic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -885,8 +885,8 @@ def test_series_varied_multiindex_alignment():
885885
expected = Series(
886886
[1000, 2001, 3002, 4003],
887887
index=pd.MultiIndex.from_tuples(
888-
[("a", "x", 1), ("a", "x", 2), ("a", "y", 1), ("a", "y", 2)],
889-
names=["ab", "xy", "num"],
888+
[("x", 1, "a"), ("x", 2, "a"), ("y", 1, "a"), ("y", 2, "a")],
889+
names=["xy", "num", "ab"],
890890
),
891891
)
892892
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)