@@ -360,69 +360,61 @@ def _(
360
360
if isinstance (op .bins , int ):
361
361
col_min = _apply_window_if_present (x .min (), window )
362
362
col_max = _apply_window_if_present (x .max (), window )
363
+ adj = (col_max - col_min ) * 0.001
363
364
bin_width = (col_max - col_min ) / op .bins
364
365
365
- if op .labels is False :
366
- for this_bin in range (op .bins - 1 ):
367
- if op .right :
368
- case_expr = x <= (col_min + (this_bin + 1 ) * bin_width )
369
- else :
370
- case_expr = x < (col_min + (this_bin + 1 ) * bin_width )
371
- out = out .when (
372
- case_expr ,
373
- compile_ibis_types .literal_to_ibis_scalar (
374
- this_bin , force_dtype = pd .Int64Dtype ()
375
- ),
366
+ for this_bin in range (op .bins ):
367
+ if op .labels is False :
368
+ value = compile_ibis_types .literal_to_ibis_scalar (
369
+ this_bin , force_dtype = pd .Int64Dtype ()
376
370
)
377
- out = out .when (x .notnull (), op .bins - 1 )
378
- else :
379
- interval_struct = None
380
- adj = (col_max - col_min ) * 0.001
381
- for this_bin in range (op .bins ):
382
- left_edge_adj = adj if this_bin == 0 and op .right else 0
383
- right_edge_adj = adj if this_bin == op .bins - 1 and not op .right else 0
371
+ else :
372
+ left_adj = adj if this_bin == 0 and op .right else 0
373
+ right_adj = adj if this_bin == op .bins - 1 and not op .right else 0
384
374
385
- left_edge = col_min + this_bin * bin_width - left_edge_adj
386
- right_edge = col_min + (this_bin + 1 ) * bin_width + right_edge_adj
375
+ left = col_min + this_bin * bin_width - left_adj
376
+ right = col_min + (this_bin + 1 ) * bin_width + right_adj
387
377
388
378
if op .right :
389
- interval_struct = ibis_types .struct (
390
- {
391
- "left_exclusive" : left_edge ,
392
- "right_inclusive" : right_edge ,
393
- }
379
+ value = ibis_types .struct (
380
+ {"left_exclusive" : left , "right_inclusive" : right }
394
381
)
395
382
else :
396
- interval_struct = ibis_types .struct (
397
- {
398
- "left_inclusive" : left_edge ,
399
- "right_exclusive" : right_edge ,
400
- }
383
+ value = ibis_types .struct (
384
+ {"left_inclusive" : left , "right_exclusive" : right }
401
385
)
402
-
403
- if this_bin < op .bins - 1 :
404
- if op .right :
405
- case_expr = x <= (col_min + (this_bin + 1 ) * bin_width )
406
- else :
407
- case_expr = x < (col_min + (this_bin + 1 ) * bin_width )
408
- out = out .when (case_expr , interval_struct )
386
+ if this_bin == op .bins - 1 :
387
+ case_expr = x .notnull ()
388
+ else :
389
+ if op .right :
390
+ case_expr = x <= (col_min + (this_bin + 1 ) * bin_width )
409
391
else :
410
- out = out .when (x .notnull (), interval_struct )
392
+ case_expr = x < (col_min + (this_bin + 1 ) * bin_width )
393
+ out = out .when (case_expr , value )
411
394
else : # Interpret as intervals
412
- for interval in op .bins :
395
+ for this_bin , interval in enumerate ( op .bins ) :
413
396
left = compile_ibis_types .literal_to_ibis_scalar (interval [0 ])
414
397
right = compile_ibis_types .literal_to_ibis_scalar (interval [1 ])
415
398
if op .right :
416
399
condition = (x > left ) & (x <= right )
417
- interval_struct = ibis_types .struct (
418
- {"left_exclusive" : left , "right_inclusive" : right }
419
- )
420
400
else :
421
401
condition = (x >= left ) & (x < right )
422
- interval_struct = ibis_types .struct (
423
- {"left_inclusive" : left , "right_exclusive" : right }
402
+
403
+ if op .labels is False :
404
+ value = compile_ibis_types .literal_to_ibis_scalar (
405
+ this_bin , force_dtype = pd .Int64Dtype ()
424
406
)
425
- out = out .when (condition , interval_struct )
407
+ else :
408
+ if op .right :
409
+ value = ibis_types .struct (
410
+ {"left_exclusive" : left , "right_inclusive" : right }
411
+ )
412
+ else :
413
+ value = ibis_types .struct (
414
+ {"left_inclusive" : left , "right_exclusive" : right }
415
+ )
416
+
417
+ out = out .when (condition , value )
426
418
return out .end ()
427
419
428
420
0 commit comments