Skip to content

Commit 237ed87

Browse files
aschadeTomAugspurger
authored andcommitted
Fix tzaware dates mismatch but no exception raised (pandas-dev#18488)
(cherry picked from commit d101064)
1 parent e530347 commit 237ed87

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

doc/source/whatsnew/v0.21.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Conversion
6464
- Bug in :class:`DatetimeIndex` subtracting datetimelike from DatetimeIndex could fail to overflow (:issue:`18020`)
6565
- Bug in :meth:`IntervalIndex.copy` when copying and ``IntervalIndex`` with non-default ``closed`` (:issue:`18339`)
6666
- Bug in :func:`DataFrame.to_dict` where columns of datetime that are tz-aware were not converted to required arrays when used with ``orient='records'``, raising``TypeError` (:issue:`18372`)
67-
-
67+
- Bug in :class:`DateTimeIndex` and :meth:`date_range` where mismatching tz-aware ``start`` and ``end`` timezones would not raise an err if ``end.tzinfo`` is None (:issue:`18431`)
6868
-
6969

7070
Indexing

pandas/_libs/tslibs/timezones.pyx

+3-4
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,9 @@ cdef object get_dst_info(object tz):
283283
def infer_tzinfo(start, end):
284284
if start is not None and end is not None:
285285
tz = start.tzinfo
286-
if end.tzinfo:
287-
if not (get_timezone(tz) == get_timezone(end.tzinfo)):
288-
msg = 'Inputs must both have the same timezone, {tz1} != {tz2}'
289-
raise AssertionError(msg.format(tz1=tz, tz2=end.tzinfo))
286+
if not (get_timezone(tz) == get_timezone(end.tzinfo)):
287+
msg = 'Inputs must both have the same timezone, {tz1} != {tz2}'
288+
raise AssertionError(msg.format(tz1=tz, tz2=end.tzinfo))
290289
elif start is not None:
291290
tz = start.tzinfo
292291
elif end is not None:

pandas/tests/indexes/datetimes/test_date_range.py

+16
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,22 @@ def test_precision_finer_than_offset(self):
234234
tm.assert_index_equal(result1, expected1)
235235
tm.assert_index_equal(result2, expected2)
236236

237+
dt1, dt2 = '2017-01-01', '2017-01-01'
238+
tz1, tz2 = 'US/Eastern', 'Europe/London'
239+
240+
@pytest.mark.parametrize("start,end", [
241+
(pd.Timestamp(dt1, tz=tz1), pd.Timestamp(dt2)),
242+
(pd.Timestamp(dt1), pd.Timestamp(dt2, tz=tz2)),
243+
(pd.Timestamp(dt1, tz=tz1), pd.Timestamp(dt2, tz=tz2)),
244+
(pd.Timestamp(dt1, tz=tz2), pd.Timestamp(dt2, tz=tz1))
245+
])
246+
def test_mismatching_tz_raises_err(self, start, end):
247+
# issue 18488
248+
with pytest.raises(TypeError):
249+
pd.date_range(start, end)
250+
with pytest.raises(TypeError):
251+
pd.DatetimeIndex(start, end, freq=BDay())
252+
237253

238254
class TestBusinessDateRange(object):
239255

pandas/tests/tseries/test_timezones.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ def test_with_tz(self):
424424

425425
# datetimes with tzinfo set
426426
dr = bdate_range(datetime(2005, 1, 1, tzinfo=pytz.utc),
427-
'1/1/2009', tz=pytz.utc)
427+
datetime(2009, 1, 1, tzinfo=pytz.utc))
428428

429429
pytest.raises(Exception, bdate_range,
430430
datetime(2005, 1, 1, tzinfo=pytz.utc), '1/1/2009',

0 commit comments

Comments
 (0)