Skip to content

Commit 6b6c55f

Browse files
committed
change fixture to use numeric/non_numeric for testing
rename BoolArray -> MaskArray
1 parent 0f8b96f commit 6b6c55f

File tree

9 files changed

+90
-53
lines changed

9 files changed

+90
-53
lines changed

pandas/conftest.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -146,29 +146,36 @@ def all_arithmetic_operators(request):
146146
return request.param
147147

148148

149-
_all_numeric_reductions = ['sum', 'max', 'min',
150-
'mean', 'prod', 'std', 'var', 'median',
151-
'kurt', 'skew']
149+
# reductions that are generally applicable to all data types
150+
_non_numeric_reductions = ['min', 'max', 'sum']
152151

152+
# reductions that are generally application to
153+
# only numeric data dtypes
154+
_numeric_reductions = ['mean', 'prod',
155+
'std', 'var', 'median',
156+
'kurt', 'skew']
153157

154-
@pytest.fixture(params=_all_numeric_reductions)
155-
def all_numeric_reductions(request):
158+
159+
@pytest.fixture(params=_non_numeric_reductions)
160+
def only_non_numeric_reductions(request):
156161
"""
157-
Fixture for numeric reduction names
162+
Fixture for only non numeric reduction names
158163
"""
159164
return request.param
160165

161166

162-
_all_numeric_reductions_for_boolean = ['min', 'max', 'mean', 'prod',
163-
'std', 'var', 'median',
164-
'kurt', 'skew']
167+
@pytest.fixture(params=_numeric_reductions)
168+
def only_numeric_reductions(request):
169+
"""
170+
Fixture for only numeric reduction names
171+
"""
172+
return request.param
165173

166174

167-
@pytest.fixture(params=_all_numeric_reductions_for_boolean)
168-
def all_numeric_reductions_for_boolean(request):
175+
@pytest.fixture(params=_non_numeric_reductions + _numeric_reductions)
176+
def all_numeric_reductions(request):
169177
"""
170-
Fixture for numeric reduction names that are not allowed
171-
for boolean.
178+
Fixture for numeric reduction names
172179
"""
173180
return request.param
174181

pandas/core/arrays/mask/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ def get_mask_array_type():
1414
# if ArrowBoolArray is available use it
1515
# otherwise use the NumpyMask
1616
try:
17-
from pandas.core.arrays.mask._pyarrow import ArrowBoolArray
17+
from pandas.core.arrays.mask._pyarrow import ArrowMaskArray
1818

19-
MaskArray = ArrowBoolArray
19+
MaskArray = ArrowMaskArray
2020

2121
except ImportError:
22-
from pandas.core.arrays.mask._numpy import NumpyBoolArray
22+
from pandas.core.arrays.mask._numpy import NumpyMaskArray
2323

24-
MaskArray = NumpyBoolArray
24+
MaskArray = NumpyMaskArray
2525

2626
_MaskArrayType = MaskArray
2727
return _MaskArrayType

pandas/core/arrays/mask/_base.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from pandas.core.missing import isna
1717

1818

19-
class BoolDtype(ExtensionDtype):
19+
class MaskDtype(ExtensionDtype):
2020

2121
type = np.bool_
2222
kind = 'b'
@@ -48,7 +48,7 @@ def __eq__(self, other):
4848
return hash(self) == hash(other)
4949

5050

51-
class BoolArray(ExtensionArray):
51+
class MaskArray(ExtensionArray):
5252
"""Common baseclass for both pyarrow and numpy masked arrays"""
5353
_typ = "maskarray"
5454

@@ -120,6 +120,12 @@ def any(self, axis=0, out=None):
120120
def all(self, axis=0, out=None):
121121
return np.array(self._data, copy=False).all()
122122

123+
def min(self, axis=0, out=None):
124+
return np.array(self._data, copy=False).min()
125+
126+
def max(self, axis=0, out=None):
127+
return np.array(self._data, copy=False).max()
128+
123129
def _reduce(self, method, skipna=True, **kwargs):
124130
if skipna:
125131
arr = self[~self.isna()]

pandas/core/arrays/mask/_numpy.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
import numpy as np
66

77
from pandas.api.extensions import take
8-
from pandas.core.arrays.mask._base import BoolArray, BoolDtype
8+
from pandas.core.arrays.mask._base import MaskArray, MaskDtype
99

1010

11-
class NumpyBoolDtype(BoolDtype):
11+
class NumpyMaskDtype(MaskDtype):
1212

1313
na_value = np.nan
1414

1515
@classmethod
1616
def construct_array_type(cls):
17-
return NumpyBoolArray
17+
return NumpyMaskArray
1818

1919

20-
class NumpyBoolArray(BoolArray):
20+
class NumpyMaskArray(MaskArray):
2121
"""Generic class which can be used to represent missing data.
2222
"""
2323

24-
dtype = NumpyBoolDtype()
24+
dtype = NumpyMaskDtype()
2525

2626
@classmethod
2727
def from_scalars(cls, values):
@@ -64,7 +64,7 @@ def astype(self, dtype, copy=True):
6464
if copy:
6565
return self.copy()
6666
return self
67-
return super(NumpyBoolArray, self).astype(dtype, copy)
67+
return super(NumpyMaskArray, self).astype(dtype, copy)
6868

6969
def take(self, indices, allow_fill=False, fill_value=None, axis=None):
7070
# TODO: had to add axis here

pandas/core/arrays/mask/_pyarrow.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import numpy as np
1212

1313
from pandas.api.extensions import take
14-
from pandas.core.arrays.mask._base import BoolArray, BoolDtype
14+
from pandas.core.arrays.mask._base import MaskArray, MaskDtype
1515

1616
# we require pyarrow >= 0.10.0
1717

@@ -23,18 +23,18 @@
2323
raise
2424

2525

26-
class ArrowBoolDtype(BoolDtype):
26+
class ArrowMaskDtype(MaskDtype):
2727

2828
na_value = pa.NULL
2929

3030
@classmethod
3131
def construct_array_type(cls):
32-
return ArrowBoolArray
32+
return ArrowMaskArray
3333

3434

35-
class ArrowBoolArray(BoolArray):
35+
class ArrowMaskArray(MaskArray):
3636

37-
dtype = ArrowBoolDtype()
37+
dtype = ArrowMaskDtype()
3838

3939
@classmethod
4040
def from_scalars(cls, values):
@@ -67,7 +67,7 @@ def astype(self, dtype, copy=True):
6767
if copy:
6868
return self.copy()
6969
return self
70-
return super(ArrowBoolArray, self).astype(dtype, copy)
70+
return super(ArrowMaskArray, self).astype(dtype, copy)
7171

7272
@property
7373
def nbytes(self):

pandas/tests/arrays/mask/test_mask.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
def mask_dtype(request):
1010
""" dtype type """
1111
if request.param == 'numpy':
12-
from pandas.core.arrays.mask._numpy import NumpyBoolDtype
13-
return NumpyBoolDtype
12+
from pandas.core.arrays.mask._numpy import NumpyMaskDtype
13+
return NumpyMaskDtype
1414
elif request.param == 'arrow':
1515
pytest.importorskip('pyarrow', minversion="0.10.0")
16-
from pandas.core.arrays.mask._pyarrow import ArrowBoolDtype
17-
return ArrowBoolDtype
16+
from pandas.core.arrays.mask._pyarrow import ArrowMaskDtype
17+
return ArrowMaskDtype
1818
elif request.param == 'mask':
1919
from pandas.core.arrays.mask import get_mask_array_type
2020
return type(get_mask_array_type().dtype)

pandas/tests/extension/base/reduce.py

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ def check_reduce(self, s, op_name, skipna):
1818
expected = getattr(s.astype('float64'), op_name)(skipna=skipna)
1919
tm.assert_almost_equal(result, expected)
2020

21+
def check_reduce_bool(self, s, op_name, skipna):
22+
"""check_reduce with casting back to bool"""
23+
result = getattr(s, op_name)(skipna=skipna)
24+
expected = bool(getattr(s.astype('float64'), op_name)(skipna=skipna))
25+
tm.assert_almost_equal(result, expected)
26+
2127

2228
class BaseNoReduceTests(BaseReduceTests):
2329
""" we don't define any reductions """

pandas/tests/extension/mask/test_numpy_bool.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@
1717
import pytest
1818

1919
import pandas as pd
20-
from pandas.core.arrays.mask._numpy import NumpyBoolArray, NumpyBoolDtype
20+
from pandas.core.arrays.mask._numpy import NumpyMaskArray, NumpyMaskDtype
2121
from pandas.tests.extension import base
2222
import pandas.util.testing as tm
2323

2424

2525
@pytest.fixture
2626
def dtype():
27-
return NumpyBoolDtype()
27+
return NumpyMaskDtype()
2828

2929

3030
@pytest.fixture
3131
def data():
32-
return NumpyBoolArray.from_scalars(np.random.randint(0, 2, size=100,
33-
dtype=bool))
32+
return NumpyMaskArray.from_scalars(
33+
np.random.randint(0, 2, size=100, dtype=bool))
3434

3535

3636
@pytest.fixture
3737
def data_missing():
38-
pytest.skip("not supported in NumpyBoolArray")
38+
pytest.skip("not supported in NumpyMaskArray")
3939

4040

4141
class BaseNumpyTests(object):
@@ -60,14 +60,23 @@ def test_from_dtype(self, data):
6060
class TestReduceBoolean(base.BaseBooleanReduceTests):
6161

6262
@pytest.mark.parametrize('skipna', [True, False])
63-
def test_reduce_series_numeric(
64-
self, data, all_numeric_reductions_for_boolean, skipna):
65-
op_name = all_numeric_reductions_for_boolean
63+
def test_reduce_series(
64+
self, data, only_numeric_reductions, skipna):
65+
op_name = only_numeric_reductions
6666
s = pd.Series(data)
67-
6867
with pytest.raises(TypeError):
6968
getattr(s, op_name)(skipna=skipna)
7069

70+
@pytest.mark.parametrize('skipna', [True, False])
71+
def test_reduce_series_non_numeric(
72+
self, data, only_non_numeric_reductions, skipna):
73+
op_name = only_non_numeric_reductions
74+
s = pd.Series(data)
75+
if op_name == 'sum':
76+
self.check_reduce(s, op_name, skipna)
77+
else:
78+
self.check_reduce_bool(s, op_name, skipna)
79+
7180

7281
def test_is_bool_dtype(data):
7382
assert pd.api.types.is_bool_dtype(data)

pandas/tests/extension/mask/test_pyarrow_bool.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@
2323
pytest.importorskip('pyarrow', minversion="0.10.0")
2424

2525
from pandas.core.arrays.mask._pyarrow import ( # isort:skip
26-
ArrowBoolArray, ArrowBoolDtype)
26+
ArrowMaskArray, ArrowMaskDtype)
2727

2828

2929
@pytest.fixture
3030
def dtype():
31-
return ArrowBoolDtype()
31+
return ArrowMaskDtype()
3232

3333

3434
@pytest.fixture
3535
def data():
36-
return ArrowBoolArray.from_scalars(np.random.randint(0, 2, size=100,
37-
dtype=bool))
36+
return ArrowMaskArray.from_scalars(
37+
np.random.randint(0, 2, size=100, dtype=bool))
3838

3939

4040
@pytest.fixture
4141
def data_missing():
42-
return ArrowBoolArray.from_scalars([None, True])
42+
return ArrowMaskArray.from_scalars([None, True])
4343

4444

4545
class BaseArrowTests(object):
@@ -64,14 +64,23 @@ def test_from_dtype(self, data):
6464
class TestReduceBoolean(base.BaseBooleanReduceTests):
6565

6666
@pytest.mark.parametrize('skipna', [True, False])
67-
def test_reduce_series_numeric(
68-
self, data, all_numeric_reductions_for_boolean, skipna):
69-
op_name = all_numeric_reductions_for_boolean
67+
def test_reduce_series(
68+
self, data, only_numeric_reductions, skipna):
69+
op_name = only_numeric_reductions
7070
s = pd.Series(data)
71-
7271
with pytest.raises(TypeError):
7372
getattr(s, op_name)(skipna=skipna)
7473

74+
@pytest.mark.parametrize('skipna', [True, False])
75+
def test_reduce_series_non_numeric(
76+
self, data, only_non_numeric_reductions, skipna):
77+
op_name = only_non_numeric_reductions
78+
s = pd.Series(data)
79+
if op_name == 'sum':
80+
self.check_reduce(s, op_name, skipna)
81+
else:
82+
self.check_reduce_bool(s, op_name, skipna)
83+
7584

7685
def test_is_bool_dtype(data):
7786
assert pd.api.types.is_bool_dtype(data)

0 commit comments

Comments
 (0)