@@ -400,10 +400,12 @@ def factorize_(by: tuple, axis, expected_groups: tuple[pd.Index, ...] = None):
400
400
# this makes the reindexing logic simpler.
401
401
if expect is None :
402
402
raise ValueError ("Please pass bin edges in expected_groups." )
403
- # idx = np.digitize(groupvar.ravel(), expect) - 1
404
- idx = pd .cut (groupvar .ravel (), bins = expect , labels = False ).codes .copy ()
405
- # same sentinel value as factorize
403
+ # TODO: fix for binning
406
404
found_groups .append (expect )
405
+ # pd.cut with bins = IntervalIndex[datetime64] doesn't work...
406
+ if groupvar .dtype .kind == "M" :
407
+ expect = np .concatenate ([expect .left .to_numpy (), [expect .right [- 1 ].to_numpy ()]])
408
+ idx = pd .cut (groupvar .ravel (), bins = expect ).codes .copy ()
407
409
else :
408
410
idx , groups = pd .factorize (groupvar .ravel ())
409
411
found_groups .append (np .array (groups ))
@@ -1246,13 +1248,15 @@ def _assert_by_is_aligned(shape, by):
1246
1248
1247
1249
1248
1250
def _convert_expected_groups_to_index (expected_groups , isbin : bool ) -> pd .Index | None :
1249
- if isinstance (expected_groups , pd .Index ):
1251
+ if isinstance (expected_groups , pd .IntervalIndex ) or (
1252
+ isinstance (expected_groups , pd .Index ) and not isbin
1253
+ ):
1250
1254
return expected_groups
1251
1255
if isbin :
1252
1256
return pd .IntervalIndex .from_arrays (expected_groups [:- 1 ], expected_groups [1 :])
1253
1257
elif expected_groups is not None :
1254
1258
return pd .Index (expected_groups )
1255
- return None
1259
+ return expected_groups
1256
1260
1257
1261
1258
1262
def groupby_reduce (
0 commit comments