Skip to content

Commit 1456699

Browse files
committed
astype changes
1 parent 0eaefb1 commit 1456699

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

pandas/core/indexes/base.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
is_dtype_union_equal, is_extension_array_dtype, is_float, is_float_dtype,
2323
is_hashable, is_integer, is_integer_dtype, is_interval_dtype, is_iterator,
2424
is_list_like, is_object_dtype, is_period_dtype, is_scalar,
25-
is_signed_integer_dtype, is_timedelta64_dtype, is_unsigned_integer_dtype)
25+
is_signed_integer_dtype, is_timedelta64_dtype, is_unsigned_integer_dtype,
26+
pandas_dtype)
2627
import pandas.core.dtypes.concat as _concat
2728
from pandas.core.dtypes.generic import (
2829
ABCDataFrame, ABCDateOffset, ABCDatetimeArray, ABCIndexClass,
@@ -732,6 +733,12 @@ def astype(self, dtype, copy=True):
732733
from .category import CategoricalIndex
733734
return CategoricalIndex(self.values, name=self.name, dtype=dtype,
734735
copy=copy)
736+
elif is_datetime64tz_dtype(dtype):
737+
# avoid FutureWarning from DatetimeIndex constructor.
738+
from pandas import DatetimeIndex
739+
tz = pandas_dtype(dtype).tz
740+
return (DatetimeIndex(np.asarray(self))
741+
.tz_localize("UTC").tz_convert(tz))
735742

736743
elif is_extension_array_dtype(dtype):
737744
return Index(np.asarray(self), dtype=dtype, copy=copy)

pandas/tests/indexes/datetimes/test_astype.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -238,21 +238,10 @@ def _check_rng(rng):
238238
['US/Pacific', 'datetime64[ns, US/Pacific]'],
239239
[None, 'datetime64[ns]']])
240240
def test_integer_index_astype_datetime(self, tz, dtype):
241-
# GH 20997, 20964
241+
# GH 20997, 20964, 24559
242242
val = [pd.Timestamp('2018-01-01', tz=tz).value]
243-
244-
if tz:
245-
ex_warn = FutureWarning
246-
else:
247-
ex_warn = None
248-
249-
with tm.assert_produces_warning(ex_warn):
250-
# XXX: This likely shouldn't warn.
251-
# This raised on 0.24.x, so we can probably do the right thing
252-
# now.
253-
result = pd.Index(val).astype(dtype)
254-
with tm.assert_produces_warning(ex_warn):
255-
expected = pd.DatetimeIndex(val, tz=tz)
243+
result = pd.Index(val).astype(dtype)
244+
expected = pd.DatetimeIndex(["2018-01-01"], tz=tz)
256245
tm.assert_index_equal(result, expected)
257246

258247

0 commit comments

Comments
 (0)