Skip to content

Commit c9bfb4f

Browse files
mroeschketp
authored and
tp
committed
BUG: date_range linspace behavior respects tz (pandas-dev#20988)
1 parent 29f7067 commit c9bfb4f

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

doc/source/whatsnew/v0.23.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ Other Enhancements
525525
library. (:issue:`20564`)
526526
- 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`)
527527
- :func:`to_hdf` and :func:`read_hdf` now accept an ``errors`` keyword argument to control encoding error handling (:issue:`20835`)
528-
- :func:`date_range` now returns a linearly spaced ``DatetimeIndex`` if ``start``, ``stop``, and ``periods`` are specified, but ``freq`` is not. (:issue:`20808`)
528+
- :func:`date_range` now returns a linearly spaced ``DatetimeIndex`` if ``start``, ``stop``, and ``periods`` are specified, but ``freq`` is not. (:issue:`20808`, :issue:`20983`)
529529

530530
.. _whatsnew_0230.api_breaking:
531531

pandas/core/indexes/datetimes.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,13 @@ def _generate(cls, start, end, periods, name, freq,
587587
if end is not None:
588588
end = end.tz_localize(tz).asm8
589589
else:
590+
# Create a linearly spaced date_range in local time
591+
start = start.tz_localize(tz)
592+
end = end.tz_localize(tz)
590593
index = tools.to_datetime(np.linspace(start.value,
591-
end.value, periods))
592-
if tz is not None:
593-
index = index.tz_localize('UTC').tz_convert(tz)
594+
end.value, periods),
595+
utc=True)
596+
index = index.tz_convert(tz)
594597

595598
if not left_closed and len(index) and index[0] == start:
596599
index = index[1:]

pandas/tests/indexes/datetimes/test_date_range.py

+30-11
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,39 @@ def test_date_range_ambiguous_arguments(self):
164164

165165
def test_date_range_convenience_periods(self):
166166
# GH 20808
167-
rng = date_range('2018-04-24', '2018-04-27', periods=3)
168-
exp = DatetimeIndex(['2018-04-24 00:00:00', '2018-04-25 12:00:00',
169-
'2018-04-27 00:00:00'], freq=None)
167+
result = date_range('2018-04-24', '2018-04-27', periods=3)
168+
expected = DatetimeIndex(['2018-04-24 00:00:00',
169+
'2018-04-25 12:00:00',
170+
'2018-04-27 00:00:00'], freq=None)
170171

171-
tm.assert_index_equal(rng, exp)
172+
tm.assert_index_equal(result, expected)
172173

173174
# Test if spacing remains linear if tz changes to dst in range
174-
rng = date_range('2018-04-01 01:00:00', '2018-04-01 04:00:00',
175-
tz='Australia/Sydney', periods=3)
176-
exp = DatetimeIndex(['2018-04-01 01:00:00+11:00',
177-
'2018-04-01 02:00:00+11:00',
178-
'2018-04-01 02:00:00+10:00',
179-
'2018-04-01 03:00:00+10:00',
180-
'2018-04-01 04:00:00+10:00'], freq=None)
175+
result = date_range('2018-04-01 01:00:00',
176+
'2018-04-01 04:00:00',
177+
tz='Australia/Sydney',
178+
periods=3)
179+
expected = DatetimeIndex([Timestamp('2018-04-01 01:00:00+1100',
180+
tz='Australia/Sydney'),
181+
Timestamp('2018-04-01 02:00:00+1000',
182+
tz='Australia/Sydney'),
183+
Timestamp('2018-04-01 04:00:00+1000',
184+
tz='Australia/Sydney')])
185+
tm.assert_index_equal(result, expected)
186+
187+
@pytest.mark.parametrize('start,end,result_tz', [
188+
['20180101', '20180103', 'US/Eastern'],
189+
[datetime(2018, 1, 1), datetime(2018, 1, 3), 'US/Eastern'],
190+
[Timestamp('20180101'), Timestamp('20180103'), 'US/Eastern'],
191+
[Timestamp('20180101', tz='US/Eastern'),
192+
Timestamp('20180103', tz='US/Eastern'), 'US/Eastern'],
193+
[Timestamp('20180101', tz='US/Eastern'),
194+
Timestamp('20180103', tz='US/Eastern'), None]])
195+
def test_date_range_linspacing_tz(self, start, end, result_tz):
196+
# GH 20983
197+
result = date_range(start, end, periods=3, tz=result_tz)
198+
expected = date_range('20180101', periods=3, freq='D', tz='US/Eastern')
199+
tm.assert_index_equal(result, expected)
181200

182201
def test_date_range_businesshour(self):
183202
idx = DatetimeIndex(['2014-07-04 09:00', '2014-07-04 10:00',

0 commit comments

Comments
 (0)