@@ -1265,7 +1265,7 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
1265
1265
1266
1266
v = values [0 ]
1267
1267
1268
- if isinstance (v , (np .ndarray , Index , Series )):
1268
+ if isinstance (v , (np .ndarray , Index , Series )) or not self . as_index :
1269
1269
if isinstance (v , Series ):
1270
1270
applied_index = self ._selected_obj ._get_axis (self .axis )
1271
1271
all_indexed_same = all_indexes_same ([x .index for x in values ])
@@ -1341,6 +1341,11 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
1341
1341
result = self .obj ._constructor (
1342
1342
stacked_values .T , index = v .index , columns = key_index
1343
1343
)
1344
+ elif not self .as_index :
1345
+ # We add grouping column below, so create a frame here
1346
+ result = DataFrame (
1347
+ values , index = key_index , columns = [self ._selection ]
1348
+ )
1344
1349
else :
1345
1350
# GH#1738: values is list of arrays of unequal lengths
1346
1351
# fall through to the outer else clause
@@ -1358,6 +1363,9 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
1358
1363
else :
1359
1364
result = result ._convert (datetime = True )
1360
1365
1366
+ if not self .as_index :
1367
+ self ._insert_inaxis_grouper_inplace (result )
1368
+
1361
1369
return self ._reindex_output (result )
1362
1370
1363
1371
# values are not series or array-like but scalars
@@ -1700,9 +1708,11 @@ def _insert_inaxis_grouper_inplace(self, result):
1700
1708
),
1701
1709
)
1702
1710
)
1703
-
1711
+ columns = result . columns
1704
1712
for name , lev , in_axis in izip :
1705
- if in_axis :
1713
+ # GH #28549
1714
+ # When using .apply(-), name will be in columns already
1715
+ if in_axis and name not in columns :
1706
1716
result .insert (0 , name , lev )
1707
1717
1708
1718
def _wrap_aggregated_output (
@@ -1852,11 +1862,11 @@ def nunique(self, dropna: bool = True):
1852
1862
5 ham 5 y
1853
1863
1854
1864
>>> df.groupby('id').nunique()
1855
- id value1 value2
1865
+ value1 value2
1856
1866
id
1857
- egg 1 1 1
1858
- ham 1 1 2
1859
- spam 1 2 1
1867
+ egg 1 1
1868
+ ham 1 2
1869
+ spam 2 1
1860
1870
1861
1871
Check for rows with the same id but conflicting values:
1862
1872
@@ -1867,7 +1877,7 @@ def nunique(self, dropna: bool = True):
1867
1877
4 ham 5 x
1868
1878
5 ham 5 y
1869
1879
"""
1870
- obj = self ._selected_obj
1880
+ obj = self ._obj_with_exclusions
1871
1881
1872
1882
def groupby_series (obj , col = None ):
1873
1883
return SeriesGroupBy (obj , selection = col , grouper = self .grouper ).nunique (
@@ -1898,6 +1908,9 @@ def groupby_series(obj, col=None):
1898
1908
1899
1909
if not self .as_index :
1900
1910
results .index = ibase .default_index (len (results ))
1911
+ if results .ndim == 1 :
1912
+ results = results .to_frame ()
1913
+ self ._insert_inaxis_grouper_inplace (results )
1901
1914
return results
1902
1915
1903
1916
boxplot = boxplot_frame_groupby
0 commit comments