@@ -1848,6 +1848,8 @@ def _possibly_cast_to_datetime(value, dtype, coerce=False):
1848
1848
""" try to cast the array/value to a datetimelike dtype, converting float
1849
1849
nan to iNaT
1850
1850
"""
1851
+ from pandas .tseries .timedeltas import _possibly_cast_to_timedelta
1852
+ from pandas .tseries .tools import to_datetime
1851
1853
1852
1854
if dtype is not None :
1853
1855
if isinstance (dtype , compat .string_types ):
@@ -1886,13 +1888,11 @@ def _possibly_cast_to_datetime(value, dtype, coerce=False):
1886
1888
elif np .prod (value .shape ) and value .dtype != dtype :
1887
1889
try :
1888
1890
if is_datetime64 :
1889
- from pandas .tseries .tools import to_datetime
1890
1891
value = to_datetime (value , coerce = coerce ).values
1891
1892
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 ):
1896
1896
pass
1897
1897
1898
1898
else :
@@ -1901,28 +1901,20 @@ def _possibly_cast_to_datetime(value, dtype, coerce=False):
1901
1901
1902
1902
# catch a datetime/timedelta that is not of ns variety
1903
1903
# 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' ]:
1905
1905
dtype = value .dtype
1906
1906
1907
1907
if dtype .kind == 'M' and dtype != _NS_DTYPE :
1908
1908
value = value .astype (_NS_DTYPE )
1909
1909
1910
1910
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 )
1914
1912
1915
1913
# only do this if we have an array and the dtype of the array is not
1916
1914
# setup already we are not an integer/object, so don't bother with this
1917
1915
# 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_ )):
1926
1918
value = _possibly_infer_to_datetimelike (value )
1927
1919
1928
1920
return value
0 commit comments