Skip to content

Commit

Permalink
BUG: date_range issue with sub-second granularity
Browse files Browse the repository at this point in the history
Improves (but doesn't completely resolve) pandas-dev#24110, to avoid rounding
issues with sub-second granularity timestamps when creating a
date range.
  • Loading branch information
Gabriel Reid committed Dec 6, 2018
1 parent b78aa8d commit 62fb0f9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,9 @@ def _generate_range(cls, start, end, periods, freq, tz=None,
end = end.tz_localize(tz).asm8
else:
# Create a linearly spaced date_range in local time
arr = np.linspace(start.value, end.value, periods)
# Nanosecond-granularity timestamps aren't always correctly representable with doubles,
# so we limit the range that we pass to np.linspace as much as possible
arr = np.linspace(0, end.value - start.value, periods).astype('int64') + start.value
index = cls._simple_new(
arr.astype('M8[ns]', copy=False), freq=None, tz=tz
)
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/datetimes/test_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,3 +769,11 @@ def test_all_custom_freq(self, freq):
msg = 'invalid custom frequency string: {freq}'
with pytest.raises(ValueError, match=msg.format(freq=bad_freq)):
bdate_range(START, END, freq=bad_freq)

def test_range_with_millisecond_granularity(self):
# https://github.com/pandas-dev/pandas/issues/24110
start = '2018-01-01T00:00:00.010Z'
end = '2018-01-03T00:00:00.010Z'
result = pd.date_range(start=start, end=end, periods=2, closed='left')
expected = DatetimeIndex([start])
tm.assert_index_equal(result, expected)

0 comments on commit 62fb0f9

Please sign in to comment.