Skip to content

Commit a0740eb

Browse files
committed
Fix factorizing
1 parent bbdaa0c commit a0740eb

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

flox/core.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ def factorize_(
522522
# this is important in shared-memory parallelism with dask
523523
# TODO: figure out how to avoid this
524524
idx = flat.copy()
525-
found_groups.append(np.array(expect))
525+
found_groups.append(np.array(expect, like=flat))
526526
# TODO: fix by using masked integers
527527
idx[idx > expect[-1]] = -1
528528

@@ -537,7 +537,7 @@ def factorize_(
537537
right = expect.closed_right
538538
idx = np.digitize(
539539
flat,
540-
bins=bins.view(np.intp) if bins.dtype.kind == "M" else bins,
540+
bins=np.array(bins.view(np.intp) if bins.dtype.kind == "M" else bins, like=flat),
541541
right=right,
542542
)
543543
idx -= 1
@@ -560,9 +560,13 @@ def factorize_(
560560
idx = sorter[(idx,)]
561561
idx[mask] = -1
562562
else:
563-
idx, groups = pd.factorize(flat, sort=sort)
563+
if isinstance(flat, np.ndarray):
564+
idx, groups = pd.factorize(flat, sort=sort)
565+
else:
566+
assert sort
567+
groups, idx = np.unique(flat, return_inverse=True)
564568

565-
found_groups.append(np.array(groups))
569+
found_groups.append(groups)
566570
factorized.append(idx.reshape(groupvar.shape))
567571

568572
grp_shape = tuple(len(grp) for grp in found_groups)

0 commit comments

Comments
 (0)