Skip to content

Commit 6b3e3c2

Browse files
jbrockmendelaeltanawy
authored andcommitted
TST: Avoid DeprecationWarnings (pandas-dev#22646)
1 parent f3b3694 commit 6b3e3c2

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

pandas/core/common.py

+18
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,24 @@ def is_bool_indexer(key):
122122
return False
123123

124124

125+
def cast_scalar_indexer(val):
126+
"""
127+
To avoid numpy DeprecationWarnings, cast float to integer where valid.
128+
129+
Parameters
130+
----------
131+
val : scalar
132+
133+
Returns
134+
-------
135+
outval : scalar
136+
"""
137+
# assumes lib.is_scalar(val)
138+
if lib.is_float(val) and val == int(val):
139+
return int(val)
140+
return val
141+
142+
125143
def _not_none(*args):
126144
"""Returns a generator consisting of the arguments that are not None"""
127145
return (arg for arg in args if arg is not None)

pandas/core/indexes/base.py

+1
Original file line numberDiff line numberDiff line change
@@ -2047,6 +2047,7 @@ def __getitem__(self, key):
20472047
promote = self._shallow_copy
20482048

20492049
if is_scalar(key):
2050+
key = com.cast_scalar_indexer(key)
20502051
return getitem(key)
20512052

20522053
if isinstance(key, slice):

pandas/core/indexes/multi.py

+2
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,8 @@ def __setstate__(self, state):
15581558

15591559
def __getitem__(self, key):
15601560
if is_scalar(key):
1561+
key = com.cast_scalar_indexer(key)
1562+
15611563
retval = []
15621564
for lev, lab in zip(self.levels, self.labels):
15631565
if lab[key] == -1:

pandas/io/formats/style.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1033,10 +1033,14 @@ def css_bar(start, end, color):
10331033
def css(x):
10341034
if pd.isna(x):
10351035
return ''
1036+
1037+
# avoid deprecated indexing `colors[x > zero]`
1038+
color = colors[1] if x > zero else colors[0]
1039+
10361040
if align == 'left':
1037-
return css_bar(0, x, colors[x > zero])
1041+
return css_bar(0, x, color)
10381042
else:
1039-
return css_bar(min(x, zero), max(x, zero), colors[x > zero])
1043+
return css_bar(min(x, zero), max(x, zero), color)
10401044

10411045
if s.ndim == 1:
10421046
return [css(x) for x in normed]

pandas/util/testing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2431,7 +2431,7 @@ def assert_raises_regex(_exception, _regexp, _callable=None,
24312431
24322432
You can also use this in a with statement.
24332433
2434-
>>> with assert_raises_regex(TypeError, 'unsupported operand type\(s\)'):
2434+
>>> with assert_raises_regex(TypeError, r'unsupported operand type\(s\)'):
24352435
... 1 + {}
24362436
>>> with assert_raises_regex(TypeError, 'banana'):
24372437
... 'apple'[0] = 'b'

0 commit comments

Comments
 (0)