Skip to content

Commit f545476

Browse files
committed
Initial wiring and working func, save all NaNs
1 parent 0bde3f9 commit f545476

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

pandas/_libs/groupby_helper.pxi.in

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -794,24 +794,19 @@ def group_cummax_{{name}}(ndarray[{{dest_type2}}, ndim=2] out,
794794

795795
@cython.boundscheck(False)
796796
@cython.wraparound(False)
797-
def group_any(ndarray[uint8_t, ndim=2] out,
798-
ndarray[int64_t] counts,
799-
ndarray[:, :] values,
797+
def group_any(ndarray[int64_t] out,
798+
ndarray[int64_t] mask,
800799
ndarray[int64_t] labels,
801800
bint skipna):
802801
cdef:
803-
Py_ssize_t i, N
804-
ndarray[uint8_t] mask
805-
806-
N, _ = (<object> labels).shape
807-
808-
out = np.zeros_like(out)
809-
mask = values[:, 0].astype(np.bool)
802+
Py_ssize_t i, N=len(labels)
803+
int64_t lab
810804

811-
for i in range(N):
812-
lab = labels[i]
813-
if lab < 0:
814-
continue
805+
with nogil:
806+
for i in range(N):
807+
lab = labels[i]
808+
if lab < 0:
809+
continue
815810

816-
if mask[lab]:
817-
out[lab, 0] = 1
811+
if mask[i]:
812+
out[lab] = 1

pandas/core/groupby.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,26 @@ class GroupBy(_GroupBy):
12191219
"""
12201220
_apply_whitelist = _common_apply_whitelist
12211221

1222+
@Substitution(name='groupby')
1223+
@Appender(_doc_template)
1224+
def any(self, skipna=True):
1225+
"""Returns True if any value in the group is truthful, else False"""
1226+
obj = self._obj_with_exclusions
1227+
1228+
labels, _, _ = self.grouper.group_info
1229+
1230+
output = collections.OrderedDict()
1231+
for name, obj in self._iterate_slices():
1232+
result = np.zeros(self.ngroups, dtype=np.int64)
1233+
if obj.dtype is np.object:
1234+
mask = np.array(bool(x) for x in obj.values)
1235+
else:
1236+
mask = obj.values.astype(np.bool)
1237+
libgroupby.group_any(result, mask.astype(np.int64), labels, skipna)
1238+
output[name] = result.astype(np.bool)
1239+
1240+
return self._wrap_aggregated_output(output)
1241+
12221242
@Substitution(name='groupby')
12231243
@Appender(_doc_template)
12241244
def count(self):

0 commit comments

Comments
 (0)