Skip to content

Commit b7a2207

Browse files
committed
add an example how to get the old behavior and : correct the warning message
1 parent acee8de commit b7a2207

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

doc/source/whatsnew/v2.1.0.rst

+12-4
Original file line numberDiff line numberDiff line change
@@ -318,19 +318,27 @@ Parsing datetimes with mixed time zones is deprecated and shows a warning unless
318318
FutureWarning:
319319
In a future version of pandas, parsing datetimes with mixed time zones will raise
320320
a warning unless `utc=True`. Please specify `utc=True` to opt in to the new behaviour
321-
and silence this warning.
321+
and silence this warning. To create a `Series` with mixed offsets and `object` dtype,
322+
please use `apply` and `datetime.datetime.strptime`.
322323
Index([2020-01-01 00:00:00+06:00, 2020-01-01 00:00:00+01:00], dtype='object')
323324
324325
In order to silence this warning and avoid an error in a future version of pandas,
325326
please specify ``utc=True``:
326327

327328
.. ipython:: python
328329
329-
data = ["2020-01-01 00:00:00+06:00", "2020-01-01 00:00:00+01:00"]
330-
pd.to_datetime(data, utc=True)
330+
data = ["2020-01-01 00:00:00+06:00", "2020-01-01 00:00:00+01:00"]
331+
pd.to_datetime(data, utc=True)
331332
332333
To create a ``Series`` with mixed offsets and ``object`` dtype, please use ``apply``
333-
and ``datetime.datetime.strptime``.
334+
and ``datetime.datetime.strptime``:
335+
336+
.. ipython:: python
337+
338+
import datetime as dt
339+
340+
data = ["2020-01-01 00:00:00+06:00", "2020-01-01 00:00:00+01:00"]
341+
pd.Series(data).apply(lambda x: dt.datetime.strptime(x, '%Y-%m-%d %H:%M:%S%z'))
334342
335343
Other Deprecations
336344
~~~~~~~~~~~~~~~~~~

pandas/_libs/tslib.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,8 @@ cdef _array_to_datetime_object(
673673
"In a future version of pandas, parsing datetimes with mixed time "
674674
"zones will raise a warning unless `utc=True`. "
675675
"Please specify `utc=True` to opt in to the new behaviour "
676-
"and silence this warning.",
676+
"and silence this warning. To create a `Series` with mixed offsets and "
677+
"`object` dtype, please use `apply` and `datetime.datetime.strptime`",
677678
FutureWarning,
678679
stacklevel=find_stack_level(),
679680
)

pandas/core/tools/datetimes.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,9 @@ def _return_parsed_timezone_results(
357357
warnings.warn(
358358
"In a future version of pandas, parsing datetimes with mixed time "
359359
"zones will raise a warning unless `utc=True`. Please specify `utc=True` "
360-
"to opt in to the new behaviour and silence this warning.",
360+
"to opt in to the new behaviour and silence this warning. "
361+
"To create a `Series` with mixed offsets and `object` dtype, "
362+
"please use `apply` and `datetime.datetime.strptime`",
361363
FutureWarning,
362364
stacklevel=find_stack_level(),
363365
)
@@ -788,7 +790,8 @@ def to_datetime(
788790
In a future version of pandas, parsing datetimes with mixed time
789791
zones will raise a warning unless `utc=True`.
790792
Please specify `utc=True` to opt in to the new behaviour
791-
and silence this warning.
793+
and silence this warning. To create a `Series` with mixed offsets and
794+
`object` dtype, please use `apply` and `datetime.datetime.strptime`.
792795
793796
See also: pandas general documentation about `timezone conversion and
794797
localization
@@ -1021,7 +1024,9 @@ def to_datetime(
10211024
... '2020-10-25 04:00 +0100']) # doctest: +SKIP
10221025
FutureWarning: In a future version of pandas, parsing datetimes with mixed
10231026
time zones will raise a warning unless `utc=True`. Please specify `utc=True`
1024-
to opt in to the new behaviour and silence this warning.
1027+
to opt in to the new behaviour and silence this warning. To create a `Series`
1028+
with mixed offsets and `object` dtype, please use `apply` and
1029+
`datetime.datetime.strptime`.
10251030
Index([2020-10-25 02:00:00+02:00, 2020-10-25 04:00:00+01:00],
10261031
dtype='object')
10271032
@@ -1033,7 +1038,9 @@ def to_datetime(
10331038
... datetime(2020, 1, 1, 3, 0)]) # doctest: +SKIP
10341039
FutureWarning: In a future version of pandas, parsing datetimes with mixed
10351040
time zones will raise a warning unless `utc=True`. Please specify `utc=True`
1036-
to opt in to the new behaviour and silence this warning.
1041+
to opt in to the new behaviour and silence this warning. To create a `Series`
1042+
with mixed offsets and `object` dtype, please use `apply` and
1043+
`datetime.datetime.strptime`.
10371044
Index([2020-01-01 01:00:00-01:00, 2020-01-01 03:00:00], dtype='object')
10381045
10391046
|

0 commit comments

Comments
 (0)