BUG: date_range issue with sub-second granularity
State: closed

Improves (but doesn't completely resolve) #24110, to avoid rounding
issues with sub-second granularity timestamps when creating a
date range.

- [x] closes #24110
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry

Labels:
name: Datetime
description: Datetime data dtype

Comments:
Hello @gabrielreid! Thanks for submitting the PR.

 - There are no PEP8 issues in the file [`pandas/core/arrays/datetimes.py`](https://github.com/gabrielreid/pandas/blob/d5d936fa007c582440c7d91631a038fa9cc90766/pandas/core/arrays/datetimes.py) !

 - There are no PEP8 issues in the file [`pandas/tests/indexes/datetimes/test_date_range.py`](https://github.com/gabrielreid/pandas/blob/d5d936fa007c582440c7d91631a038fa9cc90766/pandas/tests/indexes/datetimes/test_date_range.py) !


# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24129?src=pr&el=h1) Report
> Merging [#24129](https://codecov.io/gh/pandas-dev/pandas/pull/24129?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/b78aa8d8506ac119124619a9e03ff1482262e0cc?src=pr&el=desc) will **decrease** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/24129/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/24129?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #24129      +/-   ##
==========================================
- Coverage    92.2%    92.2%   -0.01%     
==========================================
  Files         162      162              
  Lines       51701    51701              
==========================================
- Hits        47672    47671       -1     
- Misses       4029     4030       +1
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.6% <100%> (ø)` | :arrow_up: |
| #single | `43.02% <0%> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/24129?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/arrays/datetimes.py](https://codecov.io/gh/pandas-dev/pandas/pull/24129/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYXJyYXlzL2RhdGV0aW1lcy5weQ==) | `98.56% <100%> (ø)` | :arrow_up: |
| [pandas/io/json/json.py](https://codecov.io/gh/pandas-dev/pandas/pull/24129/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2pzb24vanNvbi5weQ==) | `92.61% <0%> (-0.48%)` | :arrow_down: |
| [pandas/util/testing.py](https://codecov.io/gh/pandas-dev/pandas/pull/24129/diff?src=pr&el=tree#diff-cGFuZGFzL3V0aWwvdGVzdGluZy5weQ==) | `87.51% <0%> (+0.09%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24129?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24129?src=pr&el=footer). Last update [b78aa8d...d5d936f](https://codecov.io/gh/pandas-dev/pandas/pull/24129?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24129?src=pr&el=h1) Report
> Merging [#24129](https://codecov.io/gh/pandas-dev/pandas/pull/24129?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/0ac1197c0597d2d55b1ebf0a245c31bc1fa38e19?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/24129/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/24129?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master   #24129   +/-   ##
=======================================
  Coverage   92.21%   92.21%           
=======================================
  Files         162      162           
  Lines       51721    51721           
=======================================
  Hits        47692    47692           
  Misses       4029     4029
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.61% <100%> (ø)` | :arrow_up: |
| #single | `43.03% <0%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/24129?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/arrays/datetimes.py](https://codecov.io/gh/pandas-dev/pandas/pull/24129/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYXJyYXlzL2RhdGV0aW1lcy5weQ==) | `98.56% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24129?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24129?src=pr&el=footer). Last update [0ac1197...3918053](https://codecov.io/gh/pandas-dev/pandas/pull/24129?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

Updated this PR to include the whatsnew entry.

I also tried to write a test that demonstrates a failing scenario, but it appears that it's not possible to create Timestamp objects that are far enough in the future with sub-second granularity to trigger this issue, which means that this fix seems to cover all scenarios. I've updated the commit message to reflect this as well.
lgtm. @mroeschke over to you.
Updated the whatsnew entry as requested, but specified `millisecond resolution or higher`, as the millisecond resolution is sufficient to trigger this issue (and it's what was used to report the issue).

Also updated the test as requested to be parametrized with all three cases as reported in the original issue.
Thanks @gabrielreid! Great pull request.