Skip to content

REF: use _wrap_joined_index in PeriodIndex.join #33736

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 1 commit into from
Apr 23, 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
27 changes: 17 additions & 10 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Base and utility classes for tseries type pandas objects.
"""
from datetime import datetime
from typing import Any, List, Optional, Union
from typing import Any, List, Optional, Union, cast

import numpy as np

Expand Down Expand Up @@ -583,6 +583,22 @@ def delete(self, loc):
arr = type(self._data)._simple_new(new_i8s, dtype=self.dtype, freq=freq)
return type(self)._simple_new(arr, name=self.name)

# --------------------------------------------------------------------
# Join/Set Methods

def _wrap_joined_index(self, joined: np.ndarray, other):
assert other.dtype == self.dtype, (other.dtype, self.dtype)
name = get_op_result_name(self, other)

if is_period_dtype(self.dtype):
freq = self.freq
else:
self = cast(DatetimeTimedeltaMixin, self)
freq = self.freq if self._can_fast_union(other) else None
Comment on lines +593 to +597
Copy link
Member

Choose a reason for hiding this comment

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

to avoid this pattern in the base class, could _can_fast_union be overridden in PeriodIndex to always return True.

Copy link
Member

Choose a reason for hiding this comment

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

on second thoughts perhaps put in DatetimeIndexOpsMixin and then it's overridden in DatetimeTimedeltaMixin and the cast is not necessary.

Copy link
Member Author

Choose a reason for hiding this comment

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

i considered this, but im planning to implement an actual _can_fast_union for PeriodIndex before long

new_data = type(self._data)._simple_new(joined, dtype=self.dtype, freq=freq)

return type(self)._simple_new(new_data, name=name)


class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin, Int64Index):
"""
Expand Down Expand Up @@ -878,15 +894,6 @@ def _is_convertible_to_index_for_join(cls, other: Index) -> bool:
return True
return False

def _wrap_joined_index(self, joined: np.ndarray, other):
assert other.dtype == self.dtype, (other.dtype, self.dtype)
name = get_op_result_name(self, other)

freq = self.freq if self._can_fast_union(other) else None
new_data = type(self._data)._simple_new(joined, dtype=self.dtype, freq=freq)

return type(self)._simple_new(new_data, name=name)

# --------------------------------------------------------------------
# List-Like Methods

Expand Down
14 changes: 2 additions & 12 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ def join(self, other, how="left", level=None, return_indexers=False, sort=False)
other, how=how, level=level, return_indexers=return_indexers, sort=sort
)

# _assert_can_do_setop ensures we have matching dtype
result = Int64Index.join(
self,
other,
Expand All @@ -636,11 +637,7 @@ def join(self, other, how="left", level=None, return_indexers=False, sort=False)
return_indexers=return_indexers,
sort=sort,
)

if return_indexers:
result, lidx, ridx = result
return self._apply_meta(result), lidx, ridx
return self._apply_meta(result)
return result

# ------------------------------------------------------------------------
# Set Operation Methods
Expand Down Expand Up @@ -719,13 +716,6 @@ def _union(self, other, sort):

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

def _apply_meta(self, rawarr) -> "PeriodIndex":
if not isinstance(rawarr, PeriodIndex):
if not isinstance(rawarr, PeriodArray):
rawarr = PeriodArray(rawarr, freq=self.freq)
rawarr = PeriodIndex._simple_new(rawarr, name=self.name)
return rawarr

def memory_usage(self, deep=False):
result = super().memory_usage(deep=deep)
if hasattr(self, "_cache") and "_int64index" in self._cache:
Expand Down