Skip to content

Fix tzaware dates mismatch but no exception raised #18488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 26, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.21.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Conversion
- Bug in :class:`DatetimeIndex` subtracting datetimelike from DatetimeIndex could fail to overflow (:issue:`18020`)
- Bug in :meth:`IntervalIndex.copy` when copying and ``IntervalIndex`` with non-default ``closed`` (:issue:`18339`)
- 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`)
-
- 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`)
-

Indexing
Expand Down
7 changes: 3 additions & 4 deletions pandas/_libs/tslibs/timezones.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,9 @@ cdef object get_dst_info(object tz):
def infer_tzinfo(start, end):
if start is not None and end is not None:
tz = start.tzinfo
if end.tzinfo:
if not (get_timezone(tz) == get_timezone(end.tzinfo)):
msg = 'Inputs must both have the same timezone, {tz1} != {tz2}'
raise AssertionError(msg.format(tz1=tz, tz2=end.tzinfo))
if not (get_timezone(tz) == get_timezone(end.tzinfo)):
msg = 'Inputs must both have the same timezone, {tz1} != {tz2}'
raise AssertionError(msg.format(tz1=tz, tz2=end.tzinfo))
elif start is not None:
tz = start.tzinfo
elif end is not None:
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/indexes/datetimes/test_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,25 @@ def test_precision_finer_than_offset(self):
tm.assert_index_equal(result1, expected1)
tm.assert_index_equal(result2, expected2)

def test_mismatching_tz_raises_err(self):
#issue 18488
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dt1_tz = pd.Timestamp('2017-01-01', tz='US/Eastern')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the issue number here as a comment

dt1_no_tz = pd.Timestamp('2017-01-01')
dt2_tz = pd.Timestamp('2017-01-04', tz='US/Eastern')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you parametrize this test (IOW pass in start, end), cases for raising include:

  • one naive, one with a tz & reversed
  • different timezones & reversed

I don't think we care about the freq here.

dt2_no_tz = pd.Timestamp('2017-01-04')
dt1_diff_tz = pd.Timestamp('2017-01-01', tz='Europe/London')
dt2_diff_tz = pd.Timestamp('2017-01-04', tz='Europe/London')

def assert_tz_raises_exception(start, end):
with pytest.raises(TypeError):
pd.date_range(start, end)
with pytest.raises(TypeError):
pd.DatetimeIndexstart(start, end)

assert_tz_raises_exception(dt1_no_tz, dt2_tz)
assert_tz_raises_exception(dt1_tz, dt2_no_tz)
assert_tz_raises_exception(dt1_tz, dt2_diff_tz)
assert_tz_raises_exception(dt1_diff_tz, dt2_tz)

class TestBusinessDateRange(object):

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/tseries/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def test_with_tz(self):

# datetimes with tzinfo set
dr = bdate_range(datetime(2005, 1, 1, tzinfo=pytz.utc),
'1/1/2009', tz=pytz.utc)
datetime(2009, 1, 1, tzinfo=pytz.utc))

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