Skip to content

Commit 2aa3b29

Browse files
committed
DOC: add docstring re: #477
1 parent 49dcf7b commit 2aa3b29

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

pandas/core/frame.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -2419,7 +2419,9 @@ def stack(self, level=-1, dropna=True):
24192419

24202420
def unstack(self, level=-1):
24212421
"""
2422-
"Unstack" level from MultiLevel index to produce reshaped DataFrame
2422+
"Unstack" level from MultiLevel index to produce reshaped DataFrame. If
2423+
the index is not a MultiIndex, the output will be a Series (the
2424+
analogue of stack when the columns are not a MultiIndex)
24232425
24242426
Parameters
24252427
----------
@@ -2439,14 +2441,21 @@ def unstack(self, level=-1):
24392441
one 1. 2.
24402442
two 3. 4.
24412443
2442-
>>> s.unstack(level=0)
2444+
>>> df = s.unstack(level=0)
2445+
>>> df
24432446
one two
24442447
a 1. 2.
24452448
b 3. 4.
24462449
2450+
>>> df.unstack()
2451+
one a 1.
2452+
b 3.
2453+
two a 2.
2454+
b 4.
2455+
24472456
Returns
24482457
-------
2449-
unstacked : DataFrame
2458+
unstacked : DataFrame or Series
24502459
"""
24512460
from pandas.core.reshape import unstack
24522461
if isinstance(level, (tuple, list)):

pandas/core/reshape.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def unstack(obj, level):
286286
if isinstance(obj.index, MultiIndex):
287287
return _unstack_frame(obj, level)
288288
else:
289-
return obj.transpose().stack(dropna=False)
289+
return obj.T.stack(dropna=False)
290290
else:
291291
unstacker = _Unstacker(obj.values, obj.index, level=level)
292292
return unstacker.get_result()

pandas/tests/test_frame.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3904,8 +3904,9 @@ def test_stack_unstack(self):
39043904
def test_unstack_to_series(self):
39053905
# check reversibility
39063906
data = self.frame.unstack()
3907+
39073908
self.assertTrue(isinstance(data, Series))
3908-
undo = data.unstack().transpose()
3909+
undo = data.unstack().T
39093910
assert_frame_equal(undo, self.frame)
39103911

39113912
# check NA handling

pandas/tests/test_multilevel.py

+1
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ def test_unstack_bug(self):
502502
'extra': np.arange(6.)})
503503

504504
result = df.groupby(['state','exp','barcode','v']).apply(len)
505+
505506
unstacked = result.unstack()
506507
restacked = unstacked.stack()
507508
assert_series_equal(restacked,

0 commit comments

Comments
 (0)