@@ -255,7 +255,7 @@ def _bins_to_cuts(x, bins, right=True, labels=None, retbins=False,
255
255
256
256
def _format_levels (bins , prec , right = True ,
257
257
include_lowest = False , dtype = None ):
258
- fmt = lambda v : _format_label (v , precision = prec )
258
+ fmt = lambda v : _format_label (v , precision = prec , dtype = dtype )
259
259
if right :
260
260
levels = []
261
261
for a , b in zip (bins , bins [1 :]):
@@ -264,36 +264,25 @@ def _format_levels(bins, prec, right=True,
264
264
if a != b and fa == fb :
265
265
raise ValueError ('precision too low' )
266
266
267
- if dtype == np .datetime64 :
268
- formatted = '(%s, %s]' % (to_datetime (float (fa ), unit = 'ns' ),
269
- to_datetime (float (fb ), unit = 'ns' ))
270
- elif dtype == np .timedelta64 :
271
- formatted = '(%s, %s]' % (to_timedelta (float (fa ), unit = 'ns' ),
272
- to_timedelta (float (fb ), unit = 'ns' ))
273
- else :
274
- formatted = '(%s, %s]' % (fa , fb )
267
+ formatted = '(%s, %s]' % (fa , fb )
275
268
276
269
levels .append (formatted )
277
270
278
271
if include_lowest :
279
272
levels [0 ] = '[' + levels [0 ][1 :]
280
273
else :
281
- if dtype == np .datetime64 :
282
- levels = ['[%s, %s)' % (to_datetime (float (fmt (fa )), unit = 'ns' ),
283
- to_datetime (float (fmt (b )), unit = 'ns' ))
284
- for a , b in zip (bins , bins [1 :])]
285
- elif dtype == np .timedelta64 :
286
- levels = ['[%s, %s)' % (to_timedelta (float (fmt (fa )), unit = 'ns' ),
287
- to_timedelta (float (fmt (b )), unit = 'ns' ))
288
- for a , b in zip (bins , bins [1 :])]
289
- else :
290
- levels = ['[%s, %s)' % (fmt (a ), fmt (b ))
291
- for a , b in zip (bins , bins [1 :])]
274
+ levels = ['[%s, %s)' % (fmt (a ), fmt (b ))
275
+ for a , b in zip (bins , bins [1 :])]
292
276
return levels
293
277
294
278
295
- def _format_label (x , precision = 3 ):
279
+ def _format_label (x , precision = 3 , dtype = None ):
296
280
fmt_str = '%%.%dg' % precision
281
+
282
+ if dtype == np .datetime64 :
283
+ return to_datetime (x , unit = 'ns' )
284
+ if dtype == np .timedelta64 :
285
+ return to_timedelta (x , unit = 'ns' )
297
286
if np .isinf (x ):
298
287
return str (x )
299
288
elif is_float (x ):
0 commit comments