Skip to content

TST: Avoid DeprecationWarnings #22646

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 4 commits into from
Sep 12, 2018
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
18 changes: 18 additions & 0 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down