7
7
8
8
from pandas ._libs import tslib , lib
9
9
from pandas ._libs .tslib import iNaT
10
- from pandas .compat import string_types , text_type , PY3
10
+ from pandas .compat import string_types , text_type
11
11
from .common import (_ensure_object , is_bool , is_integer , is_float ,
12
12
is_complex , is_datetimetz , is_categorical_dtype ,
13
13
is_datetimelike ,
24
24
pandas_dtype ,
25
25
_ensure_int8 , _ensure_int16 ,
26
26
_ensure_int32 , _ensure_int64 ,
27
- _NS_DTYPE , _TD_DTYPE , _INT64_DTYPE ,
27
+ _NS_DTYPE , _TD_DTYPE ,
28
28
_POSSIBLY_CAST_DTYPES )
29
29
from .dtypes import ExtensionDtype , DatetimeTZDtype , PeriodDtype
30
30
from .generic import (ABCDatetimeIndex , ABCPeriodIndex ,
@@ -656,33 +656,29 @@ def astype_nansafe(arr, dtype, copy=True):
656
656
return tslib .ints_to_pydatetime (arr .view (np .int64 ))
657
657
elif dtype == np .int64 :
658
658
return arr .view (dtype )
659
- elif dtype != _NS_DTYPE :
660
- raise TypeError ("cannot astype a datetimelike from [{from_dtype}] "
661
- "to [{to_dtype}]" .format (from_dtype = arr .dtype ,
662
- to_dtype = dtype ))
663
- return arr .astype (_NS_DTYPE )
659
+
660
+ # allow frequency conversions
661
+ if dtype .kind == 'M' :
662
+ return arr .astype (dtype )
663
+
664
+ raise TypeError ("cannot astype a datetimelike from [{from_dtype}] "
665
+ "to [{to_dtype}]" .format (from_dtype = arr .dtype ,
666
+ to_dtype = dtype ))
667
+
664
668
elif is_timedelta64_dtype (arr ):
665
669
if dtype == np .int64 :
666
670
return arr .view (dtype )
667
671
elif dtype == object :
668
672
return tslib .ints_to_pytimedelta (arr .view (np .int64 ))
669
673
670
- # in py3, timedelta64[ns] are int64
671
- elif ((PY3 and dtype not in [_INT64_DTYPE , _TD_DTYPE ]) or
672
- (not PY3 and dtype != _TD_DTYPE )):
673
-
674
- # allow frequency conversions
675
- if dtype .kind == 'm' :
676
- mask = isna (arr )
677
- result = arr .astype (dtype ).astype (np .float64 )
678
- result [mask ] = np .nan
679
- return result
674
+ # allow frequency conversions
675
+ if dtype .kind == 'm' :
676
+ return arr .astype (dtype )
680
677
681
- raise TypeError ("cannot astype a timedelta from [{from_dtype}] "
682
- "to [{to_dtype}]" .format (from_dtype = arr .dtype ,
683
- to_dtype = dtype ))
678
+ raise TypeError ("cannot astype a timedelta from [{from_dtype}] "
679
+ "to [{to_dtype}]" .format (from_dtype = arr .dtype ,
680
+ to_dtype = dtype ))
684
681
685
- return arr .astype (_TD_DTYPE )
686
682
elif (np .issubdtype (arr .dtype , np .floating ) and
687
683
np .issubdtype (dtype , np .integer )):
688
684
@@ -707,14 +703,16 @@ def astype_nansafe(arr, dtype, copy=True):
707
703
if arr .dtype == dtype :
708
704
return arr .copy ()
709
705
710
- # we handle datetimelikes with pandas machinery
711
- # to be robust to the input type
712
- elif is_datetime64_dtype (dtype ):
713
- from pandas import to_datetime
714
- return to_datetime (arr ).values
715
- elif is_timedelta64_dtype (dtype ):
716
- from pandas import to_timedelta
717
- return to_timedelta (arr ).values
706
+ elif is_object_dtype (arr ):
707
+
708
+ # we handle datetimelikes with pandas machinery
709
+ # to be robust to the input type
710
+ if is_datetime64_dtype (dtype ):
711
+ from pandas import to_datetime
712
+ return to_datetime (arr ).values
713
+ elif is_timedelta64_dtype (dtype ):
714
+ from pandas import to_timedelta
715
+ return to_timedelta (arr ).values
718
716
719
717
return arr .astype (dtype )
720
718
return arr .view (dtype )
0 commit comments