Skip to content

Commit 6376011

Browse files
changhiskhanwesm
authored andcommitted
ENH: value_vars for melt
1 parent 8abd46c commit 6376011

File tree

6 files changed

+38
-13
lines changed

6 files changed

+38
-13
lines changed

RELEASE.rst

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pandas 0.10.0
9191
structures, which should do the right thing on both py2.x and py3.x. (#2224)
9292
- Reduce groupby.apply overhead substantially by low-level manipulation of
9393
internal NumPy arrays in DataFrames (#535)
94+
- Implement ``value_vars`` in ``melt`` and add ``melt`` to pandas namespace (#2412)
9495

9596
**Bug fixes**
9697

doc/source/v0.10.0.txt

+3
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ Updated PyTables Support
111111
import os
112112
os.remove('store.h5')
113113

114+
- Implement ``value_vars`` in ``melt`` and add ``melt`` to pandas namespace (GH2412_)
115+
114116
API changes
115117
~~~~~~~~~~~
116118

@@ -157,3 +159,4 @@ on GitHub for a complete list.
157159
.. _GH2097: https://github.com/pydata/pandas/issues/2097
158160
.. _GH2224: https://github.com/pydata/pandas/issues/2224
159161
.. _GH2431: https://github.com/pydata/pandas/issues/2431
162+
.. _GH2412: https://github.com/pydata/pandas/issues/2412

pandas/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@
4040
from pandas.tools.pivot import pivot_table, crosstab
4141
from pandas.tools.plotting import scatter_matrix, plot_params
4242
from pandas.tools.tile import cut, qcut
43+
from pandas.core.reshape import melt

pandas/core/reshape.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -528,29 +528,34 @@ def melt(frame, id_vars=None, value_vars=None):
528528
b 3 4
529529
c 5 6
530530
531-
>>> melt(df, id_vars=['A'])
531+
>>> melt(df, id_vars=['A'], value_vars=['B'])
532532
A variable value
533533
a B 1
534534
b B 3
535535
c B 5
536-
a C 2
537-
b C 4
538-
c C 6
539536
"""
540537
# TODO: what about the existing index?
538+
if id_vars is not None:
539+
if not isinstance(id_vars, (tuple, list, np.ndarray)):
540+
id_vars = [id_vars]
541+
else:
542+
id_vars = list(id_vars)
543+
else:
544+
id_vars = []
545+
546+
if value_vars is not None:
547+
if not isinstance(value_vars, (tuple, list, np.ndarray)):
548+
value_vars = [value_vars]
549+
frame = frame.ix[:, id_vars + value_vars]
550+
else:
551+
frame = frame.copy()
541552

542553
N, K = frame.shape
554+
K -= len(id_vars)
543555

544556
mdata = {}
545-
546-
if id_vars is not None:
547-
id_vars = list(id_vars)
548-
frame = frame.copy()
549-
K -= len(id_vars)
550-
for col in id_vars:
551-
mdata[col] = np.tile(frame.pop(col).values, K)
552-
else:
553-
id_vars = []
557+
for col in id_vars:
558+
mdata[col] = np.tile(frame.pop(col).values, K)
554559

555560
mcolumns = id_vars + ['variable', 'value']
556561

pandas/tests/test_reshape.py

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ def test_melt():
2727
molten1 = melt(df)
2828
molten2 = melt(df, id_vars=['id1'])
2929
molten3 = melt(df, id_vars=['id1', 'id2'])
30+
molten4 = melt(df, id_vars=['id1', 'id2'],
31+
value_vars='A')
32+
molten5 = melt(df, id_vars=['id1', 'id2'],
33+
value_vars=['A', 'B'])
3034

3135
def test_convert_dummies():
3236
df = DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',

vb_suite/reshape.py

+11
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,14 @@ def unpivot(frame):
4949
unstack_sparse_keyspace = Benchmark('idf.unstack()', setup,
5050
start_date=datetime(2011, 10, 1))
5151

52+
# Melt
53+
54+
setup = common_setup + """
55+
from pandas.core.reshape import melt
56+
df = DataFrame(np.random.randn(10000, 3), columns=['A', 'B', 'C'])
57+
df['id1'] = np.random.randint(0, 10, 10000)
58+
df['id2'] = np.random.randint(100, 1000, 10000)
59+
"""
60+
61+
melt_dataframe = Benchmark("melt(df, id_vars=['id1', 'id2'])", setup,
62+
start_date=datetime(2012, 8, 1))

0 commit comments

Comments
 (0)