Skip to content

Commit 7adee8b

Browse files
committed
reorg
1 parent 46f3270 commit 7adee8b

File tree

11 files changed

+184
-230
lines changed

11 files changed

+184
-230
lines changed

pandas/core/arrays/_mask.py

-41
This file was deleted.

pandas/core/arrays/_numpy_bool.py

-111
This file was deleted.

pandas/core/arrays/integer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from pandas.core import nanops
2121
from pandas.core.arrays import ExtensionArray, ExtensionOpsMixin
22-
from pandas.core.arrays._mask import get_mask_array_type
22+
from pandas.core.arrays.mask import get_mask_array_type
2323
from pandas.core.tools.numeric import to_numeric
2424

2525

pandas/core/arrays/mask/__init__.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
_MaskArrayType = None
2+
3+
4+
def get_mask_array_type():
5+
"""Set the mask array type to use, we need to do
6+
this after all modules are imported as the implementations
7+
e.g. pyarrow depend on pandas being importable
8+
"""
9+
global _MaskArrayType
10+
11+
if _MaskArrayType is not None:
12+
return _MaskArrayType
13+
14+
# if ArrowBoolArray is available use it
15+
# otherwise use the NumpyMask
16+
try:
17+
from pandas.core.arrays.mask._pyarrow_bool import ArrowBoolArray
18+
19+
MaskArray = ArrowBoolArray
20+
21+
except ImportError:
22+
from pandas.core.arrays.mask._numpy_bool import NumpyBoolArray
23+
24+
MaskArray = NumpyBoolArray
25+
26+
_MaskArrayType = MaskArray
27+
return _MaskArrayType
28+
29+
30+
__all__ = ['get_mask_array_type']

pandas/core/arrays/mask/_base.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""A boolean mask interace.
2+
3+
This module provides an interface to a numpy / pyarrow boolean mask.
4+
This is limited as not all of the implementations can hold NA, so
5+
for consistency this is an internal.
6+
"""
7+
8+
import copy
9+
import numpy as np
10+
from pandas.core.missing import isna
11+
from pandas.core.arrays.base import ExtensionArray
12+
13+
14+
class BoolArray(ExtensionArray):
15+
"""Common baseclass for both pyarrow and numpy masked arrays"""
16+
_typ = "maskarray"
17+
18+
@classmethod
19+
def _from_sequence(cls, scalars, dtype=None, copy=False):
20+
return cls.from_scalars(scalars)
21+
22+
@property
23+
def size(self):
24+
return len(self)
25+
26+
def __eq__(self, other):
27+
return np.array(self, copy=False) == np.array(other, copy=False)
28+
29+
def __len__(self):
30+
return len(self._data)
31+
32+
def isna(self):
33+
nas = isna(np.array(self._data, copy=False))
34+
return type(self).from_scalars(nas)
35+
36+
def __invert__(self):
37+
return type(self).from_scalars(
38+
~np.array(self._data, copy=False)
39+
)
40+
41+
def __or__(self, other):
42+
return type(self).from_scalars(np.array(
43+
self, copy=False).__or__(np.array(other, copy=False)))
44+
45+
def __ior__(self, other):
46+
return type(self).from_scalars(
47+
np.array(self, copy=False) | np.array(other, copy=False))
48+
49+
def __and__(self, other):
50+
return type(self).from_scalars(
51+
np.array(self, copy=False).__and__(np.array(other, copy=False)))
52+
53+
def __iand__(self, other):
54+
return type(self).from_scalars(
55+
np.array(self, copy=False) & (np.array(other, copy=False)))
56+
57+
def view(self, dtype):
58+
return np.array(self._data, copy=False).view(dtype)
59+
60+
def sum(self, axis=None):
61+
return np.array(self, copy=False).sum()
62+
63+
def copy(self, deep=False):
64+
if deep:
65+
return type(self)(copy.deepcopy(self._data))
66+
else:
67+
return type(self)(copy.copy(self._data))
68+
69+
def _reduce(self, method, skipna=True, **kwargs):
70+
if skipna:
71+
arr = self[~self.isna()]
72+
else:
73+
arr = self
74+
75+
try:
76+
op = getattr(arr, method)
77+
except AttributeError:
78+
raise TypeError
79+
return op(**kwargs)
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
This module provide a numpy-boolean boolean array
3+
"""
4+
5+
import numpy as np
6+
7+
from pandas.core.arrays.mask._base import BoolArray
8+
9+
10+
class NumpyBoolArray(BoolArray):
11+
"""Generic class which can be used to represent missing data.
12+
"""
13+
14+
@property
15+
def dtype(self):
16+
return np.dtype('bool')
17+
18+
@classmethod
19+
def from_scalars(cls, values):
20+
arr = np.asarray(values).astype(np.bool_, copy=False)
21+
return cls(arr, copy=False)
22+
23+
def __init__(self, mask, copy=True):
24+
"""
25+
Parameters
26+
----------
27+
mask : numpy array
28+
Mask of missing values.
29+
"""
30+
assert isinstance(mask, np.ndarray)
31+
assert mask.dtype == np.bool_
32+
33+
if copy:
34+
mask = mask.copy()
35+
self._data = mask
36+
37+
def __getitem__(self, key):
38+
return self._data[key]
39+
40+
def __setitem__(self, key, value):
41+
self._data[key] = value
42+
43+
def __array__(self, dtype=None):
44+
return self._data
45+
46+
def __iter__(self):
47+
return iter(self._data)
48+
49+
@property
50+
def nbytes(self):
51+
return self._data.nbytes
52+
53+
def reshape(self, shape, **kwargs):
54+
return np.array(self, copy=False).reshape(shape, **kwargs)
55+
56+
def astype(self, dtype, copy=False):
57+
return np.array(self, copy=False).astype(dtype, copy=copy)
58+
59+
def any(self):
60+
return self._data.any()
61+
62+
def all(self):
63+
return self._data.all()
64+
65+
def take(self, indicies, **kwargs):
66+
return np.array(self, copy=False).take(indicies)

0 commit comments

Comments
 (0)