Skip to content

Commit a214e82

Browse files
committed
CLN: move com.mask_missing to missing
1 parent e77be39 commit a214e82

File tree

5 files changed

+53
-52
lines changed

5 files changed

+53
-52
lines changed

pandas/core/common.py

-47
Original file line numberDiff line numberDiff line change
@@ -378,53 +378,6 @@ def flatten(l):
378378
yield el
379379

380380

381-
def mask_missing(arr, values_to_mask):
382-
"""
383-
Return a masking array of same size/shape as arr
384-
with entries equaling any member of values_to_mask set to True
385-
"""
386-
if not isinstance(values_to_mask, (list, np.ndarray)):
387-
values_to_mask = [values_to_mask]
388-
389-
try:
390-
values_to_mask = np.array(values_to_mask, dtype=arr.dtype)
391-
except Exception:
392-
values_to_mask = np.array(values_to_mask, dtype=object)
393-
394-
na_mask = isnull(values_to_mask)
395-
nonna = values_to_mask[~na_mask]
396-
397-
mask = None
398-
for x in nonna:
399-
if mask is None:
400-
401-
# numpy elementwise comparison warning
402-
if is_numeric_v_string_like(arr, x):
403-
mask = False
404-
else:
405-
mask = arr == x
406-
407-
# if x is a string and arr is not, then we get False and we must
408-
# expand the mask to size arr.shape
409-
if lib.isscalar(mask):
410-
mask = np.zeros(arr.shape, dtype=bool)
411-
else:
412-
413-
# numpy elementwise comparison warning
414-
if is_numeric_v_string_like(arr, x):
415-
mask |= False
416-
else:
417-
mask |= arr == x
418-
419-
if na_mask.any():
420-
if mask is None:
421-
mask = isnull(arr)
422-
else:
423-
mask |= isnull(arr)
424-
425-
return mask
426-
427-
428381
def _pickle_array(arr):
429382
arr = arr.view(np.ndarray)
430383

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def _single_replace(self, to_replace, method, inplace, limit):
5454
result = self if inplace else self.copy()
5555
fill_f = missing.get_fill_func(method)
5656

57-
mask = com.mask_missing(result.values, to_replace)
57+
mask = missing.mask_missing(result.values, to_replace)
5858
values = fill_f(result.values, limit=limit, mask=mask)
5959

6060
if values.dtype == orig_dtype and inplace:

pandas/core/internals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ def replace(self, to_replace, value, inplace=False, filter=None,
597597
try:
598598
values, _, to_replace, _ = self._try_coerce_args(self.values,
599599
to_replace)
600-
mask = com.mask_missing(values, to_replace)
600+
mask = missing.mask_missing(values, to_replace)
601601
if filter is not None:
602602
filtered_out = ~self.mgr_locs.isin(filter)
603603
mask[filtered_out.nonzero()[0]] = False

pandas/core/missing.py

+48-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,53 @@
1010
from pandas.compat import range
1111

1212

13+
def mask_missing(arr, values_to_mask):
14+
"""
15+
Return a masking array of same size/shape as arr
16+
with entries equaling any member of values_to_mask set to True
17+
"""
18+
if not isinstance(values_to_mask, (list, np.ndarray)):
19+
values_to_mask = [values_to_mask]
20+
21+
try:
22+
values_to_mask = np.array(values_to_mask, dtype=arr.dtype)
23+
except Exception:
24+
values_to_mask = np.array(values_to_mask, dtype=object)
25+
26+
na_mask = com.isnull(values_to_mask)
27+
nonna = values_to_mask[~na_mask]
28+
29+
mask = None
30+
for x in nonna:
31+
if mask is None:
32+
33+
# numpy elementwise comparison warning
34+
if com.is_numeric_v_string_like(arr, x):
35+
mask = False
36+
else:
37+
mask = arr == x
38+
39+
# if x is a string and arr is not, then we get False and we must
40+
# expand the mask to size arr.shape
41+
if lib.isscalar(mask):
42+
mask = np.zeros(arr.shape, dtype=bool)
43+
else:
44+
45+
# numpy elementwise comparison warning
46+
if com.is_numeric_v_string_like(arr, x):
47+
mask |= False
48+
else:
49+
mask |= arr == x
50+
51+
if na_mask.any():
52+
if mask is None:
53+
mask = com.isnull(arr)
54+
else:
55+
mask |= com.isnull(arr)
56+
57+
return mask
58+
59+
1360
def clean_fill_method(method, allow_nearest=False):
1461
if method is None:
1562
return None
@@ -239,7 +286,7 @@ def interpolate_2d(values, method='pad', axis=0, limit=None, fill_value=None,
239286
if fill_value is None:
240287
mask = None
241288
else: # todo create faster fill func without masking
242-
mask = com.mask_missing(transf(values), fill_value)
289+
mask = mask_missing(transf(values), fill_value)
243290

244291
method = clean_fill_method(method)
245292
if method == 'pad':

pandas/tseries/period.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from pandas.lib import Timedelta
2525
import pandas.lib as lib
2626
import pandas.tslib as tslib
27+
import pandas.core.missing as missing
2728
from pandas.compat import zip, u
2829

2930

@@ -77,8 +78,8 @@ def wrapper(self, other):
7778

7879
result = getattr(self.values, opname)(other.values)
7980

80-
mask = (com.mask_missing(self.values, tslib.iNaT) |
81-
com.mask_missing(other.values, tslib.iNaT))
81+
mask = (missing.mask_missing(self.values, tslib.iNaT) |
82+
missing.mask_missing(other.values, tslib.iNaT))
8283
if mask.any():
8384
result[mask] = nat_result
8485

0 commit comments

Comments
 (0)