forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_string_array.py
138 lines (110 loc) · 4.32 KB
/
test_string_array.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
import operator
import numpy as np
import pytest
from pandas._libs import lib
import pandas as pd
from pandas import (
DataFrame,
Series,
_testing as tm,
)
def test_string_array(nullable_string_dtype, any_string_method, request):
method_name, args, kwargs = any_string_method
if method_name == "decode":
pytest.skip("decode requires bytes.")
if nullable_string_dtype == "arrow_string" and method_name in {
"extract",
"extractall",
}:
reason = "extract/extractall does not yet dispatch to array"
mark = pytest.mark.xfail(reason=reason)
request.node.add_marker(mark)
data = ["a", "bb", np.nan, "ccc"]
a = Series(data, dtype=object)
b = Series(data, dtype=nullable_string_dtype)
expected = getattr(a.str, method_name)(*args, **kwargs)
result = getattr(b.str, method_name)(*args, **kwargs)
if isinstance(expected, Series):
if expected.dtype == "object" and lib.is_string_array(
expected.dropna().values,
):
assert result.dtype == nullable_string_dtype
result = result.astype(object)
elif expected.dtype == "object" and lib.is_bool_array(
expected.values, skipna=True
):
assert result.dtype == "boolean"
result = result.astype(object)
elif expected.dtype == "bool":
assert result.dtype == "boolean"
result = result.astype("bool")
elif expected.dtype == "float" and expected.isna().any():
assert result.dtype == "Int64"
result = result.astype("float")
elif isinstance(expected, DataFrame):
columns = expected.select_dtypes(include="object").columns
assert all(result[columns].dtypes == nullable_string_dtype)
result[columns] = result[columns].astype(object)
tm.assert_equal(result, expected)
@pytest.mark.parametrize(
"method,expected",
[
("count", [2, None]),
("find", [0, None]),
("index", [0, None]),
("rindex", [2, None]),
],
)
def test_string_array_numeric_integer_array(nullable_string_dtype, method, expected):
s = Series(["aba", None], dtype=nullable_string_dtype)
result = getattr(s.str, method)("a")
expected = Series(expected, dtype="Int64")
tm.assert_series_equal(result, expected)
@pytest.mark.parametrize(
"method,expected",
[
("isdigit", [False, None, True]),
("isalpha", [True, None, False]),
("isalnum", [True, None, True]),
("isnumeric", [False, None, True]),
],
)
def test_string_array_boolean_array(nullable_string_dtype, method, expected):
s = Series(["a", None, "1"], dtype=nullable_string_dtype)
result = getattr(s.str, method)()
expected = Series(expected, dtype="boolean")
tm.assert_series_equal(result, expected)
def test_string_array_extract(nullable_string_dtype, request):
# https://github.com/pandas-dev/pandas/issues/30969
# Only expand=False & multiple groups was failing
if nullable_string_dtype == "arrow_string":
reason = "extract does not yet dispatch to array"
mark = pytest.mark.xfail(reason=reason)
request.node.add_marker(mark)
a = Series(["a1", "b2", "cc"], dtype=nullable_string_dtype)
b = Series(["a1", "b2", "cc"], dtype="object")
pat = r"(\w)(\d)"
result = a.str.extract(pat, expand=False)
expected = b.str.extract(pat, expand=False)
assert all(result.dtypes == nullable_string_dtype)
result = result.astype(object)
tm.assert_equal(result, expected)
def test_str_get_stringarray_multiple_nans(nullable_string_dtype):
s = Series(pd.array(["a", "ab", pd.NA, "abc"], dtype=nullable_string_dtype))
result = s.str.get(2)
expected = Series(pd.array([pd.NA, pd.NA, pd.NA, "c"], dtype=nullable_string_dtype))
tm.assert_series_equal(result, expected)
@pytest.mark.parametrize(
"input, method",
[
(["a", "b", "c"], operator.methodcaller("capitalize")),
(["a b", "a bc. de"], operator.methodcaller("capitalize")),
],
)
def test_capitalize(input, method, nullable_string_dtype):
a = Series(input, dtype=nullable_string_dtype)
b = Series(input, dtype="object")
result = method(a.str)
expected = method(b.str)
assert result.dtype.name == nullable_string_dtype
tm.assert_series_equal(result.astype(object), expected)