Skip to content

Implement PeriodIndex.difference without object-dtype cast #30697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ def _get_ilevel_values(index, level):
# accept level number only
unique = index.levels[level]
level_codes = index.codes[level]
filled = take_1d(unique.values, level_codes, fill_value=unique._na_value)
filled = take_1d(unique._values, level_codes, fill_value=unique._na_value)
values = unique._shallow_copy(filled, name=index.names[level])
return values

Expand Down
38 changes: 31 additions & 7 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
is_float_dtype,
is_integer,
is_integer_dtype,
is_object_dtype,
pandas_dtype,
)

Expand Down Expand Up @@ -588,13 +589,13 @@ def get_indexer_non_unique(self, target):
return ensure_platform_int(indexer), missing

def _get_unique_index(self, dropna=False):
"""
wrap Index._get_unique_index to handle NaT
"""
res = super()._get_unique_index(dropna=dropna)
if dropna:
res = res.dropna()
return res
if self.is_unique and not dropna:
return self

result = self._data.unique()
if dropna and self.hasnans:
result = result[~result.isna()]
return self._shallow_copy(result)

def get_loc(self, key, method=None, tolerance=None):
"""
Expand Down Expand Up @@ -809,6 +810,29 @@ def intersection(self, other, sort=False):
result = self._shallow_copy(np.asarray(i8result, dtype=np.int64), name=res_name)
return result

def difference(self, other, sort=None):
self._validate_sort_keyword(sort)
self._assert_can_do_setop(other)
res_name = get_op_result_name(self, other)
other = ensure_index(other)

if self.equals(other):
# pass an empty PeriodArray with the appropriate dtype
return self._shallow_copy(self._data[:0])

if is_object_dtype(other):
return self.astype(object).difference(other).astype(self.dtype)

elif not is_dtype_equal(self.dtype, other.dtype):
return self

i8self = Int64Index._simple_new(self.asi8)
i8other = Int64Index._simple_new(other.asi8)
i8result = i8self.difference(i8other, sort=sort)

result = self._shallow_copy(np.asarray(i8result, dtype=np.int64), name=res_name)
return result

# ------------------------------------------------------------------------

def _apply_meta(self, rawarr):
Expand Down