|
17 | 17 | is_float_dtype,
|
18 | 18 | is_integer,
|
19 | 19 | is_integer_dtype,
|
| 20 | + is_object_dtype, |
20 | 21 | pandas_dtype,
|
21 | 22 | )
|
22 | 23 |
|
@@ -588,13 +589,13 @@ def get_indexer_non_unique(self, target):
|
588 | 589 | return ensure_platform_int(indexer), missing
|
589 | 590 |
|
590 | 591 | def _get_unique_index(self, dropna=False):
|
591 |
| - """ |
592 |
| - wrap Index._get_unique_index to handle NaT |
593 |
| - """ |
594 |
| - res = super()._get_unique_index(dropna=dropna) |
595 |
| - if dropna: |
596 |
| - res = res.dropna() |
597 |
| - return res |
| 592 | + if self.is_unique and not dropna: |
| 593 | + return self |
| 594 | + |
| 595 | + result = self._data.unique() |
| 596 | + if dropna and self.hasnans: |
| 597 | + result = result[~result.isna()] |
| 598 | + return self._shallow_copy(result) |
598 | 599 |
|
599 | 600 | def get_loc(self, key, method=None, tolerance=None):
|
600 | 601 | """
|
@@ -809,6 +810,29 @@ def intersection(self, other, sort=False):
|
809 | 810 | result = self._shallow_copy(np.asarray(i8result, dtype=np.int64), name=res_name)
|
810 | 811 | return result
|
811 | 812 |
|
| 813 | + def difference(self, other, sort=None): |
| 814 | + self._validate_sort_keyword(sort) |
| 815 | + self._assert_can_do_setop(other) |
| 816 | + res_name = get_op_result_name(self, other) |
| 817 | + other = ensure_index(other) |
| 818 | + |
| 819 | + if self.equals(other): |
| 820 | + # pass an empty PeriodArray with the appropriate dtype |
| 821 | + return self._shallow_copy(self._data[:0]) |
| 822 | + |
| 823 | + if is_object_dtype(other): |
| 824 | + return self.astype(object).difference(other).astype(self.dtype) |
| 825 | + |
| 826 | + elif not is_dtype_equal(self.dtype, other.dtype): |
| 827 | + return self |
| 828 | + |
| 829 | + i8self = Int64Index._simple_new(self.asi8) |
| 830 | + i8other = Int64Index._simple_new(other.asi8) |
| 831 | + i8result = i8self.difference(i8other, sort=sort) |
| 832 | + |
| 833 | + result = self._shallow_copy(np.asarray(i8result, dtype=np.int64), name=res_name) |
| 834 | + return result |
| 835 | + |
812 | 836 | # ------------------------------------------------------------------------
|
813 | 837 |
|
814 | 838 | def _apply_meta(self, rawarr):
|
|
0 commit comments