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 1 commit
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
5 changes: 4 additions & 1 deletion pandas/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,10 @@ 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)
if is_extension_array_dtype(unique):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not as nice (though its only for testing), won't unique.take(...) work here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ill give it a shot

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out this unique.take(...) wont work here. the trouble is that we are calling _shallow_copy below (presumably for perf) and using unique.take ends up passing the wrong types to shallow_copy

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using unique.take ends up passing the wrong types to shallow_copy

then i'd rather not change this, take_1d just does the right thing, so why change it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like passing unique._values instead of unique.values fixes the problem cleanly

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok great

filled = unique.take(level_codes, fill_value=unique._na_value)
else:
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 @@ -594,13 +595,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 @@ -818,6 +819,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