-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Fixed handling of non-list value_vars in melt #15351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
33728de
d4c5da3
7406222
455a310
70d7256
129d531
20159c1
e907135
3038f64
a2f2510
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,38 @@ def test_value_vars(self): | |
columns=['id1', 'id2', 'variable', 'value']) | ||
tm.assert_frame_equal(result4, expected4) | ||
|
||
def test_value_vars_types(self): | ||
# GH 15348 | ||
expected = DataFrame({'id1': self.df['id1'].tolist() * 2, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add the issue number as a comment |
||
'id2': self.df['id2'].tolist() * 2, | ||
'variable': ['A'] * 10 + ['B'] * 10, | ||
'value': (self.df['A'].tolist() + | ||
self.df['B'].tolist())}, | ||
columns=['id1', 'id2', 'variable', 'value']) | ||
|
||
for type_ in (tuple, list, np.array): | ||
result = melt(self.df, id_vars=['id1', 'id2'], | ||
value_vars=type_(('A', 'B'))) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_id_and_value_vars_types_with_multiindex(self): | ||
expected = DataFrame({ | ||
('A', 'a'): self.df1[('A', 'a')], | ||
'CAP': ['B'] * len(self.df1), | ||
'low': ['b'] * len(self.df1), | ||
'value': self.df1[('B', 'b')], | ||
}, columns=[('A', 'a'), 'CAP', 'low', 'value']) | ||
|
||
for id_vars in ([('A', 'a')], ('A', 'a')): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. separate this in to 2 parts, the success part (e.g. both lists), and the failing parts (the other 3 possibilites) |
||
for value_vars in ([('B', 'b')], ('B', 'b')): | ||
if isinstance(id_vars, list) and isinstance(value_vars, list): | ||
result = melt(self.df1, id_vars=id_vars, | ||
value_vars=value_vars) | ||
tm.assert_frame_equal(result, expected) | ||
else: | ||
with self.assertRaisesRegex(TypeError, r'MultiIndex'): | ||
melt(self.df1, id_vars=id_vars, value_vars=value_vars) | ||
|
||
def test_custom_var_name(self): | ||
result5 = melt(self.df, var_name=self.var_name) | ||
self.assertEqual(result5.columns.tolist(), ['var', 'value']) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these should be ValueError (both errors)