forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_common.py
292 lines (238 loc) · 8.51 KB
/
test_common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import collections
from functools import partial
import string
import subprocess
import sys
import textwrap
import numpy as np
import pytest
import pandas.util._test_decorators as td
import pandas as pd
from pandas import Series
import pandas._testing as tm
from pandas.core import ops
import pandas.core.common as com
from pandas.util.version import Version
def test_get_callable_name():
getname = com.get_callable_name
def fn(x):
return x
lambda_ = lambda x: x
part1 = partial(fn)
part2 = partial(part1)
class somecall:
def __call__(self):
# This shouldn't actually get called below; somecall.__init__
# should.
raise NotImplementedError
assert getname(fn) == "fn"
assert getname(lambda_)
assert getname(part1) == "fn"
assert getname(part2) == "fn"
assert getname(somecall()) == "somecall"
assert getname(1) is None
def test_any_none():
assert com.any_none(1, 2, 3, None)
assert not com.any_none(1, 2, 3, 4)
def test_all_not_none():
assert com.all_not_none(1, 2, 3, 4)
assert not com.all_not_none(1, 2, 3, None)
assert not com.all_not_none(None, None, None, None)
def test_random_state():
# Check with seed
state = com.random_state(5)
assert state.uniform() == np.random.RandomState(5).uniform()
# Check with random state object
state2 = np.random.RandomState(10)
assert com.random_state(state2).uniform() == np.random.RandomState(10).uniform()
# check with no arg random state
assert com.random_state() is np.random
# check array-like
# GH32503
state_arr_like = np.random.default_rng(None).integers(
0, 2**31, size=624, dtype="uint32"
)
assert (
com.random_state(state_arr_like).uniform()
== np.random.RandomState(state_arr_like).uniform()
)
# Check BitGenerators
# GH32503
assert (
com.random_state(np.random.MT19937(3)).uniform()
== np.random.RandomState(np.random.MT19937(3)).uniform()
)
assert (
com.random_state(np.random.PCG64(11)).uniform()
== np.random.RandomState(np.random.PCG64(11)).uniform()
)
# Error for floats or strings
msg = (
"random_state must be an integer, array-like, a BitGenerator, Generator, "
"a numpy RandomState, or None"
)
with pytest.raises(ValueError, match=msg):
com.random_state("test")
with pytest.raises(ValueError, match=msg):
com.random_state(5.5)
@pytest.mark.parametrize(
"left, right, expected",
[
(Series([1], name="x"), Series([2], name="x"), "x"),
(Series([1], name="x"), Series([2], name="y"), None),
(Series([1]), Series([2], name="x"), None),
(Series([1], name="x"), Series([2]), None),
(Series([1], name="x"), [2], "x"),
([1], Series([2], name="y"), "y"),
# matching NAs
(Series([1], name=np.nan), pd.Index([], name=np.nan), np.nan),
(Series([1], name=np.nan), pd.Index([], name=pd.NaT), None),
(Series([1], name=pd.NA), pd.Index([], name=pd.NA), pd.NA),
# tuple name GH#39757
(
Series([1], name=np.int64(1)),
pd.Index([], name=(np.int64(1), np.int64(2))),
None,
),
(
Series([1], name=(np.int64(1), np.int64(2))),
pd.Index([], name=(np.int64(1), np.int64(2))),
(np.int64(1), np.int64(2)),
),
pytest.param(
Series([1], name=(np.float64("nan"), np.int64(2))),
pd.Index([], name=(np.float64("nan"), np.int64(2))),
(np.float64("nan"), np.int64(2)),
marks=pytest.mark.xfail(
reason="Not checking for matching NAs inside tuples."
),
),
],
)
def test_maybe_match_name(left, right, expected):
res = ops.common._maybe_match_name(left, right)
assert res is expected or res == expected
def test_standardize_mapping():
# No uninitialized defaultdicts
msg = r"to_dict\(\) only accepts initialized defaultdicts"
with pytest.raises(TypeError, match=msg):
com.standardize_mapping(collections.defaultdict)
# No non-mapping subtypes, instance
msg = "unsupported type: <class 'list'>"
with pytest.raises(TypeError, match=msg):
com.standardize_mapping([])
# No non-mapping subtypes, class
with pytest.raises(TypeError, match=msg):
com.standardize_mapping(list)
fill = {"bad": "data"}
assert com.standardize_mapping(fill) == dict
# Convert instance to type
assert com.standardize_mapping({}) == dict
dd = collections.defaultdict(list)
assert isinstance(com.standardize_mapping(dd), partial)
def test_git_version():
# GH 21295
git_version = pd.__git_version__
assert len(git_version) == 40
assert all(c in string.hexdigits for c in git_version)
def test_version_tag():
version = Version(pd.__version__)
try:
version > Version("0.0.1")
except TypeError:
raise ValueError(
"No git tags exist, please sync tags between upstream and your repo"
)
@pytest.mark.parametrize(
"obj", [(obj,) for obj in pd.__dict__.values() if callable(obj)]
)
def test_serializable(obj):
# GH 35611
unpickled = tm.round_trip_pickle(obj)
assert type(obj) == type(unpickled)
class TestIsBoolIndexer:
def test_non_bool_array_with_na(self):
# in particular, this should not raise
arr = np.array(["A", "B", np.nan], dtype=object)
assert not com.is_bool_indexer(arr)
def test_list_subclass(self):
# GH#42433
class MyList(list):
pass
val = MyList(["a"])
assert not com.is_bool_indexer(val)
val = MyList([True])
assert com.is_bool_indexer(val)
def test_frozenlist(self):
# GH#42461
data = {"col1": [1, 2], "col2": [3, 4]}
df = pd.DataFrame(data=data)
frozen = df.index.names[1:]
assert not com.is_bool_indexer(frozen)
result = df[frozen]
expected = df[[]]
tm.assert_frame_equal(result, expected)
@pytest.mark.parametrize("with_exception", [True, False])
def test_temp_setattr(with_exception):
# GH#45954
ser = Series(dtype=object)
ser.name = "first"
# Raise a ValueError in either case to satisfy pytest.raises
match = "Inside exception raised" if with_exception else "Outside exception raised"
with pytest.raises(ValueError, match=match):
with com.temp_setattr(ser, "name", "second"):
assert ser.name == "second"
if with_exception:
raise ValueError("Inside exception raised")
raise ValueError("Outside exception raised")
assert ser.name == "first"
@pytest.mark.single_cpu
def test_str_size():
# GH#21758
a = "a"
expected = sys.getsizeof(a)
pyexe = sys.executable.replace("\\", "/")
call = [
pyexe,
"-c",
"a='a';import sys;sys.getsizeof(a);import pandas;print(sys.getsizeof(a));",
]
result = subprocess.check_output(call).decode()[-4:-1].strip("\n")
assert int(result) == int(expected)
@pytest.mark.single_cpu
def test_bz2_missing_import():
# Check whether bz2 missing import is handled correctly (issue #53857)
code = """
import sys
sys.modules['bz2'] = None
import pytest
import pandas as pd
from pandas.compat import get_bz2_file
msg = 'bz2 module not available.'
with pytest.raises(RuntimeError, match=msg):
get_bz2_file()
"""
code = textwrap.dedent(code)
call = [sys.executable, "-c", code]
subprocess.check_output(call)
@td.skip_if_installed("pyarrow")
@pytest.mark.parametrize("module", ["pandas", "pandas.arrays"])
def test_pyarrow_missing_warn(module):
# GH56896
response = subprocess.run(
[sys.executable, "-c", f"import {module}"],
capture_output=True,
check=True,
)
msg = """
Pyarrow will become a required dependency of pandas in the next major release of pandas (pandas 3.0),
(to allow more performant data types, such as the Arrow string type, and better interoperability with other libraries)
but was not found to be installed on your system.
If this would cause problems for you,
please provide us feedback at https://github.com/pandas-dev/pandas/issues/54466
""" # noqa: E501
stderr_msg = response.stderr.decode("utf-8")
# Split by \n to avoid \r\n vs \n differences on Windows/Unix
# https://stackoverflow.com/questions/11989501/replacing-r-n-with-n
stderr_msg = "\n".join(stderr_msg.splitlines())
assert msg in stderr_msg