|
10 | 10 | from pandas.core.base import _shared_docs
|
11 | 11 |
|
12 | 12 | from pandas.core.dtypes.common import (
|
13 |
| - _NS_DTYPE, _INT64_DTYPE, |
14 |
| - is_object_dtype, is_datetime64_dtype, |
15 |
| - is_datetimetz, is_dtype_equal, |
| 13 | + _INT64_DTYPE, |
| 14 | + _NS_DTYPE, |
| 15 | + is_object_dtype, |
| 16 | + is_datetime64_dtype, |
| 17 | + is_datetimetz, |
| 18 | + is_dtype_equal, |
16 | 19 | is_timedelta64_dtype,
|
17 |
| - is_integer, is_float, |
| 20 | + is_integer, |
| 21 | + is_float, |
18 | 22 | is_integer_dtype,
|
19 | 23 | is_datetime64_ns_dtype,
|
20 | 24 | is_period_dtype,
|
21 | 25 | is_bool_dtype,
|
22 |
| - is_string_dtype, |
23 |
| - is_categorical_dtype, |
24 | 26 | is_string_like,
|
25 | 27 | is_list_like,
|
26 | 28 | is_scalar,
|
|
36 | 38 | from pandas.core.algorithms import checked_add_with_arr
|
37 | 39 |
|
38 | 40 | from pandas.core.indexes.base import Index, _index_shared_docs
|
39 |
| -from pandas.core.indexes.category import CategoricalIndex |
40 | 41 | from pandas.core.indexes.numeric import Int64Index, Float64Index
|
41 | 42 | import pandas.compat as compat
|
42 |
| -from pandas.tseries.frequencies import ( |
43 |
| - to_offset, get_period_alias, |
44 |
| - Resolution) |
| 43 | +from pandas.tseries.frequencies import to_offset, get_period_alias, Resolution |
45 | 44 | from pandas.core.indexes.datetimelike import (
|
46 | 45 | DatelikeOps, TimelikeOps, DatetimeIndexOpsMixin)
|
47 | 46 | from pandas.tseries.offsets import (
|
48 | 47 | DateOffset, generate_range, Tick, CDay, prefix_mapping)
|
49 | 48 |
|
50 | 49 | from pandas.core.tools.timedeltas import to_timedelta
|
51 |
| -from pandas.util._decorators import (Appender, cache_readonly, |
52 |
| - deprecate_kwarg, Substitution) |
| 50 | +from pandas.util._decorators import ( |
| 51 | + Appender, cache_readonly, deprecate_kwarg, Substitution) |
53 | 52 | import pandas.core.common as com
|
54 | 53 | import pandas.tseries.offsets as offsets
|
55 | 54 | import pandas.core.tools.datetimes as tools
|
@@ -906,25 +905,16 @@ def _format_native_types(self, na_rep='NaT', date_format=None, **kwargs):
|
906 | 905 | @Appender(_index_shared_docs['astype'])
|
907 | 906 | def astype(self, dtype, copy=True):
|
908 | 907 | dtype = pandas_dtype(dtype)
|
909 |
| - if is_object_dtype(dtype): |
910 |
| - return self._box_values_as_index() |
911 |
| - elif is_integer_dtype(dtype): |
912 |
| - return Index(self.values.astype('i8', copy=copy), name=self.name, |
913 |
| - dtype='i8') |
914 |
| - elif is_datetime64_ns_dtype(dtype): |
915 |
| - if self.tz is not None: |
916 |
| - return self.tz_convert('UTC').tz_localize(None) |
917 |
| - elif copy is True: |
918 |
| - return self.copy() |
919 |
| - return self |
920 |
| - elif is_categorical_dtype(dtype): |
921 |
| - return CategoricalIndex(self.values, name=self.name, dtype=dtype, |
922 |
| - copy=copy) |
923 |
| - elif is_string_dtype(dtype): |
924 |
| - return Index(self.format(), name=self.name, dtype=object) |
| 908 | + if (is_datetime64_ns_dtype(dtype) and |
| 909 | + not is_dtype_equal(dtype, self.dtype)): |
| 910 | + # GH 18951: datetime64_ns dtype but not equal means different tz |
| 911 | + new_tz = getattr(dtype, 'tz', None) |
| 912 | + if getattr(self.dtype, 'tz', None) is None: |
| 913 | + return self.tz_localize(new_tz) |
| 914 | + return self.tz_convert(new_tz) |
925 | 915 | elif is_period_dtype(dtype):
|
926 | 916 | return self.to_period(freq=dtype.freq)
|
927 |
| - raise TypeError('Cannot cast DatetimeIndex to dtype %s' % dtype) |
| 917 | + return super(DatetimeIndex, self).astype(dtype, copy=copy) |
928 | 918 |
|
929 | 919 | def _get_time_micros(self):
|
930 | 920 | values = self.asi8
|
|
0 commit comments