Skip to content

Commit

Permalink
add .astype('datetime64[ns, tz]') ability
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback committed Sep 7, 2015
1 parent e28680b commit d859b04
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
11 changes: 11 additions & 0 deletions doc/source/timeseries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1774,6 +1774,17 @@ TZ aware Dtypes
Both of these ``Series`` can be manipulated via the ``.dt`` accessor, see :ref:`here <basics.dt_accessors>`.
See the :ref:`docs <timeseries.dtypes>` for more details.

Further more you can ``.astype(...)`` timezone aware (and naive).

.. ipython:: python
# make this naive
s_aware.astype('datetime64[ns]')
# convert
s_aware.astype('datetime64[ns, CET]')
s_naive.astype('datetime64[ns, CET]')
.. note::

Using the ``.values`` accessor on a ``Series``, returns an numpy array of the data.
Expand Down
21 changes: 21 additions & 0 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
array_equivalent, _maybe_convert_string_to_object,
is_categorical, needs_i8_conversion, is_datetimelike_v_numeric,
is_internal_type)
from pandas.core.dtypes import DatetimeTZDtype

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

def _astype(self, dtype, **kwargs):
"""
these automatically copy, so copy=True has no effect
raise on an except if raise == True
"""

# if we are passed a datetime64[ns, tz]
if com.is_datetime64tz_dtype(dtype):
dtype = DatetimeTZDtype(dtype)

values = self.values
if getattr(values,'tz',None) is None:
values = DatetimeIndex(values).tz_localize('UTC')
values = values.tz_convert(dtype.tz)
return self.make_block(values)

# delegate
return super(DatetimeBlock, self)._astype(dtype=dtype, **kwargs)


def _can_hold_element(self, element):
if is_list_like(element):
element = np.array(element)
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,20 @@ def test_constructor_with_datetime_tz(self):
expected = Series(DatetimeIndex(s._values).asobject)
assert_series_equal(result, expected)

result = Series(s.values).dt.tz_localize('UTC').dt.tz_convert(s.dt.tz)
assert_series_equal(result, s)

# astype - datetime64[ns, tz]
result = Series(s.values).astype('datetime64[ns, US/Eastern]')
assert_series_equal(result, s)

result = Series(s.values).astype(s.dtype)
assert_series_equal(result, s)

result = s.astype('datetime64[ns, CET]')
expected = Series(date_range('20130101 06:00:00',periods=3,tz='CET'))
assert_series_equal(result, expected)

# short str
self.assertTrue('datetime64[ns, US/Eastern]' in str(s))

Expand Down

0 comments on commit d859b04

Please sign in to comment.