Debian Codegraph @

hypothesis-python/tests/pandas/test_series.py

raw
1 # This file is part of Hypothesis, which may be found at
2 # https://github.com/HypothesisWorks/hypothesis/
3 #
4 # Copyright the Hypothesis Authors.
5 # Individual contributors are listed in AUTHORS.rst and the git log.
6 #
7 # This Source Code Form is subject to the terms of the Mozilla Public License,
8 # v. 2.0. If a copy of the MPL was not distributed with this file, You can
9 # obtain one at https://mozilla.org/MPL/2.0/.
10
11 import numpy as np
12 import pandas as pd
13 import pytest
14
15 from hypothesis import given, settings, strategies as st
16 from hypothesis.errors import InvalidArgument
17 from hypothesis.extra import numpy as nps, pandas as pdst
18 from hypothesis.extra.pandas.impl import IntegerDtype
19
20 from tests.common.debug import (
21 assert_all_examples,
22 assert_no_examples,
23 check_can_generate_examples,
24 find_any,
25 )
26 from tests.pandas.helpers import supported_by_pandas
27
28
29 @given(st.data())
30 def test_can_create_a_series_of_any_dtype(data):
31 dtype = data.draw(nps.scalar_dtypes().filter(supported_by_pandas))
32 # Use raw data to work around pandas bug in repr. See
33 # https://github.com/pandas-dev/pandas/issues/27484
34 series = data.conjecture_data.draw(pdst.series(dtype=dtype))
35 assert series.dtype == dtype
36
37
38 @given(pdst.series(dtype=object))
39 def test_can_create_a_series_of_object_python_type(series):
40 assert series.dtype == np.dtype("O")
41
42
43 @given(
44 pdst.series(
45 elements=nps.arrays(
46 nps.array_dtypes() | nps.scalar_dtypes(),
47 nps.array_shapes(),
48 ),
49 dtype=object,
50 )
51 )
52 @settings(max_examples=5)
53 def test_object_series_are_of_type_object(series):
54 assert series.dtype == np.dtype("O")
55
56
57 def test_class_instances_not_allowed_in_scalar_series():
58 class A:
59 pass
60
61 with pytest.raises(InvalidArgument):
62 check_can_generate_examples(
63 pdst.series(elements=st.just(A()), dtype=np.dtype("int"))
64 )
65
66
67 def test_object_series_with_mixed_elements_still_has_object_dtype():
68 class A:
69 pass
70
71 s = nps.arrays(
72 np.dtype("O"),
73 shape=nps.array_shapes(),
74 elements=st.just(A()) | st.integers(),
75 )
76
77 assert_all_examples(s, lambda arr: arr.dtype == np.dtype("O"))
78 find_any(s, lambda arr: len({type(x) for x in arr.ravel()}) > 1)
79
80
81 @given(st.data())
82 @settings(max_examples=10)
83 def test_series_can_hold_arbitrary_class_instances(data):
84 instance = data.draw(st.from_type(type).flatmap(st.from_type))
85 s = pdst.series(elements=st.just(instance), dtype=object)
86 series = data.draw(s)
87
88 assert all(v is instance for v in series.values)
89
90
91 @given(pdst.series(dtype=float, index=pdst.range_indexes(min_size=2, max_size=5)))
92 def test_series_respects_size_bounds(s):
93 assert 2 <= len(s) <= 5
94
95
96 def test_can_fill_series():
97 nan_backed = pdst.series(elements=st.floats(allow_nan=False), fill=st.just(np.nan))
98 find_any(nan_backed, lambda x: np.isnan(x).any())
99
100
101 @given(pdst.series(dtype=int))
102 def test_can_generate_integral_series(s):
103 assert s.dtype == np.dtype(int)
104
105
106 @given(pdst.series(elements=st.integers(0, 10)))
107 def test_will_use_dtype_of_elements(s):
108 assert s.dtype == np.dtype("int64")
109
110
111 @given(pdst.series(elements=st.floats(allow_nan=False)))
112 def test_will_use_a_provided_elements_strategy(s):
113 assert not np.isnan(s).any()
114
115
116 @given(pdst.series(dtype="int8", unique=True))
117 def test_unique_series_are_unique(s):
118 assert len(s) == len(set(s))
119
120
121 @given(pdst.series(dtype="int8", name=st.just("test_name")))
122 def test_name_passed_on(s):
123 assert s.name == "test_name"
124
125
126 @pytest.mark.skipif(
127 not IntegerDtype, reason="Nullable types not available in this version of Pandas"
128 )
129 @pytest.mark.parametrize(
130 "dtype", ["Int8", pd.core.arrays.integer.Int8Dtype() if IntegerDtype else None]
131 )
132 def test_pandas_nullable_types(dtype):
133 assert_no_examples(
134 pdst.series(dtype=dtype, elements=st.just(0)),
135 lambda s: s.isna().any(),
136 )
137 assert_all_examples(
138 pdst.series(dtype=dtype, elements=st.none()),
139 lambda s: s.isna().all(),
140 )
141 find_any(pdst.series(dtype=dtype), lambda s: not s.isna().any())
142 e = find_any(pdst.series(dtype=dtype), lambda s: s.isna().any())
143 assert type(e.dtype) == pd.core.arrays.integer.Int8Dtype