From 086074c398d36fcb84e5290b33aa945f7bb377f1 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Thu, 9 Jan 2020 20:12:21 -0800 Subject: [PATCH 1/5] CLN: misc cleanups --- pandas/_libs/index.pyx | 2 -- pandas/core/arrays/categorical.py | 2 +- pandas/core/generic.py | 2 +- pandas/core/indexes/category.py | 2 +- pandas/core/indexes/datetimelike.py | 18 +++++++----------- pandas/core/indexes/range.py | 2 +- pandas/core/internals/managers.py | 2 +- 7 files changed, 12 insertions(+), 18 deletions(-) diff --git a/pandas/_libs/index.pyx b/pandas/_libs/index.pyx index ac8172146d351..8b08541d130b8 100644 --- a/pandas/_libs/index.pyx +++ b/pandas/_libs/index.pyx @@ -85,7 +85,6 @@ cdef class IndexEngine: """ cdef: object loc - void* data_ptr loc = self.get_loc(key) if isinstance(loc, slice) or util.is_array(loc): @@ -101,7 +100,6 @@ cdef class IndexEngine: """ cdef: object loc - void* data_ptr loc = self.get_loc(key) value = convert_scalar(arr, value) diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 2806635211459..24743bd090de2 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -2279,7 +2279,7 @@ def _from_factorized(cls, uniques, original): original.categories.take(uniques), dtype=original.dtype ) - def equals(self, other): + def equals(self, other) -> bool: """ Returns True if categorical arrays are equal. diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 0116207675889..864662e80598d 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1347,7 +1347,7 @@ def _indexed_same(self, other) -> bool: self._get_axis(a).equals(other._get_axis(a)) for a in self._AXIS_ORDERS ) - def equals(self, other): + def equals(self, other) -> bool_t: """ Test whether two objects contain the same elements. diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index a247a986fcb55..61aef76f2141f 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -310,7 +310,7 @@ def _is_dtype_compat(self, other) -> bool: return other - def equals(self, other): + def equals(self, other) -> bool: """ Determine if two CategoricalIndex objects contain the same elements. diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index c4dac9d1c4a11..58e16c4f90879 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -153,16 +153,16 @@ def equals(self, other) -> bool: return np.array_equal(self.asi8, other.asi8) @Appender(_index_shared_docs["contains"] % _index_doc_kwargs) - def __contains__(self, key): + def __contains__(self, key) -> bool: try: res = self.get_loc(key) - return ( - is_scalar(res) - or isinstance(res, slice) - or (is_list_like(res) and len(res)) - ) except (KeyError, TypeError, ValueError): return False + return ( + is_scalar(res) + or isinstance(res, slice) + or (is_list_like(res) and len(res)) + ) # Try to run function on index first, and then on elements of index # Especially important for group-by functionality @@ -875,11 +875,7 @@ def _is_convertible_to_index_for_join(cls, other: Index) -> bool: def _wrap_joined_index(self, joined, other): name = get_op_result_name(self, other) - if ( - isinstance(other, type(self)) - and self.freq == other.freq - and self._can_fast_union(other) - ): + if self._can_fast_union(other): joined = self._shallow_copy(joined) joined.name = name return joined diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index b4cc71a25792f..6fc548aef2e1e 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -441,7 +441,7 @@ def argsort(self, *args, **kwargs): else: return np.arange(len(self) - 1, -1, -1) - def equals(self, other): + def equals(self, other) -> bool: """ Determines if two Index objects contain the same elements. """ diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 066689b3e374e..12fd96b28b39a 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -1394,7 +1394,7 @@ def take(self, indexer, axis=1, verify=True, convert=True): new_axis=new_labels, indexer=indexer, axis=axis, allow_dups=True ) - def equals(self, other): + def equals(self, other) -> bool: self_axes, other_axes = self.axes, other.axes if len(self_axes) != len(other_axes): return False From be7b3b7918a3348f0ffc53e549864c4b51654511 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 10 Jan 2020 08:17:33 -0800 Subject: [PATCH 2/5] black/mypy fixup --- pandas/core/indexes/category.py | 2 +- pandas/core/indexes/datetimelike.py | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 61aef76f2141f..a247a986fcb55 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -310,7 +310,7 @@ def _is_dtype_compat(self, other) -> bool: return other - def equals(self, other) -> bool: + def equals(self, other): """ Determine if two CategoricalIndex objects contain the same elements. diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 58e16c4f90879..dc27f25c9a0f8 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -127,7 +127,7 @@ def __array_wrap__(self, result, context=None): # ------------------------------------------------------------------------ - def equals(self, other) -> bool: + def equals(self, other): """ Determines if two Index objects contain the same elements. """ @@ -159,9 +159,7 @@ def __contains__(self, key) -> bool: except (KeyError, TypeError, ValueError): return False return ( - is_scalar(res) - or isinstance(res, slice) - or (is_list_like(res) and len(res)) + is_scalar(res) or isinstance(res, slice) or (is_list_like(res) and len(res)) ) # Try to run function on index first, and then on elements of index From 57987d05ba717140102ea60e0c105aab014a8804 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 10 Jan 2020 08:49:51 -0800 Subject: [PATCH 3/5] mypy troubleshoot --- pandas/core/indexes/datetimelike.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index dc27f25c9a0f8..73098606f3ee0 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -127,7 +127,7 @@ def __array_wrap__(self, result, context=None): # ------------------------------------------------------------------------ - def equals(self, other): + def equals(self, other) -> bool: """ Determines if two Index objects contain the same elements. """ @@ -150,7 +150,8 @@ def equals(self, other): # have different timezone return False - return np.array_equal(self.asi8, other.asi8) + # explicit bool cast is needed for mypy in GH#30877 + return bool(np.array_equal(self.asi8, other.asi8)) @Appender(_index_shared_docs["contains"] % _index_doc_kwargs) def __contains__(self, key) -> bool: From 0dc80ea663fa62256d7b3ed34990f67742b3202e Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 10 Jan 2020 09:21:15 -0800 Subject: [PATCH 4/5] troubleshoot mypy --- pandas/core/indexes/datetimelike.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 73098606f3ee0..f1a49942c55c1 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -150,8 +150,7 @@ def equals(self, other) -> bool: # have different timezone return False - # explicit bool cast is needed for mypy in GH#30877 - return bool(np.array_equal(self.asi8, other.asi8)) + return np.array_equal(self.asi8, other.asi8) @Appender(_index_shared_docs["contains"] % _index_doc_kwargs) def __contains__(self, key) -> bool: @@ -159,7 +158,7 @@ def __contains__(self, key) -> bool: res = self.get_loc(key) except (KeyError, TypeError, ValueError): return False - return ( + return bool( is_scalar(res) or isinstance(res, slice) or (is_list_like(res) and len(res)) ) From 7f7531d51063ad60aa0cc7e8342ed599a996380a Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Mon, 13 Jan 2020 10:26:30 -0800 Subject: [PATCH 5/5] revert annotations --- pandas/core/arrays/categorical.py | 2 +- pandas/core/generic.py | 2 +- pandas/core/indexes/datetimelike.py | 2 +- pandas/core/indexes/range.py | 2 +- pandas/core/internals/managers.py | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 24743bd090de2..2806635211459 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -2279,7 +2279,7 @@ def _from_factorized(cls, uniques, original): original.categories.take(uniques), dtype=original.dtype ) - def equals(self, other) -> bool: + def equals(self, other): """ Returns True if categorical arrays are equal. diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 19ff5d041899d..04ce424edbee4 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1347,7 +1347,7 @@ def _indexed_same(self, other) -> bool: self._get_axis(a).equals(other._get_axis(a)) for a in self._AXIS_ORDERS ) - def equals(self, other) -> bool_t: + def equals(self, other): """ Test whether two objects contain the same elements. diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index eb9505d6fefa8..bf1272b223f70 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -153,7 +153,7 @@ def equals(self, other) -> bool: return np.array_equal(self.asi8, other.asi8) @Appender(_index_shared_docs["contains"] % _index_doc_kwargs) - def __contains__(self, key) -> bool: + def __contains__(self, key): try: res = self.get_loc(key) except (KeyError, TypeError, ValueError): diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index 6fc548aef2e1e..b4cc71a25792f 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -441,7 +441,7 @@ def argsort(self, *args, **kwargs): else: return np.arange(len(self) - 1, -1, -1) - def equals(self, other) -> bool: + def equals(self, other): """ Determines if two Index objects contain the same elements. """ diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 12fd96b28b39a..066689b3e374e 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -1394,7 +1394,7 @@ def take(self, indexer, axis=1, verify=True, convert=True): new_axis=new_labels, indexer=indexer, axis=axis, allow_dups=True ) - def equals(self, other) -> bool: + def equals(self, other): self_axes, other_axes = self.axes, other.axes if len(self_axes) != len(other_axes): return False