Skip to content

Commit d4c5da3

Browse files
committed
Improved type checking and tests. Added whatsnew note.
1 parent 33728de commit d4c5da3

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

doc/source/whatsnew/v0.20.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,4 @@ Bug Fixes
517517

518518
- Bug in ``DataFrame.boxplot`` where ``fontsize`` was not applied to the tick labels on both axes (:issue:`15108`)
519519
- Bug in ``Series.replace`` and ``DataFrame.replace`` which failed on empty replacement dicts (:issue:`15289`)
520+
- Bug in ``pd.melt`` where passing a tuple value for ``value_vars`` caused a TypeError (:issue:`15348`)

pandas/core/reshape.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,15 +761,15 @@ def melt(frame, id_vars=None, value_vars=None, var_name=None,
761761
"""
762762
# TODO: what about the existing index?
763763
if id_vars is not None:
764-
if not isinstance(id_vars, (tuple, list, np.ndarray)):
764+
if not is_list_like(id_vars):
765765
id_vars = [id_vars]
766766
else:
767767
id_vars = list(id_vars)
768768
else:
769769
id_vars = []
770770

771771
if value_vars is not None:
772-
if not isinstance(value_vars, (tuple, list, np.ndarray)):
772+
if not is_list_like(value_vars):
773773
value_vars = [value_vars]
774774
else:
775775
value_vars = list(value_vars)

pandas/tests/test_reshape.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,17 @@ def test_value_vars(self):
5757
tm.assert_frame_equal(result4, expected4)
5858

5959
def test_value_vars_types(self):
60-
result_with_tuple = melt(self.df, id_vars=['id1', 'id2'], value_vars=('A', 'B'))
61-
result_with_array = melt(self.df, id_vars=['id1', 'id2'], value_vars=np.array(['A', 'B']))
62-
6360
expected = DataFrame({'id1': self.df['id1'].tolist() * 2,
6461
'id2': self.df['id2'].tolist() * 2,
6562
'variable': ['A'] * 10 + ['B'] * 10,
6663
'value': (self.df['A'].tolist() +
6764
self.df['B'].tolist())},
6865
columns=['id1', 'id2', 'variable', 'value'])
6966

70-
tm.assert_frame_equal(result_with_tuple, expected)
71-
tm.assert_frame_equal(result_with_array, expected)
67+
for type_ in (tuple, list, np.array):
68+
result = melt(self.df, id_vars=['id1', 'id2'],
69+
value_vars=type_(('A', 'B')))
70+
tm.assert_frame_equal(result, expected)
7271

7372
def test_custom_var_name(self):
7473
result5 = melt(self.df, var_name=self.var_name)

0 commit comments

Comments
 (0)