@@ -12110,6 +12110,70 @@ def test_stack_ints(self):
12110
12110
df_named.stack(level=1).stack(level=1)
12111
12111
)
12112
12112
12113
+ def test_stack_mixed_levels(self):
12114
+ columns = MultiIndex.from_tuples(
12115
+ [('A', 'cat', 'long'), ('B', 'cat', 'long'),
12116
+ ('A', 'dog', 'short'), ('B', 'dog', 'short')],
12117
+ names=['exp', 'animal', 'hair_length']
12118
+ )
12119
+ df = DataFrame(randn(4, 4), columns=columns)
12120
+
12121
+ animal_hair_stacked = df.stack(level=['animal', 'hair_length'])
12122
+ exp_hair_stacked = df.stack(level=['exp', 'hair_length'])
12123
+
12124
+ # GH #8584: Need to check that stacking works when a number
12125
+ # is passed that is both a level name and in the range of
12126
+ # the level numbers
12127
+ df2 = df.copy()
12128
+ df2.columns.names = ['exp', 'animal', 1]
12129
+ assert_frame_equal(df2.stack(level=['animal', 1]),
12130
+ animal_hair_stacked, check_names=False)
12131
+ assert_frame_equal(df2.stack(level=['exp', 1]),
12132
+ exp_hair_stacked, check_names=False)
12133
+
12134
+ # When mixed types are passed and the ints are not level
12135
+ # names, raise
12136
+ self.assertRaises(ValueError, df2.stack, level=['animal', 0])
12137
+
12138
+ # GH #8584: Having 0 in the level names could raise a
12139
+ # strange error about lexsort depth
12140
+ df3 = df.copy()
12141
+ df3.columns.names = ['exp', 'animal', 0]
12142
+ assert_frame_equal(df3.stack(level=['animal', 0]),
12143
+ animal_hair_stacked, check_names=False)
12144
+
12145
+ def test_stack_int_level_names(self):
12146
+ columns = MultiIndex.from_tuples(
12147
+ [('A', 'cat', 'long'), ('B', 'cat', 'long'),
12148
+ ('A', 'dog', 'short'), ('B', 'dog', 'short')],
12149
+ names=['exp', 'animal', 'hair_length']
12150
+ )
12151
+ df = DataFrame(randn(4, 4), columns=columns)
12152
+
12153
+ exp_animal_stacked = df.stack(level=['exp', 'animal'])
12154
+ animal_hair_stacked = df.stack(level=['animal', 'hair_length'])
12155
+ exp_hair_stacked = df.stack(level=['exp', 'hair_length'])
12156
+
12157
+ df2 = df.copy()
12158
+ df2.columns.names = [0, 1, 2]
12159
+ assert_frame_equal(df2.stack(level=[1, 2]), animal_hair_stacked,
12160
+ check_names=False )
12161
+ assert_frame_equal(df2.stack(level=[0, 1]), exp_animal_stacked,
12162
+ check_names=False)
12163
+ assert_frame_equal(df2.stack(level=[0, 2]), exp_hair_stacked,
12164
+ check_names=False)
12165
+
12166
+ # Out-of-order int column names
12167
+ df3 = df.copy()
12168
+ df3.columns.names = [2, 0, 1]
12169
+ assert_frame_equal(df3.stack(level=[0, 1]), animal_hair_stacked,
12170
+ check_names=False)
12171
+ assert_frame_equal(df3.stack(level=[2, 0]), exp_animal_stacked,
12172
+ check_names=False)
12173
+ assert_frame_equal(df3.stack(level=[2, 1]), exp_hair_stacked,
12174
+ check_names=False)
12175
+
12176
+
12113
12177
def test_unstack_bool(self):
12114
12178
df = DataFrame([False, False],
12115
12179
index=MultiIndex.from_arrays([['a', 'b'], ['c', 'l']]),
0 commit comments