From b6b30491f222c064b6cbe68177a0cdfe25b7257a Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sat, 20 Apr 2019 15:09:28 -0700 Subject: [PATCH] BUG: DataFrame/Series constructor with tz aware data and datetime64[ns] dtype converts to naive --- doc/source/whatsnew/v0.25.0.rst | 2 +- pandas/core/dtypes/cast.py | 7 ++++++- pandas/tests/frame/test_timezones.py | 8 ++++++++ pandas/tests/series/test_timezones.py | 8 ++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 2a0bc0afc095c..b4a2b1ccb09d3 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -271,7 +271,7 @@ Datetimelike - Bug in :func:`to_datetime` which would raise an (incorrect) ``ValueError`` when called with a date far into the future and the ``format`` argument specified instead of raising ``OutOfBoundsDatetime`` (:issue:`23830`) - Bug in :func:`to_datetime` which would raise ``InvalidIndexError: Reindexing only valid with uniquely valued Index objects`` when called with ``cache=True``, with ``arg`` including at least two different elements from the set {None, numpy.nan, pandas.NaT} (:issue:`22305`) -- +- Bug in :class:`DataFrame` and :class:`Series` where timezone aware data with ``dtype='datetime64[ns]`` was not cast to naive (:issue:`25843`) - Timedelta diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index adaddf844ea9c..bbe01ff9d2dc4 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1017,7 +1017,12 @@ def maybe_cast_to_datetime(value, dtype, errors='raise'): dtype): try: if is_datetime64: - value = to_datetime(value, errors=errors)._values + value = to_datetime(value, errors=errors) + # GH 25843: Remove tz information since the dtype + # didn't specify one + if value.tz is not None: + value = value.tz_localize(None) + value = value._values elif is_datetime64tz: # The string check can be removed once issue #13712 # is solved. String data that is passed with a diff --git a/pandas/tests/frame/test_timezones.py b/pandas/tests/frame/test_timezones.py index c5077f0c49112..4262162a7c3a4 100644 --- a/pandas/tests/frame/test_timezones.py +++ b/pandas/tests/frame/test_timezones.py @@ -196,3 +196,11 @@ def test_tz_localize_convert_copy_inplace_mutate(self, copy, method, tz): index=date_range('20131027', periods=5, freq='1H', tz=tz)) tm.assert_frame_equal(result, expected) + + def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture): + # GH 25843 + tz = tz_aware_fixture + result = DataFrame({'d': [pd.Timestamp('2019', tz=tz)]}, + dtype='datetime64[ns]') + expected = DataFrame({'d': [pd.Timestamp('2019')]}) + tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/series/test_timezones.py b/pandas/tests/series/test_timezones.py index f47bbe51f4670..238edb7c60f2b 100644 --- a/pandas/tests/series/test_timezones.py +++ b/pandas/tests/series/test_timezones.py @@ -364,3 +364,11 @@ def test_tz_localize_convert_copy_inplace_mutate(self, copy, method, tz): index=date_range('20131027', periods=5, freq='1H', tz=tz)) tm.assert_series_equal(result, expected) + + def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture): + # GH 25843 + tz = tz_aware_fixture + result = Series([Timestamp('2019', tz=tz)], + dtype='datetime64[ns]') + expected = Series([Timestamp('2019')]) + tm.assert_series_equal(result, expected)