Skip to content

Remove from start/end dates if tz is not None (#7901, #7835) #7909

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 1 commit into from
Aug 5, 2014
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions doc/source/timeseries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,12 @@ tz-aware data to another time zone:
is localized using one version and operated on with a different version.
See :ref:`here<io.hdf5-notes>` for how to handle such a situation.

.. warning::

It is incorrect to pass a timezone directly into the ``datetime.datetime`` constructor (e.g.,
``datetime.datetime(2011, 1, 1, tz=timezone('US/Eastern'))``. Instead, the datetime
needs to be localized using the the localize method on the timezone.

Under the hood, all timestamps are stored in UTC. Scalar values from a
``DatetimeIndex`` with a time zone will have their fields (day, hour, minute)
localized to the time zone. However, timestamps with the same UTC value are
Expand Down
4 changes: 3 additions & 1 deletion doc/source/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,9 @@ Bug Fixes


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



Expand Down
2 changes: 1 addition & 1 deletion pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def _generate(cls, start, end, periods, name, offset,

else:

if inferred_tz is None and tz is not None:
if tz is not None:
# naive dates
if start is not None and start.tz is not None:
start = start.replace(tzinfo=None)
Expand Down
38 changes: 32 additions & 6 deletions pandas/tseries/tests/test_daterange.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,25 +354,51 @@ def test_range_bug(self):
def test_range_tz_pytz(self):
# GH 2906
tm._skip_if_no_pytz()
from pytz import timezone as tz
from pytz import timezone

start = datetime(2011, 1, 1, tzinfo=tz('US/Eastern'))
end = datetime(2011, 1, 3, tzinfo=tz('US/Eastern'))
tz = timezone('US/Eastern')
start = tz.localize(datetime(2011, 1, 1))
end = tz.localize(datetime(2011, 1, 3))

dr = date_range(start=start, periods=3)
self.assertEqual(dr.tz, tz('US/Eastern'))
self.assertEqual(dr.tz.zone, tz.zone)
self.assertEqual(dr[0], start)
self.assertEqual(dr[2], end)

dr = date_range(end=end, periods=3)
self.assertEqual(dr.tz, tz('US/Eastern'))
self.assertEqual(dr.tz.zone, tz.zone)
self.assertEqual(dr[0], start)
self.assertEqual(dr[2], end)

dr = date_range(start=start, end=end)
self.assertEqual(dr.tz, tz('US/Eastern'))
self.assertEqual(dr.tz.zone, tz.zone)
self.assertEqual(dr[0], start)
self.assertEqual(dr[2], end)

def test_range_tz_dst_straddle_pytz(self):

tm._skip_if_no_pytz()
from pytz import timezone
tz = timezone('US/Eastern')
dates = [(tz.localize(datetime(2014, 3, 6)),
tz.localize(datetime(2014, 3, 12))),
(tz.localize(datetime(2013, 11, 1)),
tz.localize(datetime(2013, 11, 6)))]
for (start, end) in dates:
dr = date_range(start, end, freq='D')
self.assertEqual(dr[0], start)
self.assertEqual(dr[-1], end)
self.assertEqual(np.all(dr.hour==0), True)

dr = date_range(start, end, freq='D', tz='US/Eastern')
self.assertEqual(dr[0], start)
self.assertEqual(dr[-1], end)
self.assertEqual(np.all(dr.hour==0), True)

dr = date_range(start.replace(tzinfo=None), end.replace(tzinfo=None), freq='D', tz='US/Eastern')
self.assertEqual(dr[0], start)
self.assertEqual(dr[-1], end)
self.assertEqual(np.all(dr.hour==0), True)

def test_range_tz_dateutil(self):
# GH 2906
Expand Down