Skip to content

Commit d859b04

Browse files
committed
add .astype('datetime64[ns, tz]') ability
1 parent e28680b commit d859b04

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

doc/source/timeseries.rst

+11
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,17 @@ TZ aware Dtypes
17741774
Both of these ``Series`` can be manipulated via the ``.dt`` accessor, see :ref:`here <basics.dt_accessors>`.
17751775
See the :ref:`docs <timeseries.dtypes>` for more details.
17761776

1777+
Further more you can ``.astype(...)`` timezone aware (and naive).
1778+
1779+
.. ipython:: python
1780+
1781+
# make this naive
1782+
s_aware.astype('datetime64[ns]')
1783+
1784+
# convert
1785+
s_aware.astype('datetime64[ns, CET]')
1786+
s_naive.astype('datetime64[ns, CET]')
1787+
17771788
.. note::
17781789

17791790
Using the ``.values`` accessor on a ``Series``, returns an numpy array of the data.

pandas/core/internals.py

+21
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
array_equivalent, _maybe_convert_string_to_object,
1919
is_categorical, needs_i8_conversion, is_datetimelike_v_numeric,
2020
is_internal_type)
21+
from pandas.core.dtypes import DatetimeTZDtype
2122

2223
from pandas.core.index import Index, MultiIndex, _ensure_index
2324
from pandas.core.indexing import maybe_convert_indices, length_of_indexer
@@ -1868,6 +1869,26 @@ def __init__(self, values, placement,
18681869
fastpath=True, placement=placement,
18691870
**kwargs)
18701871

1872+
def _astype(self, dtype, **kwargs):
1873+
"""
1874+
these automatically copy, so copy=True has no effect
1875+
raise on an except if raise == True
1876+
"""
1877+
1878+
# if we are passed a datetime64[ns, tz]
1879+
if com.is_datetime64tz_dtype(dtype):
1880+
dtype = DatetimeTZDtype(dtype)
1881+
1882+
values = self.values
1883+
if getattr(values,'tz',None) is None:
1884+
values = DatetimeIndex(values).tz_localize('UTC')
1885+
values = values.tz_convert(dtype.tz)
1886+
return self.make_block(values)
1887+
1888+
# delegate
1889+
return super(DatetimeBlock, self)._astype(dtype=dtype, **kwargs)
1890+
1891+
18711892
def _can_hold_element(self, element):
18721893
if is_list_like(element):
18731894
element = np.array(element)

pandas/tests/test_series.py

+14
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,20 @@ def test_constructor_with_datetime_tz(self):
10721072
expected = Series(DatetimeIndex(s._values).asobject)
10731073
assert_series_equal(result, expected)
10741074

1075+
result = Series(s.values).dt.tz_localize('UTC').dt.tz_convert(s.dt.tz)
1076+
assert_series_equal(result, s)
1077+
1078+
# astype - datetime64[ns, tz]
1079+
result = Series(s.values).astype('datetime64[ns, US/Eastern]')
1080+
assert_series_equal(result, s)
1081+
1082+
result = Series(s.values).astype(s.dtype)
1083+
assert_series_equal(result, s)
1084+
1085+
result = s.astype('datetime64[ns, CET]')
1086+
expected = Series(date_range('20130101 06:00:00',periods=3,tz='CET'))
1087+
assert_series_equal(result, expected)
1088+
10751089
# short str
10761090
self.assertTrue('datetime64[ns, US/Eastern]' in str(s))
10771091

0 commit comments

Comments
 (0)