Skip to content

Commit 141df57

Browse files
committed
ENH: more TimeGrouper issues in apply, close #1057
1 parent d022e04 commit 141df57

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

pandas/core/groupby.py

+21-18
Original file line numberDiff line numberDiff line change
@@ -489,11 +489,9 @@ def shape(self):
489489
def __iter__(self):
490490
return iter(self.indices)
491491

492-
def numkeys(self):
493-
try:
494-
return len(self.groupings)
495-
except:
496-
return 1
492+
@property
493+
def nkeys(self):
494+
return len(self.groupings)
497495

498496
def get_iterator(self, data, axis=0):
499497
"""
@@ -601,6 +599,10 @@ def _get_compressed_labels(self):
601599
def _overflow_possible(self):
602600
return _int64_overflow_possible(self.shape)
603601

602+
@cache_readonly
603+
def ngroups(self):
604+
return len(self.result_index)
605+
604606
@cache_readonly
605607
def result_index(self):
606608
recons = self.get_group_levels()
@@ -830,6 +832,10 @@ def __init__(self, bins, binlabels):
830832
self.bins = bins
831833
self.binlabels = binlabels
832834

835+
@property
836+
def nkeys(self):
837+
return 1
838+
833839
def get_iterator(self, data, axis=0):
834840
"""
835841
Groupby iterator
@@ -1271,7 +1277,7 @@ def aggregate(self, func_or_funcs, *args, **kwargs):
12711277
if cyfunc and not args and not kwargs:
12721278
return getattr(self, cyfunc)()
12731279

1274-
if self.grouper.numkeys() > 1:
1280+
if self.grouper.nkeys > 1:
12751281
return self._python_agg_general(func_or_funcs, *args, **kwargs)
12761282

12771283
try:
@@ -1312,18 +1318,15 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
13121318
if len(keys) == 0:
13131319
return Series([])
13141320

1315-
key_names = self.grouper.names
1316-
13171321
def _get_index():
1318-
if self.grouper.numkeys() > 1:
1319-
index = MultiIndex.from_tuples(keys, names=key_names)
1322+
if self.grouper.nkeys > 1:
1323+
index = MultiIndex.from_tuples(keys, names=self.grouper.names)
13201324
else:
1321-
ping = self.grouper.groupings[0]
1322-
if len(keys) == ping.ngroups:
1323-
index = ping.group_index
1324-
index.name = key_names[0]
1325-
else:
1326-
index = Index(keys, name=key_names[0])
1325+
index = Index(keys, name=self.grouper.names[0])
1326+
# if len(keys) == self.grouper.ngroups:
1327+
# index = self.grouper.result_index
1328+
# else:
1329+
# index = Index(keys, name=self.grouper.names[0])
13271330
return index
13281331

13291332
if isinstance(values[0], Series):
@@ -1524,7 +1527,7 @@ def aggregate(self, arg, *args, **kwargs):
15241527
if cyfunc and not args and not kwargs:
15251528
return getattr(self, cyfunc)()
15261529

1527-
if self.grouper.numkeys() > 1:
1530+
if self.grouper.nkeys > 1:
15281531
return self._python_agg_general(arg, *args, **kwargs)
15291532
else:
15301533
result = self._aggregate_generic(arg, *args, **kwargs)
@@ -1568,7 +1571,7 @@ def _aggregate_multiple_funcs(self, arg):
15681571
return result
15691572

15701573
def _aggregate_generic(self, func, *args, **kwargs):
1571-
assert(self.grouper.numkeys() == 1)
1574+
assert(self.grouper.nkeys == 1)
15721575

15731576
axis = self.axis
15741577
obj = self._obj_with_exclusions

pandas/tests/test_timeseries.py

+11
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,17 @@ def test_apply(self):
975975
expected.index = expected.index.droplevel(0)
976976
assert_series_equal(applied, expected)
977977

978+
def test_count(self):
979+
self.ts[::3] = np.nan
980+
981+
grouper = TimeGrouper('A', label='right', closed='right')
982+
result = self.ts.convert('A', how='count')
983+
984+
expected = self.ts.groupby(lambda x: x.year).count()
985+
expected.index = result.index
986+
987+
assert_series_equal(result, expected)
988+
978989
def test_numpy_reduction(self):
979990
result = self.ts.convert('A', how='prod', closed='right')
980991

0 commit comments

Comments
 (0)