Skip to content

REF: define DTA._infer_matches #38008

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 5 commits into from
Nov 24, 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
2 changes: 2 additions & 0 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class DatetimeLikeArrayMixin(OpsMixin, NDArrayBackedExtensionArray):
_generate_range
"""

# _infer_matches -> which infer_dtype strings are close enough to our own
_infer_matches: Tuple[str, ...]
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a 1-line comment to this one, as its not completely obvious (others if you think it would help)

Copy link
Member Author

Choose a reason for hiding this comment

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

updated + greenish

_is_recognized_dtype: Callable[[DtypeObj], bool]
_recognized_scalars: Tuple[Type, ...]
_data: np.ndarray
Expand Down
1 change: 1 addition & 0 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class DatetimeArray(dtl.TimelikeOps, dtl.DatelikeOps):
_scalar_type = Timestamp
_recognized_scalars = (datetime, np.datetime64)
_is_recognized_dtype = is_datetime64_any_dtype
_infer_matches = ("datetime", "datetime64", "date")

# define my properties & methods for delegation
_bool_ops = [
Expand Down
1 change: 1 addition & 0 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class PeriodArray(PeriodMixin, dtl.DatelikeOps):
_scalar_type = Period
_recognized_scalars = (Period,)
_is_recognized_dtype = is_period_dtype
_infer_matches = ("period",)

# Names others delegate to us
_other_ops: List[str] = []
Expand Down
1 change: 1 addition & 0 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class TimedeltaArray(dtl.TimelikeOps):
_scalar_type = Timedelta
_recognized_scalars = (timedelta, np.timedelta64, Tick)
_is_recognized_dtype = is_timedelta64_dtype
_infer_matches = ("timedelta", "timedelta64")

__array_priority__ = 1000
# define my properties & methods for delegation
Expand Down
13 changes: 4 additions & 9 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,8 @@ def equals(self, other: object) -> bool:
elif other.dtype.kind in ["f", "i", "u", "c"]:
return False
elif not isinstance(other, type(self)):
inferrable = [
"timedelta",
"timedelta64",
"datetime",
"datetime64",
"date",
"period",
]

should_try = False
inferrable = self._data._infer_matches
if other.dtype == object:
should_try = other.inferred_type in inferrable
elif is_categorical_dtype(other.dtype):
Expand Down Expand Up @@ -648,6 +640,9 @@ def _has_complex_internals(self) -> bool:
# used to avoid libreduction code paths, which raise or require conversion
return False

def is_type_compatible(self, kind: str) -> bool:
return kind in self._data._infer_matches

# --------------------------------------------------------------------
# Set Operation Methods

Expand Down
3 changes: 0 additions & 3 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,9 +814,6 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None):

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

def is_type_compatible(self, kind: str) -> bool:
return kind == self.inferred_type or kind == "datetime"

@property
def inferred_type(self) -> str:
# b/c datetime is represented as microseconds since the epoch, make
Expand Down
3 changes: 0 additions & 3 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,6 @@ def _maybe_cast_slice_bound(self, label, side: str, kind):

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

def is_type_compatible(self, kind: str) -> bool:
return kind == self.inferred_type or kind == "timedelta"

@property
def inferred_type(self) -> str:
return "timedelta64"
Expand Down