Skip to content

Commit b50a2e2

Browse files
PERF: avoid zeros_like in groupby.pyx (#40194)
1 parent 38b5e4a commit b50a2e2

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

pandas/_libs/groupby.pyx

+8-6
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,9 @@ def _group_add(complexfloating_t[:, :] out,
497497
raise ValueError("len(index) != len(labels)")
498498

499499
nobs = np.zeros((<object>out).shape, dtype=np.int64)
500-
sumx = np.zeros_like(out)
501-
compensation = np.zeros_like(out)
500+
# the below is equivalent to `np.zeros_like(out)` but faster
501+
sumx = np.zeros((<object>out).shape, dtype=(<object>out).base.dtype)
502+
compensation = np.zeros((<object>out).shape, dtype=(<object>out).base.dtype)
502503

503504
N, K = (<object>values).shape
504505

@@ -555,7 +556,7 @@ def _group_prod(floating[:, :] out,
555556
raise ValueError("len(index) != len(labels)")
556557

557558
nobs = np.zeros((<object>out).shape, dtype=np.int64)
558-
prodx = np.ones_like(out)
559+
prodx = np.ones((<object>out).shape, dtype=(<object>out).base.dtype)
559560

560561
N, K = (<object>values).shape
561562

@@ -608,7 +609,7 @@ def _group_var(floating[:, :] out,
608609
raise ValueError("len(index) != len(labels)")
609610

610611
nobs = np.zeros((<object>out).shape, dtype=np.int64)
611-
mean = np.zeros_like(out)
612+
mean = np.zeros((<object>out).shape, dtype=(<object>out).base.dtype)
612613

613614
N, K = (<object>values).shape
614615

@@ -665,8 +666,9 @@ def _group_mean(floating[:, :] out,
665666
raise ValueError("len(index) != len(labels)")
666667

667668
nobs = np.zeros((<object>out).shape, dtype=np.int64)
668-
sumx = np.zeros_like(out)
669-
compensation = np.zeros_like(out)
669+
# the below is equivalent to `np.zeros_like(out)` but faster
670+
sumx = np.zeros((<object>out).shape, dtype=(<object>out).base.dtype)
671+
compensation = np.zeros((<object>out).shape, dtype=(<object>out).base.dtype)
670672

671673
N, K = (<object>values).shape
672674

0 commit comments

Comments
 (0)