Skip to content

Commit 7568018

Browse files
committed
Merge pull request #7909 from rockg/master
Remove from start/end dates if tz is not None (#7901, #7835)
2 parents 472de31 + 8da210c commit 7568018

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

doc/source/timeseries.rst

+6
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,12 @@ tz-aware data to another time zone:
13971397
is localized using one version and operated on with a different version.
13981398
See :ref:`here<io.hdf5-notes>` for how to handle such a situation.
13991399

1400+
.. warning::
1401+
1402+
It is incorrect to pass a timezone directly into the ``datetime.datetime`` constructor (e.g.,
1403+
``datetime.datetime(2011, 1, 1, tz=timezone('US/Eastern'))``. Instead, the datetime
1404+
needs to be localized using the the localize method on the timezone.
1405+
14001406
Under the hood, all timestamps are stored in UTC. Scalar values from a
14011407
``DatetimeIndex`` with a time zone will have their fields (day, hour, minute)
14021408
localized to the time zone. However, timestamps with the same UTC value are

doc/source/v0.15.0.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,9 @@ Bug Fixes
384384

385385

386386
- Bug in ``GroupBy.filter()`` where fast path vs. slow path made the filter
387-
return a non scalar value that appeared valid but wasnt' (:issue:`7870`).
387+
return a non scalar value that appeared valid but wasn't (:issue:`7870`).
388+
- Bug in ``date_range()``/``DatetimeIndex()`` when the timezone was inferred from input dates yet incorrect
389+
times were returned when crossing DST boundaries (:issue:`7835`, :issue:`7901`).
388390

389391

390392

pandas/tseries/index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def _generate(cls, start, end, periods, name, offset,
416416

417417
else:
418418

419-
if inferred_tz is None and tz is not None:
419+
if tz is not None:
420420
# naive dates
421421
if start is not None and start.tz is not None:
422422
start = start.replace(tzinfo=None)

pandas/tseries/tests/test_daterange.py

+32-6
Original file line numberDiff line numberDiff line change
@@ -354,25 +354,51 @@ def test_range_bug(self):
354354
def test_range_tz_pytz(self):
355355
# GH 2906
356356
tm._skip_if_no_pytz()
357-
from pytz import timezone as tz
357+
from pytz import timezone
358358

359-
start = datetime(2011, 1, 1, tzinfo=tz('US/Eastern'))
360-
end = datetime(2011, 1, 3, tzinfo=tz('US/Eastern'))
359+
tz = timezone('US/Eastern')
360+
start = tz.localize(datetime(2011, 1, 1))
361+
end = tz.localize(datetime(2011, 1, 3))
361362

362363
dr = date_range(start=start, periods=3)
363-
self.assertEqual(dr.tz, tz('US/Eastern'))
364+
self.assertEqual(dr.tz.zone, tz.zone)
364365
self.assertEqual(dr[0], start)
365366
self.assertEqual(dr[2], end)
366367

367368
dr = date_range(end=end, periods=3)
368-
self.assertEqual(dr.tz, tz('US/Eastern'))
369+
self.assertEqual(dr.tz.zone, tz.zone)
369370
self.assertEqual(dr[0], start)
370371
self.assertEqual(dr[2], end)
371372

372373
dr = date_range(start=start, end=end)
373-
self.assertEqual(dr.tz, tz('US/Eastern'))
374+
self.assertEqual(dr.tz.zone, tz.zone)
374375
self.assertEqual(dr[0], start)
375376
self.assertEqual(dr[2], end)
377+
378+
def test_range_tz_dst_straddle_pytz(self):
379+
380+
tm._skip_if_no_pytz()
381+
from pytz import timezone
382+
tz = timezone('US/Eastern')
383+
dates = [(tz.localize(datetime(2014, 3, 6)),
384+
tz.localize(datetime(2014, 3, 12))),
385+
(tz.localize(datetime(2013, 11, 1)),
386+
tz.localize(datetime(2013, 11, 6)))]
387+
for (start, end) in dates:
388+
dr = date_range(start, end, freq='D')
389+
self.assertEqual(dr[0], start)
390+
self.assertEqual(dr[-1], end)
391+
self.assertEqual(np.all(dr.hour==0), True)
392+
393+
dr = date_range(start, end, freq='D', tz='US/Eastern')
394+
self.assertEqual(dr[0], start)
395+
self.assertEqual(dr[-1], end)
396+
self.assertEqual(np.all(dr.hour==0), True)
397+
398+
dr = date_range(start.replace(tzinfo=None), end.replace(tzinfo=None), freq='D', tz='US/Eastern')
399+
self.assertEqual(dr[0], start)
400+
self.assertEqual(dr[-1], end)
401+
self.assertEqual(np.all(dr.hour==0), True)
376402

377403
def test_range_tz_dateutil(self):
378404
# GH 2906

0 commit comments

Comments
 (0)