Skip to content

Commit 650abcf

Browse files
committed
BUG: Correctly localize naive ts strings with Series and datetimetz dtype (pandas-dev#17415)
Add whatsnew Fix lint error
1 parent be66ef8 commit 650abcf

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

doc/source/whatsnew/v0.21.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,7 @@ Conversion
10471047
- Bug in :meth:`~DataFrame.astype` converting to object dtype when passed extension type classes (`DatetimeTZDtype``, ``CategoricalDtype``) rather than instances. Now a ``TypeError`` is raised when a class is passed (:issue:`17780`).
10481048
- Bug in :meth:`to_numeric` in which elements were not always being coerced to numeric when ``errors='coerce'`` (:issue:`17007`, :issue:`17125`)
10491049
- Bug in ``DataFrame`` and ``Series`` constructors where ``range`` objects are converted to ``int32`` dtype on Windows instead of ``int64`` (:issue:`16804`)
1050+
- Bug in localization of a naive, datetime string in a ``Series`` constructor with a ``datetime[ns, timezone]`` dtype (:issue:`174151`)
10501051

10511052
Indexing
10521053
^^^^^^^^

pandas/core/dtypes/cast.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
is_integer_dtype,
2121
is_datetime_or_timedelta_dtype,
2222
is_bool_dtype, is_scalar,
23-
_string_dtypes,
23+
is_string_dtype, _string_dtypes,
2424
pandas_dtype,
2525
_ensure_int8, _ensure_int16,
2626
_ensure_int32, _ensure_int64,
@@ -1003,12 +1003,16 @@ def maybe_cast_to_datetime(value, dtype, errors='raise'):
10031003
if is_datetime64:
10041004
value = to_datetime(value, errors=errors)._values
10051005
elif is_datetime64tz:
1006-
# input has to be UTC at this point, so just
1007-
# localize
1008-
value = (to_datetime(value, errors=errors)
1009-
.tz_localize('UTC')
1010-
.tz_convert(dtype.tz)
1011-
)
1006+
is_dt_string = is_string_dtype(value)
1007+
value = to_datetime(value, errors=errors)
1008+
if is_dt_string:
1009+
# Strings here are naive, so directly localize
1010+
value = value.tz_localize(dtype.tz)
1011+
else:
1012+
# Numeric values are UTC at this point,
1013+
# so localize and convert
1014+
value = (value.tz_localize('UTC')
1015+
.tz_convert(dtype.tz))
10121016
elif is_timedelta64:
10131017
value = to_timedelta(value, errors=errors)._values
10141018
except (AttributeError, ValueError, TypeError):

pandas/tests/series/test_constructors.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,12 @@ def test_constructor_with_datetime_tz(self):
574574
expected = Series(pd.DatetimeIndex(['NaT', 'NaT'], tz='US/Eastern'))
575575
assert_series_equal(s, expected)
576576

577+
# GH 17415: With naive string
578+
ts_naive = '2013-01-01 00:00:00'
579+
result = Series([ts_naive], dtype='datetime64[ns, CET]')
580+
expected = Series([pd.Timestamp(ts_naive, tz='CET')])
581+
assert_series_equal(result, expected)
582+
577583
def test_construction_interval(self):
578584
# construction from interval & array of intervals
579585
index = IntervalIndex.from_breaks(np.arange(3), closed='right')

0 commit comments

Comments
 (0)