From 13044f3b8f78ff98170dc859703c3fed3e797a64 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 8 Sep 2018 20:39:08 -0700 Subject: [PATCH 1/3] Avoid DeprecationWarnings in py37 --- pandas/io/formats/style.py | 8 ++++++-- pandas/util/testing.py | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 6501717f715cb..b175dd540a518 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1033,10 +1033,14 @@ def css_bar(start, end, color): def css(x): if pd.isna(x): return '' + + # avoid deprecated indexing `colors[x > zero]` + color = colors[1] if x > zero else colors[0] + if align == 'left': - return css_bar(0, x, colors[x > zero]) + return css_bar(0, x, color) else: - return css_bar(min(x, zero), max(x, zero), colors[x > zero]) + return css_bar(min(x, zero), max(x, zero), color) if s.ndim == 1: return [css(x) for x in normed] diff --git a/pandas/util/testing.py b/pandas/util/testing.py index 01fafd7219382..f785ec35f52db 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -2431,7 +2431,7 @@ def assert_raises_regex(_exception, _regexp, _callable=None, You can also use this in a with statement. - >>> with assert_raises_regex(TypeError, 'unsupported operand type\(s\)'): + >>> with assert_raises_regex(TypeError, r'unsupported operand type\(s\)'): ... 1 + {} >>> with assert_raises_regex(TypeError, 'banana'): ... 'apple'[0] = 'b' From ac5afd69df6e955950c201ec3c7fd1c90019decb Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 8 Sep 2018 20:45:40 -0700 Subject: [PATCH 2/3] Avoid DeprecationWarnings for non-integer numeric indexers --- pandas/core/indexes/base.py | 4 ++++ pandas/core/indexes/multi.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 710c9d0e296c9..b8136d275eba4 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2047,6 +2047,10 @@ def __getitem__(self, key): promote = self._shallow_copy if is_scalar(key): + if is_float(key) and key == int(key): + # avoid numpy(?) DeprecationWarning about non-integer + # number passed + key = int(key) return getitem(key) if isinstance(key, slice): diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 955f1461075f9..03b0cc150df18 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1551,6 +1551,10 @@ def __setstate__(self, state): def __getitem__(self, key): if is_scalar(key): + if lib.is_float(key) and key == int(key): + # Avoid DeprecationWarning for non-integer number indexer + key = int(key) + retval = [] for lev, lab in zip(self.levels, self.labels): if lab[key] == -1: From 162962ead13c339856f111c594f6872c74d13397 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sun, 9 Sep 2018 14:33:19 -0700 Subject: [PATCH 3/3] implement cast_scalar_indexer --- pandas/core/common.py | 18 ++++++++++++++++++ pandas/core/indexes/base.py | 5 +---- pandas/core/indexes/multi.py | 4 +--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/pandas/core/common.py b/pandas/core/common.py index a3fba762509f1..92e4e23ce958e 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -122,6 +122,24 @@ def is_bool_indexer(key): return False +def cast_scalar_indexer(val): + """ + To avoid numpy DeprecationWarnings, cast float to integer where valid. + + Parameters + ---------- + val : scalar + + Returns + ------- + outval : scalar + """ + # assumes lib.is_scalar(val) + if lib.is_float(val) and val == int(val): + return int(val) + return val + + def _not_none(*args): """Returns a generator consisting of the arguments that are not None""" return (arg for arg in args if arg is not None) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index b8136d275eba4..b2b6e02e908c5 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2047,10 +2047,7 @@ def __getitem__(self, key): promote = self._shallow_copy if is_scalar(key): - if is_float(key) and key == int(key): - # avoid numpy(?) DeprecationWarning about non-integer - # number passed - key = int(key) + key = com.cast_scalar_indexer(key) return getitem(key) if isinstance(key, slice): diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 03b0cc150df18..90743033e492c 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1551,9 +1551,7 @@ def __setstate__(self, state): def __getitem__(self, key): if is_scalar(key): - if lib.is_float(key) and key == int(key): - # Avoid DeprecationWarning for non-integer number indexer - key = int(key) + key = com.cast_scalar_indexer(key) retval = [] for lev, lab in zip(self.levels, self.labels):