Skip to content

Commit d526a4f

Browse files
committed
COMPAT: avoid warnings from numeric/string-like comparisons
1 parent 2bf9fb5 commit d526a4f

File tree

3 files changed

+58
-19
lines changed

3 files changed

+58
-19
lines changed

pandas/core/common.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def _isnull_ndarraylike(obj):
232232
values = getattr(obj, 'values', obj)
233233
dtype = values.dtype
234234

235-
if dtype.kind in ('O', 'S', 'U'):
235+
if is_string_dtype(dtype):
236236
if is_categorical_dtype(values):
237237
from pandas import Categorical
238238
if not isinstance(values, Categorical):
@@ -243,7 +243,7 @@ def _isnull_ndarraylike(obj):
243243
# Working around NumPy ticket 1542
244244
shape = values.shape
245245

246-
if dtype.kind in ('S', 'U'):
246+
if is_string_like_dtype(dtype):
247247
result = np.zeros(values.shape, dtype=bool)
248248
else:
249249
result = np.empty(shape, dtype=bool)
@@ -267,11 +267,11 @@ def _isnull_ndarraylike_old(obj):
267267
values = getattr(obj, 'values', obj)
268268
dtype = values.dtype
269269

270-
if dtype.kind in ('O', 'S', 'U'):
270+
if is_string_dtype(dtype):
271271
# Working around NumPy ticket 1542
272272
shape = values.shape
273273

274-
if values.dtype.kind in ('S', 'U'):
274+
if is_string_like_dtype(dtype):
275275
result = np.zeros(values.shape, dtype=bool)
276276
else:
277277
result = np.empty(shape, dtype=bool)
@@ -2208,13 +2208,17 @@ def is_numeric_v_string_like(a, b):
22082208

22092209
is_a_numeric_array = is_a_array and is_numeric_dtype(a)
22102210
is_b_numeric_array = is_b_array and is_numeric_dtype(b)
2211+
is_a_string_array = is_a_array and is_string_like_dtype(a)
2212+
is_b_string_array = is_b_array and is_string_like_dtype(b)
22112213

22122214
is_a_scalar_string_like = not is_a_array and is_string_like(a)
22132215
is_b_scalar_string_like = not is_b_array and is_string_like(b)
22142216

22152217
return (
22162218
is_a_numeric_array and is_b_scalar_string_like) or (
2217-
is_b_numeric_array and is_a_scalar_string_like
2219+
is_b_numeric_array and is_a_scalar_string_like) or (
2220+
is_a_numeric_array and is_b_string_array) or (
2221+
is_b_numeric_array and is_a_string_array
22182222
)
22192223

22202224
def is_datetimelike_v_numeric(a, b):
@@ -2257,6 +2261,15 @@ def is_numeric_dtype(arr_or_dtype):
22572261
and not issubclass(tipo, (np.datetime64, np.timedelta64)))
22582262

22592263

2264+
def is_string_dtype(arr_or_dtype):
2265+
dtype = _get_dtype(arr_or_dtype)
2266+
return dtype.kind in ('O', 'S', 'U')
2267+
2268+
def is_string_like_dtype(arr_or_dtype):
2269+
# exclude object as its a mixed dtype
2270+
dtype = _get_dtype(arr_or_dtype)
2271+
return dtype.kind in ('S', 'U')
2272+
22602273
def is_float_dtype(arr_or_dtype):
22612274
tipo = _get_dtype_type(arr_or_dtype)
22622275
return issubclass(tipo, np.floating)

pandas/core/internals.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -4419,11 +4419,14 @@ def _putmask_smart(v, m, n):
44194419
try:
44204420
nn = n[m]
44214421
nn_at = nn.astype(v.dtype)
4422-
comp = (nn == nn_at)
4423-
if is_list_like(comp) and comp.all():
4424-
nv = v.copy()
4425-
nv[m] = nn_at
4426-
return nv
4422+
4423+
# avoid invalid dtype comparisons
4424+
if not is_numeric_v_string_like(nn, nn_at):
4425+
comp = (nn == nn_at)
4426+
if is_list_like(comp) and comp.all():
4427+
nv = v.copy()
4428+
nv[m] = nn_at
4429+
return nv
44274430
except (ValueError, IndexError, TypeError):
44284431
pass
44294432

pandas/tests/test_series.py

+32-9
Original file line numberDiff line numberDiff line change
@@ -5112,26 +5112,49 @@ def test_cov(self):
51125112

51135113
def test_copy(self):
51145114

5115-
for deep in [False, True]:
5115+
for deep in [None, False, True]:
51165116
s = Series(np.arange(10),dtype='float64')
5117-
s2 = s.copy(deep=deep)
5117+
5118+
# default deep is True
5119+
if deep is None:
5120+
s2 = s.copy()
5121+
else:
5122+
s2 = s.copy(deep=deep)
5123+
51185124
s2[::2] = np.NaN
51195125

5120-
# Did not modify original Series
5121-
self.assertTrue(np.isnan(s2[0]))
5122-
self.assertFalse(np.isnan(s[0]))
5126+
if deep is None or deep is True:
5127+
# Did not modify original Series
5128+
self.assertTrue(np.isnan(s2[0]))
5129+
self.assertFalse(np.isnan(s[0]))
5130+
else:
5131+
5132+
# we DID modify the original Series
5133+
self.assertTrue(np.isnan(s2[0]))
5134+
self.assertTrue(np.isnan(s[0]))
51235135

51245136
# GH 11794
51255137
# copy of tz-aware
51265138
expected = Series([Timestamp('2012/01/01', tz='UTC')])
51275139
expected2 = Series([Timestamp('1999/01/01', tz='UTC')])
51285140

5129-
for deep in [False, True]:
5141+
for deep in [None, False, True]:
51305142
s = Series([Timestamp('2012/01/01', tz='UTC')])
5131-
s2 = s.copy()
5143+
5144+
if deep is None:
5145+
s2 = s.copy()
5146+
else:
5147+
s2 = s.copy(deep=deep)
5148+
51325149
s2[0] = pd.Timestamp('1999/01/01', tz='UTC')
5133-
assert_series_equal(s, expected)
5134-
assert_series_equal(s2, expected2)
5150+
5151+
# default deep is True
5152+
if deep is None or deep is True:
5153+
assert_series_equal(s, expected)
5154+
assert_series_equal(s2, expected2)
5155+
else:
5156+
assert_series_equal(s, expected2)
5157+
assert_series_equal(s2, expected2)
51355158

51365159
def test_count(self):
51375160
self.assertEqual(self.ts.count(), len(self.ts))

0 commit comments

Comments
 (0)