Skip to content

Commit 073013f

Browse files
committed
BUG: align not adding levels only on one side if intersection is equal
1 parent 3418679 commit 073013f

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
@@ -210,6 +210,7 @@ Indexing
210210
^^^^^^^^
211211
- 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`)
212212
- 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`)
213+
- Bug in :meth:`Series.align` does not create :class:`MultiIndex` with union of levels when both MultiIndexes intersections are identical (:issue:`45224`)
213214
- 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 insead of casting to a common dtype (:issue:`45070`)
214215
- Bug when setting a value too large for a :class:`Series` dtype failing to coerce to a common type (:issue:`26049`, :issue:`32878`)
215216
-

pandas/core/series.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -4467,7 +4467,9 @@ def _reindex_indexer(
44674467
) -> Series:
44684468
# Note: new_index is None iff indexer is None
44694469
# if not None, indexer is np.intp
4470-
if indexer is None:
4470+
if indexer is None and (
4471+
new_index is None or new_index.names == self.index.names
4472+
):
44714473
if copy:
44724474
return self.copy()
44734475
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
@@ -887,8 +887,8 @@ def test_series_varied_multiindex_alignment():
887887
expected = Series(
888888
[1000, 2001, 3002, 4003],
889889
index=pd.MultiIndex.from_tuples(
890-
[("a", "x", 1), ("a", "x", 2), ("a", "y", 1), ("a", "y", 2)],
891-
names=["ab", "xy", "num"],
890+
[("x", 1, "a"), ("x", 2, "a"), ("y", 1, "a"), ("y", 2, "a")],
891+
names=["xy", "num", "ab"],
892892
),
893893
)
894894
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)