ENH: Add in sort keyword to DatetimeIndex.union
State: closed

- [x] closes #24994, xref #24471
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry

As discussed in #25063 for ``Index.union``, I have made the default ``sort=None`` which should sort except in the three cases outlined in the doc-string. When passing ``sort=False`` it should never sort.

There is a fastpath in the ``DatetimeIndex.union`` that at present automatically sorts the two ``DatetimeIndexes`` if they are both monotonic by simply putting the two indexes in the correct order and then removing any overlaps. I have tried to adjust this fastpath to control this sorting behaviour.

I have tried to adjust the tests to properly test the sorting behaviour but I think the set operations tests need more work in general.

@jorisvandenbossche and @TomAugspurger it would be great if you could have a look at this too.

Labels:
name: Reshaping
description: Concat, Merge/Join, Stack/Unstack, Explode

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

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

```diff
@@            Coverage Diff             @@
##           master   #25110      +/-   ##
==========================================
+ Coverage   92.37%   92.37%   +<.01%     
==========================================
  Files         166      166              
  Lines       52415    52423       +8     
==========================================
+ Hits        48418    48426       +8     
  Misses       3997     3997
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.79% <100%> (ø)` | :arrow_up: |
| #single | `42.86% <30.76%> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/25110?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/indexes/datetimes.py](https://codecov.io/gh/pandas-dev/pandas/pull/25110/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9kYXRldGltZXMucHk=) | `96.32% <100%> (+0.04%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/25110?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/25110?src=pr&el=footer). Last update [f75a220...31ef8f7](https://codecov.io/gh/pandas-dev/pandas/pull/25110?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/25110?src=pr&el=h1) Report
> Merging [#25110](https://codecov.io/gh/pandas-dev/pandas/pull/25110?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/5557e3627df67211f5b6406d07995dccb02196f9?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff             @@
##           master   #25110      +/-   ##
==========================================
+ Coverage   91.73%   91.73%   +<.01%     
==========================================
  Files         173      173              
  Lines       52848    52856       +8     
==========================================
+ Hits        48482    48490       +8     
  Misses       4366     4366
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.3% <100%> (ø)` | :arrow_up: |
| #single | `41.69% <15.38%> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/25110?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/indexes/datetimes.py](https://codecov.io/gh/pandas-dev/pandas/pull/25110/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9kYXRldGltZXMucHk=) | `96.34% <100%> (+0.04%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/25110?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/25110?src=pr&el=footer). Last update [5557e36...4df9345](https://codecov.io/gh/pandas-dev/pandas/pull/25110?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

lgtm. @jorisvandenbossche if you have a quick look.
@jreback it's green now. Thanks!
@reidy-p thanks a lot!
@reidy-p also, feel free to update https://github.com/pandas-dev/pandas/issues/24471 with the current status (do now all index classes have a sort keyword for intersection?)