Skip to content

REF: share _union between DTI/TDI #30714

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
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
19 changes: 19 additions & 0 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,25 @@ def _fast_union(self, other, sort=None):
else:
return left

def _union(self, other, sort):
Copy link
Contributor

Choose a reason for hiding this comment

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

other can a Union[DTI, TDI]

Copy link
Member Author

Choose a reason for hiding this comment

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

@simonjayhawkins what's the preferred way of typing as type(self)?

Copy link
Member

Choose a reason for hiding this comment

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

def _union(self: _T, other: Type[_T], sort):

Copy link
Member Author

Choose a reason for hiding this comment

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

where does _T come from? I dont see this pattern anywhere else in the code

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 vote for merging and adding annotations in follow-up

Copy link
Contributor

Choose a reason for hiding this comment

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

followup ok

if not len(other) or self.equals(other) or not len(self):
return super()._union(other, sort=sort)

# We are called by `union`, which is responsible for this validation
assert isinstance(other, type(self))

this, other = self._maybe_utc_convert(other)

if this._can_fast_union(other):
return this._fast_union(other, sort=sort)
else:
result = Index._union(this, other, sort=sort)
if isinstance(result, type(self)):
assert result._data.dtype == this.dtype
if result.freq is None:
result._set_freq("infer")
return result

# --------------------------------------------------------------------
# Join Methods
_join_precedence = 10
Expand Down
19 changes: 0 additions & 19 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,25 +379,6 @@ def _formatter_func(self):
# --------------------------------------------------------------------
# Set Operation Methods

def _union(self, other: "DatetimeIndex", sort):
if not len(other) or self.equals(other) or not len(self):
return super()._union(other, sort=sort)

# We are called by `union`, which is responsible for this validation
assert isinstance(other, DatetimeIndex)

this, other = self._maybe_utc_convert(other)

if this._can_fast_union(other):
return this._fast_union(other, sort=sort)
else:
result = Index._union(this, other, sort=sort)
if isinstance(result, DatetimeIndex):
assert result._data.dtype == this.dtype
if result.freq is None:
result._set_freq("infer")
return result

def union_many(self, others):
"""
A bit of a hack to accelerate unioning a collection of indexes.
Expand Down
18 changes: 0 additions & 18 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,24 +246,6 @@ def astype(self, dtype, copy=True):
return Index(result.astype("i8"), name=self.name)
return DatetimeIndexOpsMixin.astype(self, dtype, copy=copy)

def _union(self, other: "TimedeltaIndex", sort):
if len(other) == 0 or self.equals(other) or len(self) == 0:
return super()._union(other, sort=sort)

# We are called by `union`, which is responsible for this validation
assert isinstance(other, TimedeltaIndex)

this, other = self, other

if this._can_fast_union(other):
return this._fast_union(other, sort=sort)
else:
result = Index._union(this, other, sort=sort)
if isinstance(result, TimedeltaIndex):
if result.freq is None:
result._set_freq("infer")
return result

def _maybe_promote(self, other):
if other.inferred_type == "timedelta":
other = TimedeltaIndex(other)
Expand Down