@@ -1848,6 +1848,8 @@ def _possibly_cast_to_datetime(value, dtype, coerce=False):
18481848 """ try to cast the array/value to a datetimelike dtype, converting float
18491849 nan to iNaT
18501850 """
1851+ from pandas .tseries .timedeltas import _possibly_cast_to_timedelta
1852+ from pandas .tseries .tools import to_datetime
18511853
18521854 if dtype is not None :
18531855 if isinstance (dtype , compat .string_types ):
@@ -1886,13 +1888,11 @@ def _possibly_cast_to_datetime(value, dtype, coerce=False):
18861888 elif np .prod (value .shape ) and value .dtype != dtype :
18871889 try :
18881890 if is_datetime64 :
1889- from pandas .tseries .tools import to_datetime
18901891 value = to_datetime (value , coerce = coerce ).values
18911892 elif is_timedelta64 :
1892- from pandas .tseries .timedeltas import \
1893- _possibly_cast_to_timedelta
1894- value = _possibly_cast_to_timedelta (value , coerce = 'compat' , dtype = dtype )
1895- except :
1893+ value = _possibly_cast_to_timedelta (value ,
1894+ dtype = dtype )
1895+ except (AttributeError , ValueError ):
18961896 pass
18971897
18981898 else :
@@ -1901,28 +1901,20 @@ def _possibly_cast_to_datetime(value, dtype, coerce=False):
19011901
19021902 # catch a datetime/timedelta that is not of ns variety
19031903 # and no coercion specified
1904- if ( is_array and value .dtype .kind in ['M' ,'m' ]) :
1904+ if is_array and value .dtype .kind in ['M' , 'm' ]:
19051905 dtype = value .dtype
19061906
19071907 if dtype .kind == 'M' and dtype != _NS_DTYPE :
19081908 value = value .astype (_NS_DTYPE )
19091909
19101910 elif dtype .kind == 'm' and dtype != _TD_DTYPE :
1911- from pandas .tseries .timedeltas import \
1912- _possibly_cast_to_timedelta
1913- value = _possibly_cast_to_timedelta (value , coerce = 'compat' )
1911+ value = _possibly_cast_to_timedelta (value )
19141912
19151913 # only do this if we have an array and the dtype of the array is not
19161914 # setup already we are not an integer/object, so don't bother with this
19171915 # conversion
1918- elif (is_array and not (
1919- issubclass (value .dtype .type , np .integer ) or
1920- value .dtype == np .object_ )):
1921- pass
1922-
1923- # try to infer if we have a datetimelike here
1924- # otherwise pass thru
1925- else :
1916+ elif not (is_array and not (issubclass (value .dtype .type , np .integer ) or
1917+ value .dtype == np .object_ )):
19261918 value = _possibly_infer_to_datetimelike (value )
19271919
19281920 return value
0 commit comments