Skip to content

Commit 5fa8a31

Browse files
mroeschkejreback
authored andcommitted
BUG: DataFrame/Series constructor with tz aware data and datetime64[ns] dtype converts to naive (#26167)
1 parent 84fa2ef commit 5fa8a31

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ Datetimelike
272272

273273
- 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`)
274274
- 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`)
275-
-
275+
- Bug in :class:`DataFrame` and :class:`Series` where timezone aware data with ``dtype='datetime64[ns]`` was not cast to naive (:issue:`25843`)
276276
-
277277

278278
Timedelta

pandas/core/dtypes/cast.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,12 @@ def maybe_cast_to_datetime(value, dtype, errors='raise'):
10171017
dtype):
10181018
try:
10191019
if is_datetime64:
1020-
value = to_datetime(value, errors=errors)._values
1020+
value = to_datetime(value, errors=errors)
1021+
# GH 25843: Remove tz information since the dtype
1022+
# didn't specify one
1023+
if value.tz is not None:
1024+
value = value.tz_localize(None)
1025+
value = value._values
10211026
elif is_datetime64tz:
10221027
# The string check can be removed once issue #13712
10231028
# is solved. String data that is passed with a

pandas/tests/frame/test_timezones.py

+8
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,11 @@ def test_tz_localize_convert_copy_inplace_mutate(self, copy, method, tz):
196196
index=date_range('20131027', periods=5,
197197
freq='1H', tz=tz))
198198
tm.assert_frame_equal(result, expected)
199+
200+
def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture):
201+
# GH 25843
202+
tz = tz_aware_fixture
203+
result = DataFrame({'d': [pd.Timestamp('2019', tz=tz)]},
204+
dtype='datetime64[ns]')
205+
expected = DataFrame({'d': [pd.Timestamp('2019')]})
206+
tm.assert_frame_equal(result, expected)

pandas/tests/series/test_timezones.py

+8
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,11 @@ def test_tz_localize_convert_copy_inplace_mutate(self, copy, method, tz):
364364
index=date_range('20131027', periods=5, freq='1H',
365365
tz=tz))
366366
tm.assert_series_equal(result, expected)
367+
368+
def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture):
369+
# GH 25843
370+
tz = tz_aware_fixture
371+
result = Series([Timestamp('2019', tz=tz)],
372+
dtype='datetime64[ns]')
373+
expected = Series([Timestamp('2019')])
374+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)