Skip to content

BUG: bug in Series construction from UTC #14928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ Bug Fixes




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



Expand Down
4 changes: 2 additions & 2 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,9 @@ def _offset(lvalues, rvalues):

# with tz, convert to UTC
if self.is_datetime64tz_lhs:
lvalues = lvalues.tz_localize(None)
lvalues = lvalues.tz_convert('UTC').tz_localize(None)
if self.is_datetime64tz_rhs:
rvalues = rvalues.tz_localize(None)
rvalues = rvalues.tz_convert('UTC').tz_localize(None)

lvalues = lvalues.view(np.int64)
rvalues = rvalues.view(np.int64)
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,21 @@ def test_constructor_with_datetime_tz(self):
expected = Series(pd.DatetimeIndex(['NaT', 'NaT'], tz='US/Eastern'))
assert_series_equal(s, expected)

def test_construction_consistency(self):

# make sure that we are not re-localizing upon construction
# GH 14928
s = Series(pd.date_range('20130101', periods=3, tz='US/Eastern'))

result = Series(s, dtype=s.dtype)
tm.assert_series_equal(result, s)

result = Series(s.dt.tz_convert('UTC'), dtype=s.dtype)
tm.assert_series_equal(result, s)

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

def test_constructor_periodindex(self):
# GH7932
# converting a PeriodIndex when put in a Series
Expand Down
7 changes: 4 additions & 3 deletions pandas/types/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,9 +823,10 @@ def _possibly_cast_to_datetime(value, dtype, errors='raise'):
elif is_datetime64tz:
# input has to be UTC at this point, so just
# localize
value = to_datetime(
value,
errors=errors).tz_localize(dtype.tz)
value = (to_datetime(value, errors=errors)
.tz_localize('UTC')
.tz_convert(dtype.tz)
)
elif is_timedelta64:
value = to_timedelta(value, errors=errors)._values
except (AttributeError, ValueError, TypeError):
Expand Down