@@ -43,6 +43,9 @@ class AmbiguousIndexError(PandasError, KeyError):
43
43
44
44
45
45
_POSSIBLY_CAST_DTYPES = set ([ np .dtype (t ) for t in ['M8[ns]' ,'m8[ns]' ,'O' ,'int8' ,'uint8' ,'int16' ,'uint16' ,'int32' ,'uint32' ,'int64' ,'uint64' ] ])
46
+ _NS_DTYPE = np .dtype ('M8[ns]' )
47
+ _TD_DTYPE = np .dtype ('m8[ns]' )
48
+ _INT64_DTYPE = np .dtype (np .int64 )
46
49
47
50
def isnull (obj ):
48
51
'''
@@ -1085,10 +1088,10 @@ def _possibly_cast_to_datetime(value, dtype, coerce = False):
1085
1088
if is_datetime64 or is_timedelta64 :
1086
1089
1087
1090
# force the dtype if needed
1088
- # if is_datetime64 and dtype != 'datetime64[ns]' :
1089
- # dtype = np.dtype('datetime64[ns]' )
1090
- # elif is_timedelta64 and dtype != 'timedelta64[ns]' :
1091
- # dtype = np. dtype('timedelta64[ns]' )
1091
+ if is_datetime64 and dtype != _NS_DTYPE :
1092
+ raise TypeError ( "cannot convert datetimelike to dtype [%s]" % dtype )
1093
+ elif is_timedelta64 and dtype != _TD_DTYPE :
1094
+ raise TypeError ( "cannot convert timedeltalike to dtype [%s]" % dtype )
1092
1095
1093
1096
if np .isscalar (value ):
1094
1097
if value == tslib .iNaT or isnull (value ):
@@ -1106,7 +1109,6 @@ def _possibly_cast_to_datetime(value, dtype, coerce = False):
1106
1109
if is_datetime64 :
1107
1110
from pandas .tseries .tools import to_datetime
1108
1111
value = to_datetime (value , coerce = coerce ).values
1109
- #value = tslib.array_to_datetime(value, coerce = coerce)
1110
1112
elif is_timedelta64 :
1111
1113
value = _possibly_cast_to_timedelta (value )
1112
1114
except :
@@ -1523,9 +1525,24 @@ def _astype_nansafe(arr, dtype, copy = True):
1523
1525
if not isinstance (dtype , np .dtype ):
1524
1526
dtype = np .dtype (dtype )
1525
1527
1526
- if issubclass (arr . dtype . type , np . datetime64 ):
1528
+ if is_datetime64_dtype (arr ):
1527
1529
if dtype == object :
1528
1530
return tslib .ints_to_pydatetime (arr .view (np .int64 ))
1531
+ elif issubclass (dtype .type , np .int ):
1532
+ return arr .view (dtype )
1533
+ elif dtype != _NS_DTYPE :
1534
+ raise TypeError ("cannot astype a datetimelike from [%s] to [%s]" % (arr .dtype ,dtype ))
1535
+ return arr .astype (_NS_DTYPE )
1536
+ elif is_timedelta64_dtype (arr ):
1537
+ if issubclass (dtype .type , np .int ):
1538
+ return arr .view (dtype )
1539
+ elif dtype == object :
1540
+ return arr .astype (object )
1541
+
1542
+ # in py3, timedelta64[ns] are int64
1543
+ elif (py3compat .PY3 and dtype not in [_INT64_DTYPE ,_TD_DTYPE ]) or (not py3compat .PY3 and dtype != _TD_DTYPE ):
1544
+ raise TypeError ("cannot astype a timedelta from [%s] to [%s]" % (arr .dtype ,dtype ))
1545
+ return arr .astype (_TD_DTYPE )
1529
1546
elif (np .issubdtype (arr .dtype , np .floating ) and
1530
1547
np .issubdtype (dtype , np .integer )):
1531
1548
@@ -1729,9 +1746,6 @@ def _check_as_is(x):
1729
1746
self .queue .truncate (0 )
1730
1747
1731
1748
1732
- _NS_DTYPE = np .dtype ('M8[ns]' )
1733
-
1734
-
1735
1749
def _concat_compat (to_concat , axis = 0 ):
1736
1750
# filter empty arrays
1737
1751
to_concat = [x for x in to_concat if x .shape [axis ] > 0 ]
@@ -1759,7 +1773,6 @@ def _to_pydatetime(x):
1759
1773
1760
1774
return x
1761
1775
1762
-
1763
1776
def _where_compat (mask , arr1 , arr2 ):
1764
1777
if arr1 .dtype == _NS_DTYPE and arr2 .dtype == _NS_DTYPE :
1765
1778
new_vals = np .where (mask , arr1 .view (np .int64 ), arr2 .view (np .int64 ))
0 commit comments