Skip to content

Fix PeriodIndex._shallow_copy allowing object-dtype #30713

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 2 commits into from
Jan 6, 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
1 change: 1 addition & 0 deletions pandas/core/indexes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def conv(i):
result = indexes[0]

if hasattr(result, "union_many"):
# DatetimeIndex
return result.union_many(indexes[1:])
else:
for other in indexes[1:]:
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1947,7 +1947,7 @@ def dropna(self, how="any"):
raise ValueError(f"invalid how option: {how}")

if self.hasnans:
return self._shallow_copy(self.values[~self._isnan])
return self._shallow_copy(self._values[~self._isnan])
return self._shallow_copy()

# --------------------------------------------------------------------
Expand Down Expand Up @@ -2568,11 +2568,11 @@ def symmetric_difference(self, other, result_name=None, sort=None):
left_indexer = np.setdiff1d(
np.arange(this.size), common_indexer, assume_unique=True
)
left_diff = this.values.take(left_indexer)
left_diff = this._values.take(left_indexer)

# {other} minus {this}
right_indexer = (indexer == -1).nonzero()[0]
right_diff = other.values.take(right_indexer)
right_diff = other._values.take(right_indexer)

the_diff = concat_compat([left_diff, right_diff])
if sort is None:
Expand Down
13 changes: 4 additions & 9 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,19 +272,14 @@ def _shallow_copy(self, values=None, **kwargs):
values = self._data

if isinstance(values, type(self)):
values = values._values
values = values._data

if not isinstance(values, PeriodArray):
if isinstance(values, np.ndarray) and is_integer_dtype(values.dtype):
if isinstance(values, np.ndarray) and values.dtype == "i8":
values = PeriodArray(values, freq=self.freq)
else:
# in particular, I would like to avoid period_array here.
# Some people seem to be calling use with unexpected types
# Index.difference -> ndarray[Period]
# DatetimelikeIndexOpsMixin.repeat -> ndarray[ordinal]
# I think that once all of Datetime* are EAs, we can simplify
# this quite a bit.
values = period_array(values, freq=self.freq)
# GH#30713 this should never be reached
raise TypeError(type(values), getattr(values, "dtype", None))
Copy link
Contributor

Choose a reason for hiding this comment

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

what triggers this?

Copy link
Member Author

Choose a reason for hiding this comment

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

After this PR, this is never reached. In master we get there with ndarray[object] via dropna and symmetric_difference. The PeriodIndex set ops PRs over the last couple days have been aimed at this

Copy link
Contributor

Choose a reason for hiding this comment

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

ok can u make a comment about this then

should this just be an assertion ?


# We don't allow changing `freq` in _shallow_copy.
validate_dtype_freq(self.dtype, kwargs.get("freq"))
Expand Down