Skip to content

Commit cd7c92f

Browse files
jbrockmendelrhshadrach
authored andcommitted
REF: use _wrap_joined_index in PeriodIndex.join (pandas-dev#33736)
1 parent cded129 commit cd7c92f

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

pandas/core/indexes/datetimelike.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Base and utility classes for tseries type pandas objects.
33
"""
44
from datetime import datetime
5-
from typing import Any, List, Optional, Union
5+
from typing import Any, List, Optional, Union, cast
66

77
import numpy as np
88

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

586+
# --------------------------------------------------------------------
587+
# Join/Set Methods
588+
589+
def _wrap_joined_index(self, joined: np.ndarray, other):
590+
assert other.dtype == self.dtype, (other.dtype, self.dtype)
591+
name = get_op_result_name(self, other)
592+
593+
if is_period_dtype(self.dtype):
594+
freq = self.freq
595+
else:
596+
self = cast(DatetimeTimedeltaMixin, self)
597+
freq = self.freq if self._can_fast_union(other) else None
598+
new_data = type(self._data)._simple_new(joined, dtype=self.dtype, freq=freq)
599+
600+
return type(self)._simple_new(new_data, name=name)
601+
586602

587603
class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin, Int64Index):
588604
"""
@@ -878,15 +894,6 @@ def _is_convertible_to_index_for_join(cls, other: Index) -> bool:
878894
return True
879895
return False
880896

881-
def _wrap_joined_index(self, joined: np.ndarray, other):
882-
assert other.dtype == self.dtype, (other.dtype, self.dtype)
883-
name = get_op_result_name(self, other)
884-
885-
freq = self.freq if self._can_fast_union(other) else None
886-
new_data = type(self._data)._simple_new(joined, dtype=self.dtype, freq=freq)
887-
888-
return type(self)._simple_new(new_data, name=name)
889-
890897
# --------------------------------------------------------------------
891898
# List-Like Methods
892899

pandas/core/indexes/period.py

+2-12
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ def join(self, other, how="left", level=None, return_indexers=False, sort=False)
628628
other, how=how, level=level, return_indexers=return_indexers, sort=sort
629629
)
630630

631+
# _assert_can_do_setop ensures we have matching dtype
631632
result = Int64Index.join(
632633
self,
633634
other,
@@ -636,11 +637,7 @@ def join(self, other, how="left", level=None, return_indexers=False, sort=False)
636637
return_indexers=return_indexers,
637638
sort=sort,
638639
)
639-
640-
if return_indexers:
641-
result, lidx, ridx = result
642-
return self._apply_meta(result), lidx, ridx
643-
return self._apply_meta(result)
640+
return result
644641

645642
# ------------------------------------------------------------------------
646643
# Set Operation Methods
@@ -719,13 +716,6 @@ def _union(self, other, sort):
719716

720717
# ------------------------------------------------------------------------
721718

722-
def _apply_meta(self, rawarr) -> "PeriodIndex":
723-
if not isinstance(rawarr, PeriodIndex):
724-
if not isinstance(rawarr, PeriodArray):
725-
rawarr = PeriodArray(rawarr, freq=self.freq)
726-
rawarr = PeriodIndex._simple_new(rawarr, name=self.name)
727-
return rawarr
728-
729719
def memory_usage(self, deep=False):
730720
result = super().memory_usage(deep=deep)
731721
if hasattr(self, "_cache") and "_int64index" in self._cache:

0 commit comments

Comments
 (0)