From 321e95f058b3d62e78a6d2b3d87d6fd7876fc2c9 Mon Sep 17 00:00:00 2001 From: Dea Leon Date: Sun, 19 Mar 2023 17:56:01 +0100 Subject: [PATCH 1/3] BUG Added TypeError msg --- pandas/core/arrays/datetimes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 1624870705b8f..f3b8cd6a17447 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -2199,7 +2199,7 @@ def objects_to_datetime64ns( # datetimes but they have conflicting timezones/awareness if allow_object: return result, tz_parsed - raise TypeError(result) + raise TypeError("DatetimeIndex has mixed timezones") else: # pragma: no cover # GH#23675 this TypeError should never be hit, whereas the TypeError # in the object-dtype branch above is reachable. From e245d0765e6dce8b9a92301394419b8b7744d286 Mon Sep 17 00:00:00 2001 From: Dea Leon Date: Sun, 19 Mar 2023 18:34:30 +0100 Subject: [PATCH 2/3] Added whatsnew line to Timezones --- doc/source/whatsnew/v2.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 0856382f6cbdd..ec5d08e75f0e4 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -1197,6 +1197,7 @@ Timezones - Bug in :func:`to_datetime` was failing to parse date strings with timezone name when ``format`` was specified with ``%Z`` (:issue:`49748`) - Better error message when passing invalid values to ``ambiguous`` parameter in :meth:`Timestamp.tz_localize` (:issue:`49565`) - Bug in string parsing incorrectly allowing a :class:`Timestamp` to be constructed with an invalid timezone, which would raise when trying to print (:issue:`50668`) +- Corrected TypeError message in :func:`objects_to_datetime64ns` to inform that DatetimeIndex has mixed timezones (:issue:`50974`) Numeric ^^^^^^^ From c38bf536696a4069536187bda29bae576a92f189 Mon Sep 17 00:00:00 2001 From: Dea Leon Date: Mon, 20 Mar 2023 16:22:28 +0100 Subject: [PATCH 3/3] Added msg test --- pandas/tests/indexes/datetimes/test_constructors.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/tests/indexes/datetimes/test_constructors.py b/pandas/tests/indexes/datetimes/test_constructors.py index 689e6cdb47058..82f75d9ff80e5 100644 --- a/pandas/tests/indexes/datetimes/test_constructors.py +++ b/pandas/tests/indexes/datetimes/test_constructors.py @@ -285,6 +285,10 @@ def test_construction_index_with_mixed_timezones(self): tm.assert_index_equal(result, exp, exact=True) assert not isinstance(result, DatetimeIndex) + msg = "DatetimeIndex has mixed timezones" + with pytest.raises(TypeError, match=msg): + DatetimeIndex(["2013-11-02 22:00-05:00", "2013-11-03 22:00-06:00"]) + # length = 1 result = Index([Timestamp("2011-01-01")], name="idx") exp = DatetimeIndex([Timestamp("2011-01-01")], name="idx")