Closed Dr-Irv closed 6 years ago
I'm willing to work on this, but can we have a discussion on the implementation? The suggested solution in the discussion in #19849 is to use self._shallow_copy([])
, but that method doesn't work right for empty indexes, so I think it is easier to just have a method that creates an empty index, but preserves the other properties of the index (e.g., categories for CategoricalIndex
, range step for RangeIndex
, freq for PeriodIndex
, etc.)
Alternatively, I can make self._shallow_copy([])
work for the various Index
subclasses with an empty list argument.
@Dr-Irv : That seems like a good first attempt to patch this, though other options are welcome of course.
@gfyoung By "That seems", do you mean having a method to create an empty index, or fixing _shallow_copy([])
Oh, sorry! I was referring to fixing _shallow_copy([])
.
Code Sample, a copy-pastable example if possible
Problem description
The result of taking the difference of an
Index
for variousIndex
subclasses and theIndex
produces a resultingIndex
that does not preserve the type of the subclass.From a set algebra point of view, for a set S,
S.difference(S)
should equalS.intersection(nullset)
.The output from the above is:
There is some discussion in the pull request #19849, where I discovered this bug, but at request of @jreback, I have split this into a separate issue.
Expected Output
Note that for
RangeIndex
, the result of theintersection
operation is also incorrect.Output of
pd.show_versions()