@@ -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,7 +1378,10 @@ def __call__(self, x):
1380
1378
1381
1379
class _TimelikeFormat :
1382
1380
def __init__ (self , data ):
1383
- non_nat = data [~ isnat (data )]
1381
+ if data is None :
1382
+ non_nat = []
1383
+ else :
1384
+ non_nat = data [~ isnat (data )]
1384
1385
if len (non_nat ) > 0 :
1385
1386
# Max str length of non-NaT elements
1386
1387
max_str_len = max (len (self ._format_non_nat (np .max (non_nat ))),
@@ -1406,11 +1407,13 @@ def __call__(self, x):
1406
1407
1407
1408
class DatetimeFormat (_TimelikeFormat ):
1408
1409
def __init__ (self , x , unit = None , timezone = None , casting = 'same_kind' ,
1409
- legacy = False ):
1410
+ legacy = False , * , dtype = None ):
1410
1411
# Get the unit from the dtype
1411
1412
if unit is None :
1412
- if x .dtype .kind == 'M' :
1413
- unit = datetime_data (x .dtype )[0 ]
1413
+ if dtype is None :
1414
+ dtype = x .dtype
1415
+ if dtype .kind == 'M' :
1416
+ unit = datetime_data (dtype )[0 ]
1414
1417
else :
1415
1418
unit = 's'
1416
1419
0 commit comments