Debian Codegraph @

hypothesis-python/tests/pandas/test_indexes.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 sys
12
13 import numpy as np
14 import pandas
15 import pytest
16
17 from hypothesis import HealthCheck, given, reject, settings, strategies as st
18 from hypothesis.errors import Unsatisfiable
19 from hypothesis.extra import numpy as npst, pandas as pdst
20
21 from tests.common.debug import check_can_generate_examples
22 from tests.pandas.helpers import supported_by_pandas
23
24
25 # https://pandas.pydata.org/docs/whatsnew/v2.0.0.html#index-can-now-hold-numpy-numeric-dtypes
26 @given(pdst.indexes(dtype=int, max_size=0))
27 def test_gets_right_dtype_for_empty_indices(ix):
28 is_32bit = sys.maxsize == 2**31 - 1
29 pandas2 = pandas.__version__.startswith("2.")
30 numpy1 = np.__version__.startswith("1.")
31 windows = sys.platform == "win32" # including 64-bit windows, confusingly
32 if pandas2 and (is_32bit or (windows and numpy1)):
33 # No, I don't know what this is int32 on 64-bit windows until Numpy 2.0
34 assert ix.dtype == np.dtype("int32")
35 else:
36 assert ix.dtype == np.dtype("int64")
37
38
39 @given(pdst.indexes(elements=st.integers(0, sys.maxsize), max_size=0))
40 def test_gets_right_dtype_for_empty_indices_with_elements(ix):
41 assert ix.dtype == np.dtype("int64")
42
43
44 def test_does_not_generate_impossible_conditions():
45 with pytest.raises(Unsatisfiable):
46 check_can_generate_examples(pdst.indexes(min_size=3, max_size=3, dtype=bool))
47
48
49 @given(pdst.indexes(dtype=bool, unique=True))
50 def test_unique_indexes_of_small_values(ix):
51 assert len(ix) <= 2
52 assert len(set(ix)) == len(ix)
53
54
55 @given(pdst.indexes(dtype=bool, min_size=2, unique=True))
56 def test_unique_indexes_of_many_small_values(ix):
57 assert len(ix) == 2
58 assert len(set(ix)) == len(ix)
59
60
61 @given(pdst.indexes(dtype="int8", name=st.just("test_name")))
62 def test_name_passed_on_indexes(s):
63 assert s.name == "test_name"
64
65
66 # Sizes that fit into an int64 without overflow
67 range_sizes = st.integers(0, 2**63 - 1)
68
69
70 @given(range_sizes, range_sizes | st.none(), st.data())
71 def test_arbitrary_range_index(i, j, data):
72 if j is not None:
73 i, j = sorted((i, j))
74 data.draw(pdst.range_indexes(i, j))
75
76
77 @given(pdst.range_indexes(name=st.just("test_name")))
78 def test_name_passed_on_range_indexes(s):
79 assert s.name == "test_name"
80
81
82 @given(pdst.range_indexes())
83 def test_basic_range_indexes(ix):
84 assert isinstance(ix, pandas.RangeIndex)
85
86
87 @settings(suppress_health_check=[HealthCheck.too_slow])
88 @given(st.data())
89 def test_generate_arbitrary_indices(data):
90 min_size = data.draw(st.integers(0, 10), "min_size")
91 max_size = data.draw(st.none() | st.integers(min_size, min_size + 10), "max_size")
92 unique = data.draw(st.booleans(), "unique")
93 dtype = data.draw(
94 st.one_of(
95 npst.boolean_dtypes(),
96 npst.integer_dtypes(endianness="="),
97 npst.floating_dtypes(endianness="=", sizes=(32, 64)),
98 npst.datetime64_dtypes(endianness="="),
99 npst.timedelta64_dtypes(endianness="="),
100 ).filter(supported_by_pandas),
101 "dtype",
102 )
103 pass_elements = data.draw(st.booleans(), "pass_elements")
104
105 converted_dtype = pandas.Index([], dtype=dtype).dtype
106
107 try:
108 inferred_dtype = pandas.Index([data.draw(npst.from_dtype(dtype))]).dtype
109
110 if pass_elements:
111 elements = npst.from_dtype(dtype)
112 dtype = None
113 else:
114 elements = None
115
116 index = data.draw(
117 pdst.indexes(
118 elements=elements,
119 dtype=dtype,
120 min_size=min_size,
121 max_size=max_size,
122 unique=unique,
123 )
124 )
125
126 except Exception as e:
127 if type(e).__name__ == "OutOfBoundsDatetime":
128 # See https://github.com/HypothesisWorks/hypothesis-python/pull/826
129 reject()
130 else:
131 raise
132 if dtype is None:
133 assert index.dtype == inferred_dtype
134 else:
135 assert index.dtype == converted_dtype
136
137 if unique:
138 assert len(set(index.values)) == len(index)