forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_string.py
425 lines (337 loc) · 14.3 KB
/
test_string.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
"""
This file contains a minimal set of tests for compliance with the extension
array interface test suite, and should contain no other tests.
The test suite for the full functionality of the array is located in
`pandas/tests/arrays/`.
The tests in this file are inherited from the BaseExtensionTests, and only
minimal tweaks should be applied to get the tests passing (by overwriting a
parent method).
Additional tests should either be added to one of the BaseExtensionTests
classes (if they are relevant for the extension interface for all dtypes), or
be added to the array-specific tests in `pandas/tests/arrays/`.
"""
import string
import numpy as np
import pytest
from pandas.compat import pa_version_under7p0
from pandas.errors import PerformanceWarning
import pandas as pd
import pandas._testing as tm
from pandas.api.types import is_string_dtype
from pandas.core.arrays import ArrowStringArray
from pandas.core.arrays.string_ import StringDtype
from pandas.tests.extension import base
def split_array(arr):
if arr.dtype.storage != "pyarrow":
pytest.skip("only applicable for pyarrow chunked array n/a")
def _split_array(arr):
import pyarrow as pa
arrow_array = arr._data
split = len(arrow_array) // 2
arrow_array = pa.chunked_array(
[*arrow_array[:split].chunks, *arrow_array[split:].chunks]
)
assert arrow_array.num_chunks == 2
return type(arr)(arrow_array)
return _split_array(arr)
@pytest.fixture(params=[True, False])
def chunked(request):
return request.param
@pytest.fixture
def dtype(string_storage):
return StringDtype(storage=string_storage)
@pytest.fixture
def data(dtype, chunked):
strings = np.random.choice(list(string.ascii_letters), size=100)
while strings[0] == strings[1]:
strings = np.random.choice(list(string.ascii_letters), size=100)
arr = dtype.construct_array_type()._from_sequence(strings)
return split_array(arr) if chunked else arr
@pytest.fixture
def data_missing(dtype, chunked):
"""Length 2 array with [NA, Valid]"""
arr = dtype.construct_array_type()._from_sequence([pd.NA, "A"])
return split_array(arr) if chunked else arr
@pytest.fixture
def data_for_sorting(dtype, chunked):
arr = dtype.construct_array_type()._from_sequence(["B", "C", "A"])
return split_array(arr) if chunked else arr
@pytest.fixture
def data_missing_for_sorting(dtype, chunked):
arr = dtype.construct_array_type()._from_sequence(["B", pd.NA, "A"])
return split_array(arr) if chunked else arr
@pytest.fixture
def na_value():
return pd.NA
@pytest.fixture
def data_for_grouping(dtype, chunked):
arr = dtype.construct_array_type()._from_sequence(
["B", "B", pd.NA, pd.NA, "A", "A", "B", "C"]
)
return split_array(arr) if chunked else arr
class TestDtype(base.BaseDtypeTests):
def test_eq_with_str(self, dtype):
assert dtype == f"string[{dtype.storage}]"
super().test_eq_with_str(dtype)
def test_is_not_string_type(self, dtype):
# Different from BaseDtypeTests.test_is_not_string_type
# because StringDtype is a string type
assert is_string_dtype(dtype)
class TestInterface(base.BaseInterfaceTests):
def test_view(self, data, request):
if data.dtype.storage == "pyarrow":
pytest.skip(reason="2D support not implemented for ArrowStringArray")
super().test_view(data)
class TestConstructors(base.BaseConstructorsTests):
def test_from_dtype(self, data):
# base test uses string representation of dtype
pass
def test_constructor_from_list(self):
# GH 27673
pytest.importorskip("pyarrow", minversion="1.0.0")
result = pd.Series(["E"], dtype=StringDtype(storage="pyarrow"))
assert isinstance(result.dtype, StringDtype)
assert result.dtype.storage == "pyarrow"
class TestReshaping(base.BaseReshapingTests):
def test_transpose(self, data, request):
if data.dtype.storage == "pyarrow":
pytest.skip(reason="2D support not implemented for ArrowStringArray")
super().test_transpose(data)
class TestGetitem(base.BaseGetitemTests):
pass
class TestSetitem(base.BaseSetitemTests):
def test_setitem_preserves_views(self, data, request):
if data.dtype.storage == "pyarrow":
pytest.skip(reason="2D support not implemented for ArrowStringArray")
super().test_setitem_preserves_views(data)
class TestIndex(base.BaseIndexTests):
pass
class TestMissing(base.BaseMissingTests):
def test_dropna_array(self, data_missing):
result = data_missing.dropna()
expected = data_missing[[1]]
self.assert_extension_array_equal(result, expected)
def test_fillna_no_op_returns_copy(self, data):
with tm.maybe_produces_warning(
PerformanceWarning,
pa_version_under7p0 and data.dtype.storage == "pyarrow",
check_stacklevel=False,
):
super().test_fillna_no_op_returns_copy(data)
def test_fillna_series_method(self, data_missing, fillna_method):
with tm.maybe_produces_warning(
PerformanceWarning,
pa_version_under7p0 and data_missing.dtype.storage == "pyarrow",
check_stacklevel=False,
):
super().test_fillna_series_method(data_missing, fillna_method)
class TestNoReduce(base.BaseNoReduceTests):
@pytest.mark.parametrize("skipna", [True, False])
def test_reduce_series_numeric(self, data, all_numeric_reductions, skipna):
op_name = all_numeric_reductions
if op_name in ["min", "max"]:
return None
ser = pd.Series(data)
with pytest.raises(TypeError):
getattr(ser, op_name)(skipna=skipna)
class TestMethods(base.BaseMethodsTests):
def test_argsort(self, data_for_sorting):
with tm.maybe_produces_warning(
PerformanceWarning,
pa_version_under7p0
and getattr(data_for_sorting.dtype, "storage", "") == "pyarrow",
check_stacklevel=False,
):
super().test_argsort(data_for_sorting)
def test_argsort_missing(self, data_missing_for_sorting):
with tm.maybe_produces_warning(
PerformanceWarning,
pa_version_under7p0
and getattr(data_missing_for_sorting.dtype, "storage", "") == "pyarrow",
check_stacklevel=False,
):
super().test_argsort_missing(data_missing_for_sorting)
def test_argmin_argmax(
self, data_for_sorting, data_missing_for_sorting, na_value, request
):
super().test_argmin_argmax(data_for_sorting, data_missing_for_sorting, na_value)
@pytest.mark.parametrize(
"op_name, skipna, expected",
[
("idxmax", True, 0),
("idxmin", True, 2),
("argmax", True, 0),
("argmin", True, 2),
("idxmax", False, np.nan),
("idxmin", False, np.nan),
("argmax", False, -1),
("argmin", False, -1),
],
)
def test_argreduce_series(
self, data_missing_for_sorting, op_name, skipna, expected, request
):
super().test_argreduce_series(
data_missing_for_sorting, op_name, skipna, expected
)
@pytest.mark.parametrize("dropna", [True, False])
def test_value_counts(self, all_data, dropna, request):
all_data = all_data[:10]
if dropna:
other = all_data[~all_data.isna()]
else:
other = all_data
with tm.maybe_produces_warning(
PerformanceWarning,
pa_version_under7p0
and getattr(all_data.dtype, "storage", "") == "pyarrow"
and not (dropna and "data_missing" in request.node.nodeid),
):
result = pd.Series(all_data).value_counts(dropna=dropna).sort_index()
with tm.maybe_produces_warning(
PerformanceWarning,
pa_version_under7p0
and getattr(other.dtype, "storage", "") == "pyarrow"
and not (dropna and "data_missing" in request.node.nodeid),
):
expected = pd.Series(other).value_counts(dropna=dropna).sort_index()
self.assert_series_equal(result, expected)
def test_value_counts_with_normalize(self, data):
data = data[:10].unique()
values = np.array(data[~data.isna()])
ser = pd.Series(data, dtype=data.dtype)
result = ser.value_counts(normalize=True).sort_index()
if not isinstance(data, pd.Categorical):
expected = pd.Series(
[1 / len(values)] * len(values), index=result.index, name="proportion"
)
else:
expected = pd.Series(0.0, index=result.index, name="proportion")
expected[result > 0] = 1 / len(values)
if getattr(data.dtype, "storage", "") == "pyarrow":
expected = expected.astype("double[pyarrow]")
else:
expected = expected.astype("Float64")
self.assert_series_equal(result, expected)
def test_argsort_missing_array(self, data_missing_for_sorting):
with tm.maybe_produces_warning(
PerformanceWarning,
pa_version_under7p0
and getattr(data_missing_for_sorting.dtype, "storage", "") == "pyarrow",
check_stacklevel=False,
):
super().test_argsort_missing(data_missing_for_sorting)
@pytest.mark.parametrize(
"na_position, expected",
[
("last", np.array([2, 0, 1], dtype=np.dtype("intp"))),
("first", np.array([1, 2, 0], dtype=np.dtype("intp"))),
],
)
def test_nargsort(self, data_missing_for_sorting, na_position, expected):
# GH 25439
with tm.maybe_produces_warning(
PerformanceWarning,
pa_version_under7p0
and getattr(data_missing_for_sorting.dtype, "storage", "") == "pyarrow",
check_stacklevel=False,
):
super().test_nargsort(data_missing_for_sorting, na_position, expected)
@pytest.mark.parametrize("ascending", [True, False])
def test_sort_values(self, data_for_sorting, ascending, sort_by_key):
with tm.maybe_produces_warning(
PerformanceWarning,
pa_version_under7p0
and getattr(data_for_sorting.dtype, "storage", "") == "pyarrow",
check_stacklevel=False,
):
super().test_sort_values(data_for_sorting, ascending, sort_by_key)
@pytest.mark.parametrize("ascending", [True, False])
def test_sort_values_missing(
self, data_missing_for_sorting, ascending, sort_by_key
):
with tm.maybe_produces_warning(
PerformanceWarning,
pa_version_under7p0
and getattr(data_missing_for_sorting.dtype, "storage", "") == "pyarrow",
check_stacklevel=False,
):
super().test_sort_values_missing(
data_missing_for_sorting, ascending, sort_by_key
)
@pytest.mark.parametrize("ascending", [True, False])
def test_sort_values_frame(self, data_for_sorting, ascending):
with tm.maybe_produces_warning(
PerformanceWarning,
pa_version_under7p0
and getattr(data_for_sorting.dtype, "storage", "") == "pyarrow",
check_stacklevel=False,
):
super().test_sort_values_frame(data_for_sorting, ascending)
class TestCasting(base.BaseCastingTests):
pass
class TestComparisonOps(base.BaseComparisonOpsTests):
def _compare_other(self, ser, data, op, other):
op_name = f"__{op.__name__}__"
result = getattr(ser, op_name)(other)
expected = getattr(ser.astype(object), op_name)(other).astype("boolean")
self.assert_series_equal(result, expected)
def test_compare_scalar(self, data, comparison_op):
ser = pd.Series(data)
self._compare_other(ser, data, comparison_op, "abc")
class TestParsing(base.BaseParsingTests):
pass
class TestPrinting(base.BasePrintingTests):
pass
class TestGroupBy(base.BaseGroupbyTests):
@pytest.mark.parametrize("as_index", [True, False])
def test_groupby_extension_agg(self, as_index, data_for_grouping):
df = pd.DataFrame({"A": [1, 1, 2, 2, 3, 3, 1, 4], "B": data_for_grouping})
with tm.maybe_produces_warning(
PerformanceWarning,
pa_version_under7p0
and getattr(data_for_grouping.dtype, "storage", "") == "pyarrow",
):
result = df.groupby("B", as_index=as_index).A.mean()
with tm.maybe_produces_warning(
PerformanceWarning,
pa_version_under7p0
and getattr(data_for_grouping.dtype, "storage", "") == "pyarrow",
):
_, uniques = pd.factorize(data_for_grouping, sort=True)
if as_index:
index = pd.Index(uniques, name="B")
expected = pd.Series([3.0, 1.0, 4.0], index=index, name="A")
self.assert_series_equal(result, expected)
else:
expected = pd.DataFrame({"B": uniques, "A": [3.0, 1.0, 4.0]})
self.assert_frame_equal(result, expected)
def test_groupby_extension_transform(self, data_for_grouping):
with tm.maybe_produces_warning(
PerformanceWarning,
pa_version_under7p0
and getattr(data_for_grouping.dtype, "storage", "") == "pyarrow",
check_stacklevel=False,
):
super().test_groupby_extension_transform(data_for_grouping)
@pytest.mark.filterwarnings("ignore:Falling back:pandas.errors.PerformanceWarning")
def test_groupby_extension_apply(self, data_for_grouping, groupby_apply_op):
super().test_groupby_extension_apply(data_for_grouping, groupby_apply_op)
class Test2DCompat(base.Dim2CompatTests):
@pytest.fixture(autouse=True)
def arrow_not_supported(self, data, request):
if isinstance(data, ArrowStringArray):
pytest.skip(reason="2D support not implemented for ArrowStringArray")
def test_searchsorted_with_na_raises(data_for_sorting, as_series):
# GH50447
b, c, a = data_for_sorting
arr = data_for_sorting.take([2, 0, 1]) # to get [a, b, c]
arr[-1] = pd.NA
if as_series:
arr = pd.Series(arr)
msg = (
"searchsorted requires array to be sorted, "
"which is impossible with NAs present."
)
with pytest.raises(ValueError, match=msg):
arr.searchsorted(b)