Skip to content

Commit ebe483f

Browse files
author
Michael Erickson
committed
nunique for bygroups table now outputs a single df
1 parent 7621bce commit ebe483f

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

swat/cas/table.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3356,10 +3356,20 @@ def _nunique(self, skipna=True):
33563356
:class:`swat.CASResults`
33573357
If By groups are specified.
33583358
'''
3359-
# If we have a groupby table, we will have multiple tables in our CASResults
3359+
# If we have a groupby table, we need to flatten down to one DataFrame
33603360
if self.get_groupby_vars():
3361-
# Just return the CASResults object
3362-
return self._retrieve('simple.distinct', includeMissing=not skipna)
3361+
distinct_results = self._retrieve('simple.distinct', includeMissing=not skipna)
3362+
distinct_results.pop('ByGroupInfo', None)
3363+
# Same bygroups flattening as CASTable.nmiss
3364+
out = pd.concat(list(distinct_results.values()))
3365+
out = out.set_index('Column', append=True)['NDistinct']
3366+
out = out.unstack(level=-1)
3367+
out = out.astype('int64')
3368+
# The columns that match the groupby vars will be useless
3369+
out = out.drop(labels=self.get_groupby_vars(), axis=1)
3370+
if isinstance(out, pd.DataFrame):
3371+
out.columns.name = None
3372+
return out
33633373
else:
33643374
distinct_table = self._retrieve('simple.distinct',
33653375
includeMissing=not skipna)['Distinct']

0 commit comments

Comments
 (0)