Skip to content

Commit e45e3b4

Browse files
multilocjreback
authored andcommitted
BUG: date_range breaks with tz-aware start/end dates and closed intervals
closes #12409 Author: hcontrast <compandu@gmail.com> Closes #12410 from hcontrast/bugfix_12409 and squashes the following commits: fba0724 [hcontrast] BUG fix GH12409 do comparison for closed intervals on UTC basis prior to tz conversion added tests for date_range covering case of tz-aware start/end dates with closed ranges
1 parent 4048193 commit e45e3b4

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

doc/source/whatsnew/v0.18.0.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ Bug Fixes
10301030
- Bug in ``GroupBy.size`` when data-frame is empty. (:issue:`11699`)
10311031
- Bug in ``Period.end_time`` when a multiple of time period is requested (:issue:`11738`)
10321032
- Regression in ``.clip`` with tz-aware datetimes (:issue:`11838`)
1033-
- Bug in ``date_range`` when the boundaries fell on the frequency (:issue:`11804`)
1033+
- Bug in ``date_range`` when the boundaries fell on the frequency (:issue:`11804`, :issue:`12409`)
10341034
- Bug in consistency of passing nested dicts to ``.groupby(...).agg(...)`` (:issue:`9052`)
10351035
- Accept unicode in ``Timedelta`` constructor (:issue:`11995`)
10361036
- Bug in value label reading for ``StataReader`` when reading incrementally (:issue:`12014`)

pandas/tseries/index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,12 +528,12 @@ def _generate(cls, start, end, periods, name, offset,
528528
ambiguous=ambiguous)
529529
index = index.view(_NS_DTYPE)
530530

531-
index = cls._simple_new(index, name=name, freq=offset, tz=tz)
532531
if not left_closed and len(index) and index[0] == start:
533532
index = index[1:]
534533
if not right_closed and len(index) and index[-1] == end:
535534
index = index[:-1]
536535

536+
index = cls._simple_new(index, name=name, freq=offset, tz=tz)
537537
return index
538538

539539
@property

pandas/tseries/tests/test_daterange.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,36 @@ def test_range_closed(self):
497497
self.assertTrue(expected_left.equals(left))
498498
self.assertTrue(expected_right.equals(right))
499499

500+
def test_range_closed_with_tz_aware_start_end(self):
501+
# GH12409
502+
begin = Timestamp('2011/1/1', tz='US/Eastern')
503+
end = Timestamp('2014/1/1', tz='US/Eastern')
504+
505+
for freq in ["3D", "2M", "7W", "3H", "A"]:
506+
closed = date_range(begin, end, closed=None, freq=freq)
507+
left = date_range(begin, end, closed="left", freq=freq)
508+
right = date_range(begin, end, closed="right", freq=freq)
509+
expected_left = left
510+
expected_right = right
511+
512+
if end == closed[-1]:
513+
expected_left = closed[:-1]
514+
if begin == closed[0]:
515+
expected_right = closed[1:]
516+
517+
self.assertTrue(expected_left.equals(left))
518+
self.assertTrue(expected_right.equals(right))
519+
520+
# test with default frequency, UTC
521+
begin = Timestamp('2011/1/1', tz='UTC')
522+
end = Timestamp('2014/1/1', tz='UTC')
523+
524+
intervals = ['left', 'right', None]
525+
for i in intervals:
526+
result = date_range(start=begin, end=end, closed=i)
527+
self.assertEqual(result[0], begin)
528+
self.assertEqual(result[-1], end)
529+
500530
def test_range_closed_boundary(self):
501531
# GH 11804
502532
for closed in ['right', 'left', None]:

0 commit comments

Comments
 (0)