Skip to content

Commit 1068a44

Browse files
committed
BUG: Raise a TypeError when trying to assign with a rhs of a multi-index of differeing levels (GH3738)
1 parent 65a9976 commit 1068a44

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

doc/source/indexing.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -1742,13 +1742,13 @@ As usual, **both sides** of the slicers are included as this is label indexing.
17421742

17431743
.. code-block:: python
17441744
1745-
df.loc[(slice('A1','A3'),.....,:]
1745+
df.loc[(slice('A1','A3'),.....),:]
17461746
17471747
rather than this:
17481748

17491749
.. code-block:: python
17501750
1751-
df.loc[(slice('A1','A3'),.....]
1751+
df.loc[(slice('A1','A3'),.....)]
17521752
17531753
.. warning::
17541754

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ Bug Fixes
9595
- Issue with groupby ``agg`` with a single function and a a mixed-type frame (:issue:`6337`)
9696
- Bug in ``DataFrame.replace()`` when passing a non- ``bool``
9797
``to_replace`` argument (:issue:`6332`)
98+
- Raise when trying to align on different levels of a multi-index assignment (:issue:`3738`)
9899

99100
pandas 0.13.1
100101
-------------

doc/source/v0.14.0.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ See also issues (:issue:`6134`, :issue:`4036`, :issue:`3057`, :issue:`2598`, :is
5656

5757
.. code-block:: python
5858

59-
df.loc[(slice('A1','A3'),.....,:]
59+
df.loc[(slice('A1','A3'),.....),:]
6060

6161
rather than this:
6262

6363
.. code-block:: python
6464

65-
df.loc[(slice('A1','A3'),.....]
65+
df.loc[(slice('A1','A3'),.....)]
6666

6767
.. warning::
6868

pandas/core/indexing.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,14 @@ def _align_frame(self, indexer, df):
591591
if df.index.equals(ax):
592592
val = df.copy().values
593593
else:
594-
val = df.reindex(ax).values
594+
595+
# we have a multi-index and are trying to align
596+
# with a particular, level GH3738
597+
if isinstance(ax, MultiIndex) and isinstance(
598+
df.index, MultiIndex) and ax.nlevels != df.index.nlevels:
599+
raise TypeError("cannot align on a multi-index with out specifying the join levels")
600+
601+
val = df.reindex(index=ax).values
595602
return val
596603

597604
elif np.isscalar(indexer) and not is_frame:

pandas/tests/test_indexing.py

+22
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,28 @@ def f():
12851285
expected.iloc[[0,3],[1,3]] *= expected.iloc[[0,3],[1,3]]
12861286
assert_frame_equal(df, expected)
12871287

1288+
def test_multiindex_setitem(self):
1289+
1290+
# GH 3738
1291+
# setting with a multi-index right hand side
1292+
arrays = [np.array(['bar', 'bar', 'baz', 'qux', 'qux', 'bar']),
1293+
np.array(['one', 'two', 'one', 'one', 'two', 'one']),
1294+
np.arange(0, 6, 1)]
1295+
1296+
df_orig = pd.DataFrame(np.random.randn(6, 3),
1297+
index=arrays,
1298+
columns=['A', 'B', 'C']).sort_index()
1299+
1300+
expected = df_orig.loc[['bar']]*2
1301+
df = df_orig.copy()
1302+
df.loc[['bar']] *= 2
1303+
assert_frame_equal(df.loc[['bar']],expected)
1304+
1305+
# raise because these have differing levels
1306+
def f():
1307+
df.loc['bar'] *= 2
1308+
self.assertRaises(TypeError, f)
1309+
12881310
def test_getitem_multiindex(self):
12891311

12901312
# GH 5725

0 commit comments

Comments
 (0)