Skip to content

Commit 3a172ba

Browse files
committed
Modify whatsnew and make new wrapper function to handle UTC conversion
1 parent e85263d commit 3a172ba

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

doc/source/whatsnew/v0.21.0.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,10 @@ The configuration option ``pd.options.mode.use_inf_as_null`` is deprecated, and
208208
UTC Localization with Series
209209
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
210210

211-
Previously, :func:`to_datetime` did not localize datetime ``Series`` data as when ``utc=True`` was passed. Now, :func:`to_datetime`
212-
will correctly localize `Series` with a `datetime64[ns, UTC]` data type. (:issue:`6415`)
211+
:func:`to_datetime` now correctly localizes datetime ``Series`` data as when ``utc=True`` was passed with
212+
a `datetime64[ns, UTC]` data type. (:issue:`6415`)
213213

214-
Old Behavior
214+
Previous Behavior
215215

216216
.. ipython:: python
217217

pandas/core/tools/datetimes.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,14 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
340340

341341
tz = 'utc' if utc else None
342342

343+
def _maybe_convert_to_utc(arg, utc):
344+
if utc:
345+
if isinstance(arg, ABCSeries):
346+
arg = arg.dt.tz_localize('UTC')
347+
elif isinstance(arg, DatetimeIndex):
348+
arg = arg.tz_convert(None).tz_localize('UTC')
349+
return arg
350+
343351
def _convert_listlike(arg, box, format, name=None, tz=tz):
344352

345353
if isinstance(arg, (list, tuple)):
@@ -359,9 +367,7 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
359367
return DatetimeIndex(arg, tz=tz, name=name)
360368
except ValueError:
361369
pass
362-
from pandas import Series
363-
if isinstance(arg, Series) and utc:
364-
arg = arg.dt.tz_localize('utc')
370+
arg = _maybe_convert_to_utc(arg, utc)
365371
return arg
366372

367373
elif unit is not None:
@@ -438,12 +444,11 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
438444
yearfirst=yearfirst,
439445
require_iso8601=require_iso8601
440446
)
441-
from pandas import Series
442447
if is_datetime64_dtype(result) and box:
443448
result = DatetimeIndex(result, tz=tz, name=name)
444449
# GH 6415
445-
elif isinstance(arg, Series) and utc:
446-
result = Series(result, name=name).dt.tz_localize('utc')
450+
elif isinstance(arg, ABCSeries):
451+
result = _maybe_convert_to_utc(Series(result, name=name), utc)
447452
return result
448453

449454
except ValueError as e:

pandas/tests/io/test_sql.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1321,18 +1321,19 @@ def check(col):
13211321
def test_date_parsing(self):
13221322
# No Parsing
13231323
df = sql.read_sql_table("types_test_data", self.conn)
1324-
1324+
# Now that GH 6415 is fixed, dates are automatically parsed to UTC
1325+
utc_dtype = pd.core.dtypes.dtypes.DatetimeTZDtypeType
13251326
df = sql.read_sql_table("types_test_data", self.conn,
13261327
parse_dates=['DateCol'])
1327-
assert issubclass(df.DateCol.dtype.type, np.datetime64)
1328+
assert issubclass(df.DateCol.dtype.type, utc_dtype)
13281329

13291330
df = sql.read_sql_table("types_test_data", self.conn,
13301331
parse_dates={'DateCol': '%Y-%m-%d %H:%M:%S'})
1331-
assert issubclass(df.DateCol.dtype.type, np.datetime64)
1332+
assert issubclass(df.DateCol.dtype.type, utc_dtype)
13321333

13331334
df = sql.read_sql_table("types_test_data", self.conn, parse_dates={
13341335
'DateCol': {'format': '%Y-%m-%d %H:%M:%S'}})
1335-
assert issubclass(df.DateCol.dtype.type, np.datetime64)
1336+
assert issubclass(df.DateCol.dtype.type, utc_dtype)
13361337

13371338
df = sql.read_sql_table(
13381339
"types_test_data", self.conn, parse_dates=['IntDateCol'])

0 commit comments

Comments
 (0)