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 710c9d0e296c9..b2b6e02e908c5 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2047,6 +2047,7 @@ def __getitem__(self, key): promote = self._shallow_copy if is_scalar(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 955f1461075f9..90743033e492c 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1551,6 +1551,8 @@ def __setstate__(self, state): def __getitem__(self, key): if is_scalar(key): + key = com.cast_scalar_indexer(key) + retval = [] for lev, lab in zip(self.levels, self.labels): if lab[key] == -1: 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'