Skip to content

Commit 3dd8e99

Browse files
committed
BUG: bug in Series construction from UTC
1 parent 02906ce commit 3dd8e99

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ Bug Fixes
253253

254254

255255

256-
256+
- Bug in ``Series`` construction with a datetimetz (:issue:`14928`)
257257

258258

259259

pandas/core/ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,9 @@ def _offset(lvalues, rvalues):
545545

546546
# with tz, convert to UTC
547547
if self.is_datetime64tz_lhs:
548-
lvalues = lvalues.tz_localize(None)
548+
lvalues = lvalues.tz_convert('UTC').tz_localize(None)
549549
if self.is_datetime64tz_rhs:
550-
rvalues = rvalues.tz_localize(None)
550+
rvalues = rvalues.tz_convert('UTC').tz_localize(None)
551551

552552
lvalues = lvalues.view(np.int64)
553553
rvalues = rvalues.view(np.int64)

pandas/tests/series/test_constructors.py

+15
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,21 @@ def test_constructor_with_datetime_tz(self):
529529
expected = Series(pd.DatetimeIndex(['NaT', 'NaT'], tz='US/Eastern'))
530530
assert_series_equal(s, expected)
531531

532+
def test_construction_consistency(self):
533+
534+
# make sure that we are not re-localizing upon construction
535+
# GH 14928
536+
s = Series(pd.date_range('20130101', periods=3, tz='US/Eastern'))
537+
538+
result = Series(s, dtype=s.dtype)
539+
tm.assert_series_equal(result, s)
540+
541+
result = Series(s.dt.tz_convert('UTC'), dtype=s.dtype)
542+
tm.assert_series_equal(result, s)
543+
544+
result = Series(s.values, dtype=s.dtype)
545+
tm.assert_series_equal(result, s)
546+
532547
def test_constructor_periodindex(self):
533548
# GH7932
534549
# converting a PeriodIndex when put in a Series

pandas/types/cast.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -823,9 +823,10 @@ def _possibly_cast_to_datetime(value, dtype, errors='raise'):
823823
elif is_datetime64tz:
824824
# input has to be UTC at this point, so just
825825
# localize
826-
value = to_datetime(
827-
value,
828-
errors=errors).tz_localize(dtype.tz)
826+
value = (to_datetime(value, errors=errors)
827+
.tz_localize('UTC')
828+
.tz_convert(dtype.tz)
829+
)
829830
elif is_timedelta64:
830831
value = to_timedelta(value, errors=errors)._values
831832
except (AttributeError, ValueError, TypeError):

0 commit comments

Comments
 (0)