Skip to content

Commit 116f8d2

Browse files
TST: Using more fixtures in of tests/base/test_ops.py (#32313)
1 parent 4ba48f0 commit 116f8d2

File tree

3 files changed

+88
-48
lines changed

3 files changed

+88
-48
lines changed

pandas/conftest.py

+10
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,16 @@ def series_with_simple_index(indices):
10471047
for dtype in _narrow_dtypes
10481048
}
10491049

1050+
1051+
@pytest.fixture(params=_narrow_series.keys())
1052+
def narrow_series(request):
1053+
"""
1054+
Fixture for Series with low precision data types
1055+
"""
1056+
# copy to avoid mutation, e.g. setting .name
1057+
return _narrow_series[request.param].copy()
1058+
1059+
10501060
_index_or_series_objs = {**indices_dict, **_series, **_narrow_series}
10511061

10521062

pandas/tests/base/test_ops.py

+69-47
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pandas.compat.numpy import np_array_datetime64_compat
1212

1313
from pandas.core.dtypes.common import (
14+
is_categorical_dtype,
1415
is_datetime64_dtype,
1516
is_datetime64tz_dtype,
1617
is_object_dtype,
@@ -802,62 +803,83 @@ def test_fillna(self):
802803
assert o is not result
803804

804805
@pytest.mark.skipif(PYPY, reason="not relevant for PyPy")
805-
def test_memory_usage(self):
806-
for o in self.objs:
807-
res = o.memory_usage()
808-
res_deep = o.memory_usage(deep=True)
809-
810-
if is_object_dtype(o) or (
811-
isinstance(o, Series) and is_object_dtype(o.index)
812-
):
813-
# if there are objects, only deep will pick them up
814-
assert res_deep > res
815-
else:
816-
assert res == res_deep
817-
818-
if isinstance(o, Series):
819-
assert (
820-
o.memory_usage(index=False) + o.index.memory_usage()
821-
) == o.memory_usage(index=True)
806+
def test_memory_usage(self, index_or_series_obj):
807+
obj = index_or_series_obj
808+
res = obj.memory_usage()
809+
res_deep = obj.memory_usage(deep=True)
822810

823-
# sys.getsizeof will call the .memory_usage with
824-
# deep=True, and add on some GC overhead
825-
diff = res_deep - sys.getsizeof(o)
826-
assert abs(diff) < 100
811+
is_object = is_object_dtype(obj) or (
812+
isinstance(obj, Series) and is_object_dtype(obj.index)
813+
)
814+
is_categorical = is_categorical_dtype(obj) or (
815+
isinstance(obj, Series) and is_categorical_dtype(obj.index)
816+
)
827817

828-
def test_searchsorted(self):
829-
# See gh-12238
830-
for o in self.objs:
831-
index = np.searchsorted(o, max(o))
832-
assert 0 <= index <= len(o)
818+
if len(obj) == 0:
819+
assert res_deep == res == 0
820+
elif is_object or is_categorical:
821+
# only deep will pick them up
822+
assert res_deep > res
823+
else:
824+
assert res == res_deep
833825

834-
index = np.searchsorted(o, max(o), sorter=range(len(o)))
835-
assert 0 <= index <= len(o)
826+
# sys.getsizeof will call the .memory_usage with
827+
# deep=True, and add on some GC overhead
828+
diff = res_deep - sys.getsizeof(obj)
829+
assert abs(diff) < 100
836830

837-
def test_validate_bool_args(self):
838-
invalid_values = [1, "True", [1, 2, 3], 5.0]
831+
def test_memory_usage_components_series(self, series_with_simple_index):
832+
series = series_with_simple_index
833+
total_usage = series.memory_usage(index=True)
834+
non_index_usage = series.memory_usage(index=False)
835+
index_usage = series.index.memory_usage()
836+
assert total_usage == non_index_usage + index_usage
837+
838+
def test_memory_usage_components_narrow_series(self, narrow_series):
839+
series = narrow_series
840+
total_usage = series.memory_usage(index=True)
841+
non_index_usage = series.memory_usage(index=False)
842+
index_usage = series.index.memory_usage()
843+
assert total_usage == non_index_usage + index_usage
844+
845+
def test_searchsorted(self, index_or_series_obj):
846+
# numpy.searchsorted calls obj.searchsorted under the hood.
847+
# See gh-12238
848+
obj = index_or_series_obj
839849

840-
for value in invalid_values:
841-
msg = "expected type bool"
842-
with pytest.raises(ValueError, match=msg):
843-
self.int_series.drop_duplicates(inplace=value)
850+
if isinstance(obj, pd.MultiIndex):
851+
# See gh-14833
852+
pytest.skip("np.searchsorted doesn't work on pd.MultiIndex")
844853

845-
def test_getitem(self):
846-
for i in self.indexes:
847-
s = pd.Series(i)
854+
max_obj = max(obj, default=0)
855+
index = np.searchsorted(obj, max_obj)
856+
assert 0 <= index <= len(obj)
848857

849-
assert i[0] == s.iloc[0]
850-
assert i[5] == s.iloc[5]
851-
assert i[-1] == s.iloc[-1]
858+
index = np.searchsorted(obj, max_obj, sorter=range(len(obj)))
859+
assert 0 <= index <= len(obj)
852860

853-
assert i[-1] == i[9]
861+
def test_access_by_position(self, indices):
862+
index = indices
854863

855-
msg = "index 20 is out of bounds for axis 0 with size 10"
856-
with pytest.raises(IndexError, match=msg):
857-
i[20]
858-
msg = "single positional indexer is out-of-bounds"
859-
with pytest.raises(IndexError, match=msg):
860-
s.iloc[20]
864+
if len(index) == 0:
865+
pytest.skip("Test doesn't make sense on empty data")
866+
elif isinstance(index, pd.MultiIndex):
867+
pytest.skip("Can't instantiate Series from MultiIndex")
868+
869+
series = pd.Series(index)
870+
assert index[0] == series.iloc[0]
871+
assert index[5] == series.iloc[5]
872+
assert index[-1] == series.iloc[-1]
873+
874+
size = len(index)
875+
assert index[-1] == index[size - 1]
876+
877+
msg = f"index {size} is out of bounds for axis 0 with size {size}"
878+
with pytest.raises(IndexError, match=msg):
879+
index[size]
880+
msg = "single positional indexer is out-of-bounds"
881+
with pytest.raises(IndexError, match=msg):
882+
series.iloc[size]
861883

862884
@pytest.mark.parametrize("indexer_klass", [list, pd.Index])
863885
@pytest.mark.parametrize(

pandas/tests/series/test_validate.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33

44
@pytest.mark.parametrize(
55
"func",
6-
["reset_index", "_set_name", "sort_values", "sort_index", "rename", "dropna"],
6+
[
7+
"reset_index",
8+
"_set_name",
9+
"sort_values",
10+
"sort_index",
11+
"rename",
12+
"dropna",
13+
"drop_duplicates",
14+
],
715
)
816
@pytest.mark.parametrize("inplace", [1, "True", [1, 2, 3], 5.0])
917
def test_validate_bool_args(string_series, func, inplace):

0 commit comments

Comments
 (0)