14
14
is_float ,
15
15
is_list_like ,
16
16
is_scalar ,
17
- is_numeric_dtype )
17
+ is_numeric_dtype ,
18
+ is_bool_dtype )
18
19
from pandas .core .dtypes .generic import (
19
20
ABCIndexClass , ABCSeries ,
20
21
ABCDataFrame )
@@ -337,6 +338,7 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
337
338
338
339
"""
339
340
from pandas .core .indexes .datetimes import DatetimeIndex
341
+ from pandas import DataFrame
340
342
341
343
tz = 'utc' if utc else None
342
344
@@ -446,9 +448,24 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
446
448
except (ValueError , TypeError ):
447
449
raise e
448
450
451
+ def check_numerical_arg ():
452
+ return ((is_scalar (arg ) and (is_integer (arg ) or is_float (arg ))) or
453
+ is_numeric_dtype (np .asarray (arg )))
454
+
449
455
if arg is None :
450
456
return None
451
457
458
+ if unit is not None :
459
+ if isinstance (arg , (DataFrame ,)):
460
+ raise ValueError ("unit must be None if arg is a DataFrame" )
461
+ if is_bool_dtype (np .asarray (arg )):
462
+ raise TypeError ("{0} is not convertible to datetime"
463
+ .format (type (arg )))
464
+ else :
465
+ if (format is None and check_numerical_arg () and
466
+ not isinstance (arg , (DataFrame ,))):
467
+ raise ValueError ("a unit is required in case of numerical arg" )
468
+
452
469
# handle origin
453
470
if origin == 'julian' :
454
471
@@ -474,8 +491,7 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
474
491
475
492
# arg must be a numeric
476
493
original = arg
477
- if not ((is_scalar (arg ) and (is_integer (arg ) or is_float (arg ))) or
478
- is_numeric_dtype (np .asarray (arg ))):
494
+ if not check_numerical_arg ():
479
495
raise ValueError (
480
496
"'{arg}' is not compatible with origin='{origin}'; "
481
497
"it must be numeric with a unit specified " .format (
0 commit comments