Skip to content

BUG: date_range linspace behavior respects tz #20988

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 2 commits into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ Other Enhancements
library. (:issue:`20564`)
- Added new writer for exporting Stata dta files in version 117, ``StataWriter117``. This format supports exporting strings with lengths up to 2,000,000 characters (:issue:`16450`)
- :func:`to_hdf` and :func:`read_hdf` now accept an ``errors`` keyword argument to control encoding error handling (:issue:`20835`)
- :func:`date_range` now returns a linearly spaced ``DatetimeIndex`` if ``start``, ``stop``, and ``periods`` are specified, but ``freq`` is not. (:issue:`20808`)
- :func:`date_range` now returns a linearly spaced ``DatetimeIndex`` if ``start``, ``stop``, and ``periods`` are specified, but ``freq`` is not. (:issue:`20808`, :issue:`20983`)

.. _whatsnew_0230.api_breaking:

Expand Down
9 changes: 6 additions & 3 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,13 @@ def _generate(cls, start, end, periods, name, freq,
if end is not None:
end = end.tz_localize(tz).asm8
else:
# Create a linearly spaced date_range.
Copy link
Contributor

Choose a reason for hiding this comment

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

specifiy this is in local time

start = start.tz_localize(tz)
end = end.tz_localize(tz)
index = tools.to_datetime(np.linspace(start.value,
end.value, periods))
if tz is not None:
index = index.tz_localize('UTC').tz_convert(tz)
end.value, periods),
utc=True)
index = index.tz_convert(tz)

if not left_closed and len(index) and index[0] == start:
index = index[1:]
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/indexes/datetimes/test_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ def test_date_range_convenience_periods(self):
'2018-04-01 03:00:00+10:00',
'2018-04-01 04:00:00+10:00'], freq=None)

@pytest.mark.parametrize('start,end', [
['20180101', '20180103'],
[datetime(2018, 1, 1), datetime(2018, 1, 3)],
[Timestamp('20180101'), Timestamp('20180103')],
[Timestamp('20180101', tz='US/Eastern'),
Timestamp('20180103', tz='US/Eastern')]])
def test_date_range_linspacing_tz(self, start, end):
# GH 20983
result = date_range(start, end, periods=3, tz='US/Eastern')
Copy link
Member

Choose a reason for hiding this comment

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

Might be beneficial to test when start/end are tz aware but not specifying the tz param for date_range, to ensure that tz inference from start/end is okay.

expected = date_range('20180101', periods=3, freq='D', tz='US/Eastern')
tm.assert_index_equal(result, expected)

def test_date_range_businesshour(self):
idx = DatetimeIndex(['2014-07-04 09:00', '2014-07-04 10:00',
'2014-07-04 11:00',
Expand Down