@@ -255,7 +255,7 @@ def _bins_to_cuts(x, bins, right=True, labels=None, retbins=False,
255255
256256def _format_levels (bins , prec , right = True ,
257257 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 )
259259 if right :
260260 levels = []
261261 for a , b in zip (bins , bins [1 :]):
@@ -264,36 +264,25 @@ def _format_levels(bins, prec, right=True,
264264 if a != b and fa == fb :
265265 raise ValueError ('precision too low' )
266266
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 )
275268
276269 levels .append (formatted )
277270
278271 if include_lowest :
279272 levels [0 ] = '[' + levels [0 ][1 :]
280273 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 :])]
292276 return levels
293277
294278
295- def _format_label (x , precision = 3 ):
279+ def _format_label (x , precision = 3 , dtype = None ):
296280 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' )
297286 if np .isinf (x ):
298287 return str (x )
299288 elif is_float (x ):
0 commit comments