forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_get_dummies.py
124 lines (107 loc) · 3.08 KB
/
test_get_dummies.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
import numpy as np
import pytest
import pandas.util._test_decorators as td
from pandas import (
DataFrame,
Index,
MultiIndex,
Series,
_testing as tm,
)
try:
import pyarrow as pa
except ImportError:
pa = None
def test_get_dummies(any_string_dtype):
s = Series(["a|b", "a|c", np.nan], dtype=any_string_dtype)
result = s.str.get_dummies("|")
expected = DataFrame([[1, 1, 0], [1, 0, 1], [0, 0, 0]], columns=list("abc"))
tm.assert_frame_equal(result, expected)
s = Series(["a;b", "a", 7], dtype=any_string_dtype)
result = s.str.get_dummies(";")
expected = DataFrame([[0, 1, 1], [0, 1, 0], [1, 0, 0]], columns=list("7ab"))
tm.assert_frame_equal(result, expected)
def test_get_dummies_index():
# GH9980, GH8028
idx = Index(["a|b", "a|c", "b|c"])
result = idx.str.get_dummies("|")
expected = MultiIndex.from_tuples(
[(1, 1, 0), (1, 0, 1), (0, 1, 1)], names=("a", "b", "c")
)
tm.assert_index_equal(result, expected)
# GH#47872
@pytest.mark.parametrize(
"dtype",
[
np.uint8,
np.int16,
np.uint16,
np.int32,
np.uint32,
np.int64,
np.uint64,
bool,
"Int8",
"Int16",
"Int32",
"Int64",
"boolean",
],
)
def test_get_dummies_with_dtype(any_string_dtype, dtype):
s = Series(["a|b", "a|c", np.nan], dtype=any_string_dtype)
result = s.str.get_dummies("|", dtype=dtype)
expected = DataFrame(
[[1, 1, 0], [1, 0, 1], [0, 0, 0]], columns=list("abc"), dtype=dtype
)
tm.assert_frame_equal(result, expected)
# GH#47872
@td.skip_if_no("pyarrow")
@pytest.mark.parametrize(
"dtype",
[
"int8[pyarrow]",
"uint8[pyarrow]",
"int16[pyarrow]",
"uint16[pyarrow]",
"int32[pyarrow]",
"uint32[pyarrow]",
"int64[pyarrow]",
"uint64[pyarrow]",
"bool[pyarrow]",
],
)
def test_get_dummies_with_pyarrow_dtype(any_string_dtype, dtype):
s = Series(["a|b", "a|c", np.nan], dtype=any_string_dtype)
result = s.str.get_dummies("|", dtype=dtype)
expected = DataFrame(
[[1, 1, 0], [1, 0, 1], [0, 0, 0]],
columns=list("abc"),
dtype=dtype,
)
tm.assert_frame_equal(result, expected)
# GH#47872
def test_get_dummies_with_str_dtype(any_string_dtype):
s = Series(["a|b", "a|c", np.nan], dtype=any_string_dtype)
result = s.str.get_dummies("|", dtype=str)
expected = DataFrame(
[["T", "T", "F"], ["T", "F", "T"], ["F", "F", "F"]],
columns=list("abc"),
dtype=str,
)
tm.assert_frame_equal(result, expected)
# GH#47872
@td.skip_if_no("pyarrow")
def test_get_dummies_with_pa_str_dtype(any_string_dtype):
s = Series(["a|b", "a|c", np.nan], dtype=any_string_dtype)
result = s.str.get_dummies("|", dtype="str[pyarrow]")
expected = DataFrame(
[
["true", "true", "false"],
["true", "false", "true"],
["false", "false", "false"],
],
columns=list("abc"),
dtype="str[pyarrow]",
)
tm.assert_frame_equal(result, expected)