@@ -408,7 +408,7 @@ def str_format(x):
408
408
return str (x )
409
409
410
410
def _get_formatdict (data , * , precision , floatmode , suppress , sign , legacy ,
411
- formatter , fmt = None , ** kwargs ):
411
+ formatter , fmt = None , dtype = None , ** kwargs ):
412
412
# note: extra arguments in kwargs are ignored
413
413
414
414
# wrapped in lambdas to avoid taking a code path with the wrong type of data
@@ -426,7 +426,7 @@ def _get_formatdict(data, *, precision, floatmode, suppress, sign, legacy,
426
426
'longcomplexfloat' : lambda : ComplexFloatingFormat (
427
427
data , precision , floatmode , suppress , sign ,
428
428
legacy = legacy , fmt = fmt , longdouble_quoting = True ),
429
- 'datetime' : lambda : DatetimeFormat (data , legacy = legacy ),
429
+ 'datetime' : lambda : DatetimeFormat (data , legacy = legacy , dtype = dtype ),
430
430
'timedelta' : lambda : TimedeltaFormat (data ),
431
431
'object' : lambda : _object_format ,
432
432
'void' : lambda : str_format ,
@@ -499,17 +499,14 @@ def get_formatter(*, dtype=None, data=None, fmt=None, options=None):
499
499
fmt = None
500
500
501
501
if fmt is not None :
502
- if options is not None :
503
- raise TypeError ("Use of options is only supported if `fmt=None`" )
504
-
505
502
options = _default_format_options
506
503
507
504
if fmt is not repr and fmt is not str :
508
505
raise TypeError (
509
506
"get_formatter(): only `repr`, `str`, and None (or '') "
510
507
"is currently supported for `fmt`." )
511
508
512
- formatdict = _get_formatdict (data , fmt = fmt , ** options )
509
+ formatdict = _get_formatdict (data , dtype = dtype , fmt = fmt , ** options )
513
510
514
511
dtypeobj = dtype .type
515
512
@@ -548,7 +545,8 @@ def get_formatter(*, dtype=None, data=None, fmt=None, options=None):
548
545
# given and `arr.dtype` cannot be a subarray dtype:
549
546
assert data is None
550
547
return SubArrayFormat (
551
- get_formatter (dtype = dtype .base , fmt = fmt , options = options ))
548
+ get_formatter (dtype = dtype .base , fmt = fmt ,
549
+ options = None if fmt is not None else options ))
552
550
else :
553
551
return formatdict ['void' ]()
554
552
else :
@@ -1380,16 +1378,20 @@ def __call__(self, x):
1380
1378
1381
1379
class _TimelikeFormat :
1382
1380
def __init__ (self , data ):
1383
- non_nat = data [~ isnat (data )]
1384
- if len (non_nat ) > 0 :
1385
- # Max str length of non-NaT elements
1386
- max_str_len = max (len (self ._format_non_nat (np .max (non_nat ))),
1387
- len (self ._format_non_nat (np .min (non_nat ))))
1381
+ if data is not None :
1382
+ non_nat = data [~ isnat (data )]
1383
+ if len (non_nat ) > 0 :
1384
+ # Max str length of non-NaT elements
1385
+ max_str_len = max (len (self ._format_non_nat (np .max (non_nat ))),
1386
+ len (self ._format_non_nat (np .min (non_nat ))))
1387
+ else :
1388
+ max_str_len = 0
1389
+ if len (non_nat ) < data .size :
1390
+ # data contains a NaT
1391
+ max_str_len = max (max_str_len , 5 )
1388
1392
else :
1389
1393
max_str_len = 0
1390
- if len (non_nat ) < data .size :
1391
- # data contains a NaT
1392
- max_str_len = max (max_str_len , 5 )
1394
+
1393
1395
self ._format = '%{}s' .format (max_str_len )
1394
1396
self ._nat = "'NaT'" .rjust (max_str_len )
1395
1397
@@ -1406,11 +1408,13 @@ def __call__(self, x):
1406
1408
1407
1409
class DatetimeFormat (_TimelikeFormat ):
1408
1410
def __init__ (self , x , unit = None , timezone = None , casting = 'same_kind' ,
1409
- legacy = False ):
1411
+ legacy = False , * , dtype = None ):
1410
1412
# Get the unit from the dtype
1411
1413
if unit is None :
1412
- if x .dtype .kind == 'M' :
1413
- unit = datetime_data (x .dtype )[0 ]
1414
+ if dtype is None :
1415
+ dtype = x .dtype
1416
+ if dtype .kind == 'M' :
1417
+ unit = datetime_data (dtype )[0 ]
1414
1418
else :
1415
1419
unit = 's'
1416
1420
0 commit comments