Skip to content

Commit 900ffa3

Browse files
authored
BUG: Corrected TypeError message (#52074)
1 parent 532ed6f commit 900ffa3

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,7 @@ Timezones
11971197
- Bug in :func:`to_datetime` was failing to parse date strings with timezone name when ``format`` was specified with ``%Z`` (:issue:`49748`)
11981198
- Better error message when passing invalid values to ``ambiguous`` parameter in :meth:`Timestamp.tz_localize` (:issue:`49565`)
11991199
- 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`)
1200+
- Corrected TypeError message in :func:`objects_to_datetime64ns` to inform that DatetimeIndex has mixed timezones (:issue:`50974`)
12001201

12011202
Numeric
12021203
^^^^^^^

pandas/core/arrays/datetimes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2200,7 +2200,7 @@ def objects_to_datetime64ns(
22002200
# datetimes but they have conflicting timezones/awareness
22012201
if allow_object:
22022202
return result, tz_parsed
2203-
raise TypeError(result)
2203+
raise TypeError("DatetimeIndex has mixed timezones")
22042204
else: # pragma: no cover
22052205
# GH#23675 this TypeError should never be hit, whereas the TypeError
22062206
# in the object-dtype branch above is reachable.

pandas/tests/indexes/datetimes/test_constructors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ def test_construction_index_with_mixed_timezones(self):
285285
tm.assert_index_equal(result, exp, exact=True)
286286
assert not isinstance(result, DatetimeIndex)
287287

288+
msg = "DatetimeIndex has mixed timezones"
289+
with pytest.raises(TypeError, match=msg):
290+
DatetimeIndex(["2013-11-02 22:00-05:00", "2013-11-03 22:00-06:00"])
291+
288292
# length = 1
289293
result = Index([Timestamp("2011-01-01")], name="idx")
290294
exp = DatetimeIndex([Timestamp("2011-01-01")], name="idx")

0 commit comments

Comments
 (0)