Skip to content

Commit d101064

Browse files
aschadejreback
authored andcommitted
Fix tzaware dates mismatch but no exception raised (#18488)
1 parent 68b66ab commit d101064

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
@@ -284,10 +284,9 @@ cdef object get_dst_info(object tz):
284284
def infer_tzinfo(start, end):
285285
if start is not None and end is not None:
286286
tz = start.tzinfo
287-
if end.tzinfo:
288-
if not (get_timezone(tz) == get_timezone(end.tzinfo)):
289-
msg = 'Inputs must both have the same timezone, {tz1} != {tz2}'
290-
raise AssertionError(msg.format(tz1=tz, tz2=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))
291290
elif start is not None:
292291
tz = start.tzinfo
293292
elif end is not None:

pandas/tests/indexes/datetimes/test_date_range.py

+16
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,22 @@ def test_precision_finer_than_offset(self):
290290
tm.assert_index_equal(result1, expected1)
291291
tm.assert_index_equal(result2, expected2)
292292

293+
dt1, dt2 = '2017-01-01', '2017-01-01'
294+
tz1, tz2 = 'US/Eastern', 'Europe/London'
295+
296+
@pytest.mark.parametrize("start,end", [
297+
(pd.Timestamp(dt1, tz=tz1), pd.Timestamp(dt2)),
298+
(pd.Timestamp(dt1), pd.Timestamp(dt2, tz=tz2)),
299+
(pd.Timestamp(dt1, tz=tz1), pd.Timestamp(dt2, tz=tz2)),
300+
(pd.Timestamp(dt1, tz=tz2), pd.Timestamp(dt2, tz=tz1))
301+
])
302+
def test_mismatching_tz_raises_err(self, start, end):
303+
# issue 18488
304+
with pytest.raises(TypeError):
305+
pd.date_range(start, end)
306+
with pytest.raises(TypeError):
307+
pd.DatetimeIndex(start, end, freq=BDay())
308+
293309

294310
class TestBusinessDateRange(object):
295311

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)