BUG: names on union and intersection for Index were inconsistent (GH9943 GH9862)
State: closed

xref #9943
closes #9862 
- [x] tests added / passed
  - Added tests to check names on unions when Index has different values
  - Fixed test for ranges with unions
  - Added tests for all index types for corner cases for union, difference
  - Added tests so that `index.intersection(empty_index)==index.difference(index)`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry

Also fixes bug in `Index.difference` when computing `index.difference(index)`


Labels:
name: Bug
name: Indexing
description: Related to indexing on series/frames, not to indexes themselves
name: Reshaping
description: Concat, Merge/Join, Stack/Unstack, Explode

Comments:
@Dr-Irv @jreback : Are you sure we can close #9862 with this PR?  Seems to be the case that it can given some of the discussion that took place.  However, just want to double-check.
@gfyoung Re your question above about closing #9862, that was my sense based on the comments and my analysis of the open checkmarks at the top.

BTW, I'm working on those bugs identified in testing.  Seems like I didn't have 'numexpr' installed on my local machine so those errors weren't picked up when I ran pytest locally.

# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/19849?src=pr&el=h1) Report
> :exclamation: No coverage uploaded for pull request base (`master@6a5c34c`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference#section-missing-base-commit).
> The diff coverage is `96.15%`.

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

```diff
@@            Coverage Diff            @@
##             master   #19849   +/-   ##
=========================================
  Coverage          ?   92.24%           
=========================================
  Files             ?      161           
  Lines             ?    51193           
  Branches          ?        0           
=========================================
  Hits              ?    47225           
  Misses            ?     3968           
  Partials          ?        0
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.63% <96.15%> (?)` | |
| #single | `42.29% <59.61%> (?)` | |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/19849?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/indexes/category.py](https://codecov.io/gh/pandas-dev/pandas/pull/19849/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9jYXRlZ29yeS5weQ==) | `97.87% <100%> (ø)` | |
| [pandas/core/indexes/period.py](https://codecov.io/gh/pandas-dev/pandas/pull/19849/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9wZXJpb2QucHk=) | `92.17% <100%> (ø)` | |
| [pandas/core/indexes/interval.py](https://codecov.io/gh/pandas-dev/pandas/pull/19849/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9pbnRlcnZhbC5weQ==) | `94.66% <100%> (ø)` | |
| [pandas/core/indexes/numeric.py](https://codecov.io/gh/pandas-dev/pandas/pull/19849/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9udW1lcmljLnB5) | `97.32% <100%> (ø)` | |
| [pandas/core/indexes/base.py](https://codecov.io/gh/pandas-dev/pandas/pull/19849/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9iYXNlLnB5) | `96.45% <100%> (ø)` | |
| [pandas/core/indexes/range.py](https://codecov.io/gh/pandas-dev/pandas/pull/19849/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9yYW5nZS5weQ==) | `97.32% <100%> (ø)` | |
| [pandas/core/indexes/timedeltas.py](https://codecov.io/gh/pandas-dev/pandas/pull/19849/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy90aW1lZGVsdGFzLnB5) | `89.88% <83.33%> (ø)` | |
| [pandas/core/indexes/datetimes.py](https://codecov.io/gh/pandas-dev/pandas/pull/19849/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9kYXRldGltZXMucHk=) | `96.13% <88.88%> (ø)` | |

------

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

@TomAugspurger With respect to your question about #10186, the issue comes down to whether we use `self._shallow_copy()` to return the result of set operations. When I tried using `self._shallow_copy()` in `Index._wrap_setop_result()`, and looked at what was happening, then `CategoricalIndex` was the problem.  So it was easier to leave the existing implementation of `Index._wrap_setop_result`.  If you were to handle #10186, then I could experiment more with changing the implementation of `Index._wrap_setop_result`.
@Dr-Irv as I said above. this needs to be greatly downscoped. You are changing way too many things and not using the current code conventions. So pls break this in to separate PR's.
@jreback wrote
> as I said above. this needs to be greatly downscoped. You are changing way too many things and not using the current code conventions. So pls break this in to separate PR's.

My latest commit (https://github.com/pandas-dev/pandas/pull/19849/commits/fb3a9db0e47bacc2a0e46c81d037cd2819645d75) removes the changes relative to the bug in `Index.difference`, and I created a separate Issue for that (#20040), so we can discuss there the implementation choices.
@jreback with respect to the helper function `Index._union_corner_case`, the function is called from the implementations of `union` in `DateTimeIndex`, `RangeIndex` and `TimeDeltaIndex`. I can remove it by just repeating the code in those implementations. If you want the repeated code, let me know.
@jreback This is all green now, and I think I have addressed all requested changes.
@jreback Checking with you on status of this one, as I think I've addressed all requested changes and it's all green.
@Dr-Irv if you want to rebase we can have a look
@jbrockmendel if any comments.
This looks like a nice cleanup, thanks @Dr-Irv.
@Dr-Irv this got a bit lost, can you rebase
Hello @Dr-Irv! Thanks for updating the PR.

 - There are no PEP8 issues in the file [`pandas/core/indexes/base.py`](https://github.com/Dr-Irv/pandas/blob/d1620f66cce778d86bdbed1de1eb2f14e795ac59/pandas/core/indexes/base.py) !

 - There are no PEP8 issues in the file [`pandas/core/indexes/category.py`](https://github.com/Dr-Irv/pandas/blob/d1620f66cce778d86bdbed1de1eb2f14e795ac59/pandas/core/indexes/category.py) !

 - There are no PEP8 issues in the file [`pandas/core/indexes/datetimes.py`](https://github.com/Dr-Irv/pandas/blob/d1620f66cce778d86bdbed1de1eb2f14e795ac59/pandas/core/indexes/datetimes.py) !

 - There are no PEP8 issues in the file [`pandas/core/indexes/interval.py`](https://github.com/Dr-Irv/pandas/blob/d1620f66cce778d86bdbed1de1eb2f14e795ac59/pandas/core/indexes/interval.py) !

 - There are no PEP8 issues in the file [`pandas/core/indexes/numeric.py`](https://github.com/Dr-Irv/pandas/blob/d1620f66cce778d86bdbed1de1eb2f14e795ac59/pandas/core/indexes/numeric.py) !

 - There are no PEP8 issues in the file [`pandas/core/indexes/period.py`](https://github.com/Dr-Irv/pandas/blob/d1620f66cce778d86bdbed1de1eb2f14e795ac59/pandas/core/indexes/period.py) !

 - There are no PEP8 issues in the file [`pandas/core/indexes/range.py`](https://github.com/Dr-Irv/pandas/blob/d1620f66cce778d86bdbed1de1eb2f14e795ac59/pandas/core/indexes/range.py) !

 - There are no PEP8 issues in the file [`pandas/core/indexes/timedeltas.py`](https://github.com/Dr-Irv/pandas/blob/d1620f66cce778d86bdbed1de1eb2f14e795ac59/pandas/core/indexes/timedeltas.py) !

 - There are no PEP8 issues in the file [`pandas/tests/indexes/common.py`](https://github.com/Dr-Irv/pandas/blob/d1620f66cce778d86bdbed1de1eb2f14e795ac59/pandas/tests/indexes/common.py) !

 - There are no PEP8 issues in the file [`pandas/tests/indexes/conftest.py`](https://github.com/Dr-Irv/pandas/blob/d1620f66cce778d86bdbed1de1eb2f14e795ac59/pandas/tests/indexes/conftest.py) !

 - There are no PEP8 issues in the file [`pandas/tests/indexes/test_base.py`](https://github.com/Dr-Irv/pandas/blob/d1620f66cce778d86bdbed1de1eb2f14e795ac59/pandas/tests/indexes/test_base.py) !

 - There are no PEP8 issues in the file [`pandas/tests/reshape/test_concat.py`](https://github.com/Dr-Irv/pandas/blob/d1620f66cce778d86bdbed1de1eb2f14e795ac59/pandas/tests/reshape/test_concat.py) !


@jreback This is ready.  The failure was due to a timeout on travis.  If you want me to do something about that, let me know.
> The failure was due to a timeout on travis.

@Dr-Irv : I think that's being handled in #23441 .
Merged master with #23441.
lgtm. @Dr-Irv ping on green.
@jreback all green
@Dr-Irv @jreback : Is this PR meant to also close #9943?  (or are you just "xref"-ing it) . I only ask because you mention the issue number in the `whatsnew` but are not closing it.
@gfyoung Back when I created the PR, I thought this closed #9943 because #9943 was split into #9862 and #9885 .  But then I see that @jreback edited the original description to xref #9943.  #9943 and #9862 cross reference each other, so I thought putting both refs in the whatsnew is what made sense.  But I'll do whatever you guys think is best.
Alright then.  I'll merge, as is, and let @jreback decide what to do with #9943.
Thanks @Dr-Irv !