Skip to content

REF: use _unbox_scalar to de-duplicate TDA/DTA/PA comparison methods #24576

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

Closed
jbrockmendel opened this issue Jan 3, 2019 · 0 comments
Closed
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. Refactor Internal refactoring of code

Comments

@jbrockmendel
Copy link
Member

DatetimeArray and TimedeltaArray comparison methods share a lot of code, might be possible to de-duplicate a lot of it by using the new unbox_scalar method:

From DTA:

        if isinstance(other, (datetime, np.datetime64, compat.string_types)):
            if isinstance(other, (datetime, np.datetime64)):
                # GH#18435 strings get a pass from tzawareness compat
                self._assert_tzawareness_compat(other)

            try:
                other = _to_m8(other, tz=self.tz)
            except ValueError:
                # string that cannot be parsed to Timestamp
                return ops.invalid_comparison(self, other, op)

            result = op(self.asi8, other.view('i8'))
            if isna(other):
                result.fill(nat_result)

From TDA:

        if _is_convertible_to_td(other) or other is NaT:
            try:
                other = _to_m8(other)
            except ValueError:
                # failed to parse as timedelta
                return ops.invalid_comparison(self, other, op)

            result = meth(self, other)
            if isna(other):
                result.fill(nat_result)

Note that _to_m8 means something different in these two files.

The _unbox_scalar methods might not be quite the right fit, but the idea behind them is really similar to _is_convertible_to_td and the try/except calls to to_m8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. Refactor Internal refactoring of code
Projects
None yet
Development

No branches or pull requests

2 participants