Skip to content

Commit 7b33d8e

Browse files
natmokvalpmhatre1
authored andcommitted
CLN: unify error message when parsing datetimes with mixed time zones with utc=False (pandas-dev#57653)
* unify error msg for mixed offsets when utc=False * capitalize the letter p in error message
1 parent 4240746 commit 7b33d8e

File tree

6 files changed

+27
-21
lines changed

6 files changed

+27
-21
lines changed

pandas/_libs/tslib.pyx

+3-2
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,8 @@ cpdef array_to_datetime(
598598
is_same_offsets = len(out_tzoffset_vals) == 1
599599
if not is_same_offsets:
600600
raise ValueError(
601-
"cannot parse datetimes with mixed time zones unless `utc=True`"
601+
"Mixed timezones detected. Pass utc=True in to_datetime "
602+
"or tz='UTC' in DatetimeIndex to convert to a common timezone."
602603
)
603604
elif state.found_naive or state.found_other:
604605
# e.g. test_to_datetime_mixed_awareness_mixed_types
@@ -610,7 +611,7 @@ cpdef array_to_datetime(
610611
if not tz_compare(tz_out, tz_out2):
611612
# e.g. test_to_datetime_mixed_tzs_mixed_types
612613
raise ValueError(
613-
"Mixed timezones detected. pass utc=True in to_datetime "
614+
"Mixed timezones detected. Pass utc=True in to_datetime "
614615
"or tz='UTC' in DatetimeIndex to convert to a common timezone."
615616
)
616617
# e.g. test_to_datetime_mixed_types_matching_tzs

pandas/_libs/tslibs/strptime.pyx

+6-3
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,8 @@ def array_strptime(
503503
is_same_offsets = len(out_tzoffset_vals) == 1
504504
if not is_same_offsets or (state.found_naive or state.found_other):
505505
raise ValueError(
506-
"cannot parse datetimes with mixed time zones unless `utc=True`"
506+
"Mixed timezones detected. Pass utc=True in to_datetime "
507+
"or tz='UTC' in DatetimeIndex to convert to a common timezone."
507508
)
508509
elif tz_out is not None:
509510
# GH#55693
@@ -512,7 +513,8 @@ def array_strptime(
512513
if not tz_compare(tz_out, tz_out2):
513514
# e.g. test_to_datetime_mixed_offsets_with_utc_false_removed
514515
raise ValueError(
515-
"cannot parse datetimes with mixed time zones unless `utc=True`"
516+
"Mixed timezones detected. Pass utc=True in to_datetime "
517+
"or tz='UTC' in DatetimeIndex to convert to a common timezone."
516518
)
517519
# e.g. test_guess_datetime_format_with_parseable_formats
518520
else:
@@ -523,7 +525,8 @@ def array_strptime(
523525
if tz_out and (state.found_other or state.found_naive_str):
524526
# found_other indicates a tz-naive int, float, dt64, or date
525527
raise ValueError(
526-
"cannot parse datetimes with mixed time zones unless `utc=True`"
528+
"Mixed timezones detected. Pass utc=True in to_datetime "
529+
"or tz='UTC' in DatetimeIndex to convert to a common timezone."
527530
)
528531

529532
if infer_reso:

pandas/core/tools/datetimes.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,8 @@ def to_datetime(
932932
>>> pd.to_datetime(
933933
... ["2020-10-25 02:00 +0200", "2020-10-25 04:00 +0100"]
934934
... ) # doctest: +SKIP
935-
ValueError: cannot parse datetimes with mixed time zones unless `utc=True`
935+
ValueError: Mixed timezones detected. Pass utc=True in to_datetime
936+
or tz='UTC' in DatetimeIndex to convert to a common timezone.
936937
937938
- To create a :class:`Series` with mixed offsets and ``object`` dtype, please use
938939
:meth:`Series.apply` and :func:`datetime.datetime.strptime`:
@@ -951,7 +952,8 @@ def to_datetime(
951952
>>> pd.to_datetime(
952953
... ["2020-01-01 01:00:00-01:00", datetime(2020, 1, 1, 3, 0)]
953954
... ) # doctest: +SKIP
954-
ValueError: cannot parse datetimes with mixed time zones unless `utc=True`
955+
ValueError: Mixed timezones detected. Pass utc=True in to_datetime
956+
or tz='UTC' in DatetimeIndex to convert to a common timezone.
955957
956958
|
957959

pandas/tests/indexes/datetimes/test_constructors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def test_construction_index_with_mixed_timezones(self):
296296
tm.assert_index_equal(result, exp, exact=True)
297297
assert not isinstance(result, DatetimeIndex)
298298

299-
msg = "cannot parse datetimes with mixed time zones unless `utc=True`"
299+
msg = "Mixed timezones detected. Pass utc=True in to_datetime"
300300
with pytest.raises(ValueError, match=msg):
301301
DatetimeIndex(["2013-11-02 22:00-05:00", "2013-11-03 22:00-06:00"])
302302

pandas/tests/tools/test_to_datetime.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ def test_to_datetime_parse_tzname_or_tzoffset_utc_false_removed(
463463
self, fmt, dates, expected_dates
464464
):
465465
# GH#13486, GH#50887, GH#57275
466-
msg = "cannot parse datetimes with mixed time zones unless `utc=True`"
466+
msg = "Mixed timezones detected. Pass utc=True in to_datetime"
467467
with pytest.raises(ValueError, match=msg):
468468
to_datetime(dates, format=fmt)
469469

@@ -646,7 +646,7 @@ def test_to_datetime_mixed_dt_and_str_with_format_mixed_offsets_utc_false_remove
646646
args = ["2000-01-01 01:00:00", "2000-01-01 02:00:00+00:00"]
647647
ts1 = constructor(args[0])
648648
ts2 = args[1]
649-
msg = "cannot parse datetimes with mixed time zones unless `utc=True`"
649+
msg = "Mixed timezones detected. Pass utc=True in to_datetime"
650650

651651
with pytest.raises(ValueError, match=msg):
652652
to_datetime([ts1, ts2], format=fmt, utc=False)
@@ -679,7 +679,7 @@ def test_to_datetime_mixed_offsets_with_none_tz_utc_false_removed(
679679
):
680680
# https://github.com/pandas-dev/pandas/issues/50071
681681
# GH#57275
682-
msg = "cannot parse datetimes with mixed time zones unless `utc=True`"
682+
msg = "Mixed timezones detected. Pass utc=True in to_datetime"
683683

684684
with pytest.raises(ValueError, match=msg):
685685
to_datetime(
@@ -1152,7 +1152,7 @@ def test_to_datetime_different_offsets_removed(self, cache):
11521152
ts_string_1 = "March 1, 2018 12:00:00+0400"
11531153
ts_string_2 = "March 1, 2018 12:00:00+0500"
11541154
arr = [ts_string_1] * 5 + [ts_string_2] * 5
1155-
msg = "cannot parse datetimes with mixed time zones unless `utc=True`"
1155+
msg = "Mixed timezones detected. Pass utc=True in to_datetime"
11561156
with pytest.raises(ValueError, match=msg):
11571157
to_datetime(arr, cache=cache)
11581158

@@ -1509,7 +1509,7 @@ def test_to_datetime_coerce(self):
15091509
"March 1, 2018 12:00:00+0500",
15101510
"20100240",
15111511
]
1512-
msg = "cannot parse datetimes with mixed time zones unless `utc=True`"
1512+
msg = "Mixed timezones detected. Pass utc=True in to_datetime"
15131513
with pytest.raises(ValueError, match=msg):
15141514
to_datetime(ts_strings, errors="coerce")
15151515

@@ -1581,7 +1581,7 @@ def test_iso_8601_strings_with_same_offset(self):
15811581
def test_iso_8601_strings_with_different_offsets_removed(self):
15821582
# GH#17697, GH#11736, GH#50887, GH#57275
15831583
ts_strings = ["2015-11-18 15:30:00+05:30", "2015-11-18 16:30:00+06:30", NaT]
1584-
msg = "cannot parse datetimes with mixed time zones unless `utc=True`"
1584+
msg = "Mixed timezones detected. Pass utc=True in to_datetime"
15851585
with pytest.raises(ValueError, match=msg):
15861586
to_datetime(ts_strings)
15871587

@@ -1608,7 +1608,7 @@ def test_mixed_offsets_with_native_datetime_utc_false_raises(self):
16081608
ser = Series(vals)
16091609
assert all(ser[i] is vals[i] for i in range(len(vals))) # GH#40111
16101610

1611-
msg = "cannot parse datetimes with mixed time zones unless `utc=True`"
1611+
msg = "Mixed timezones detected. Pass utc=True in to_datetime"
16121612
with pytest.raises(ValueError, match=msg):
16131613
to_datetime(ser)
16141614

@@ -1673,7 +1673,7 @@ def test_to_datetime_fixed_offset(self):
16731673
)
16741674
def test_to_datetime_mixed_offsets_with_utc_false_removed(self, date):
16751675
# GH#50887, GH#57275
1676-
msg = "cannot parse datetimes with mixed time zones unless `utc=True`"
1676+
msg = "Mixed timezones detected. Pass utc=True in to_datetime"
16771677
with pytest.raises(ValueError, match=msg):
16781678
to_datetime(date, utc=False)
16791679

@@ -3463,7 +3463,7 @@ def test_to_datetime_with_empty_str_utc_false_format_mixed():
34633463

34643464
def test_to_datetime_with_empty_str_utc_false_offsets_and_format_mixed():
34653465
# GH#50887, GH#57275
3466-
msg = "cannot parse datetimes with mixed time zones unless `utc=True`"
3466+
msg = "Mixed timezones detected. Pass utc=True in to_datetime"
34673467

34683468
with pytest.raises(ValueError, match=msg):
34693469
to_datetime(
@@ -3479,7 +3479,7 @@ def test_to_datetime_mixed_tzs_mixed_types():
34793479
arr = [ts, dtstr]
34803480

34813481
msg = (
3482-
"Mixed timezones detected. pass utc=True in to_datetime or tz='UTC' "
3482+
"Mixed timezones detected. Pass utc=True in to_datetime or tz='UTC' "
34833483
"in DatetimeIndex to convert to a common timezone"
34843484
)
34853485
with pytest.raises(ValueError, match=msg):
@@ -3578,15 +3578,15 @@ def test_to_datetime_mixed_awareness_mixed_types(aware_val, naive_val, naive_fir
35783578
to_datetime(vec, utc=True)
35793579

35803580
else:
3581-
msg = "cannot parse datetimes with mixed time zones unless `utc=True`"
3581+
msg = "Mixed timezones detected. Pass utc=True in to_datetime"
35823582
with pytest.raises(ValueError, match=msg):
35833583
to_datetime(vec)
35843584

35853585
# No warning/error with utc=True
35863586
to_datetime(vec, utc=True)
35873587

35883588
if both_strs:
3589-
msg = "cannot parse datetimes with mixed time zones unless `utc=True`"
3589+
msg = "Mixed timezones detected. Pass utc=True in to_datetime"
35903590
with pytest.raises(ValueError, match=msg):
35913591
to_datetime(vec, format="mixed")
35923592
with pytest.raises(ValueError, match=msg):

pandas/tests/tslibs/test_array_to_datetime.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def test_parsing_different_timezone_offsets():
200200
data = ["2015-11-18 15:30:00+05:30", "2015-11-18 15:30:00+06:30"]
201201
data = np.array(data, dtype=object)
202202

203-
msg = "cannot parse datetimes with mixed time zones unless `utc=True`"
203+
msg = "Mixed timezones detected. Pass utc=True in to_datetime"
204204
with pytest.raises(ValueError, match=msg):
205205
tslib.array_to_datetime(data)
206206

0 commit comments

Comments
 (0)