@@ -169,7 +169,7 @@ class GroupBy(object):
169
169
170
170
def __init__ (self , obj , keys = None , axis = 0 , level = None ,
171
171
grouper = None , exclusions = None , selection = None , as_index = True ,
172
- sort = True , group_keys = True ):
172
+ sort = True , group_keys = True , squeeze = False ):
173
173
self ._selection = selection
174
174
175
175
if isinstance (obj , NDFrame ):
@@ -189,6 +189,7 @@ def __init__(self, obj, keys=None, axis=0, level=None,
189
189
self .keys = keys
190
190
self .sort = sort
191
191
self .group_keys = group_keys
192
+ self .squeeze = squeeze
192
193
193
194
if grouper is None :
194
195
grouper , exclusions = _get_grouper (obj , keys , axis = axis ,
@@ -1841,15 +1842,22 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
1841
1842
all_indexed_same = _all_indexes_same ([x .index for x in values ])
1842
1843
singular_series = len (values ) == 1 and applied_index .nlevels == 1
1843
1844
1844
- # assign the name to this series
1845
- if singular_series :
1846
- values [ 0 ]. name = keys [ 0 ]
1845
+ # GH3596
1846
+ # provide a reduction (Frame -> Series) if groups are unique
1847
+ if self . squeeze :
1847
1848
1848
- # GH2893
1849
- # we have series in the values array, we want to produce a series:
1850
- # if any of the sub-series are not indexed the same
1851
- # OR we don't have a multi-index and we have only a single values
1852
- if singular_series or not all_indexed_same :
1849
+ # assign the name to this series
1850
+ if singular_series :
1851
+ values [0 ].name = keys [0 ]
1852
+
1853
+ # GH2893
1854
+ # we have series in the values array, we want to produce a series:
1855
+ # if any of the sub-series are not indexed the same
1856
+ # OR we don't have a multi-index and we have only a single values
1857
+ return self ._concat_objects (keys , values ,
1858
+ not_indexed_same = not_indexed_same )
1859
+
1860
+ if not all_indexed_same :
1853
1861
return self ._concat_objects (keys , values ,
1854
1862
not_indexed_same = not_indexed_same )
1855
1863
0 commit comments