|
2 | 2 | import pytest
|
3 | 3 |
|
4 | 4 | import pandas as pd
|
5 |
| -from pandas import DataFrame, Series |
6 |
| -import pandas._testing as tm |
| 5 | +from pandas import Series |
7 | 6 |
|
8 | 7 |
|
9 | 8 | class TestSeriesCombine:
|
10 |
| - def test_combine_scalar(self): |
11 |
| - # GH 21248 |
12 |
| - # Note - combine() with another Series is tested elsewhere because |
13 |
| - # it is used when testing operators |
14 |
| - s = pd.Series([i * 10 for i in range(5)]) |
15 |
| - result = s.combine(3, lambda x, y: x + y) |
16 |
| - expected = pd.Series([i * 10 + 3 for i in range(5)]) |
17 |
| - tm.assert_series_equal(result, expected) |
18 |
| - |
19 |
| - result = s.combine(22, lambda x, y: min(x, y)) |
20 |
| - expected = pd.Series([min(i * 10, 22) for i in range(5)]) |
21 |
| - tm.assert_series_equal(result, expected) |
22 |
| - |
23 |
| - def test_update(self): |
24 |
| - s = Series([1.5, np.nan, 3.0, 4.0, np.nan]) |
25 |
| - s2 = Series([np.nan, 3.5, np.nan, 5.0]) |
26 |
| - s.update(s2) |
27 |
| - |
28 |
| - expected = Series([1.5, 3.5, 3.0, 5.0, np.nan]) |
29 |
| - tm.assert_series_equal(s, expected) |
30 |
| - |
31 |
| - # GH 3217 |
32 |
| - df = DataFrame([{"a": 1}, {"a": 3, "b": 2}]) |
33 |
| - df["c"] = np.nan |
34 |
| - |
35 |
| - df["c"].update(Series(["foo"], index=[0])) |
36 |
| - expected = DataFrame( |
37 |
| - [[1, np.nan, "foo"], [3, 2.0, np.nan]], columns=["a", "b", "c"] |
38 |
| - ) |
39 |
| - tm.assert_frame_equal(df, expected) |
40 |
| - |
41 | 9 | @pytest.mark.parametrize(
|
42 |
| - "other, dtype, expected", |
43 |
| - [ |
44 |
| - # other is int |
45 |
| - ([61, 63], "int32", pd.Series([10, 61, 12], dtype="int32")), |
46 |
| - ([61, 63], "int64", pd.Series([10, 61, 12])), |
47 |
| - ([61, 63], float, pd.Series([10.0, 61.0, 12.0])), |
48 |
| - ([61, 63], object, pd.Series([10, 61, 12], dtype=object)), |
49 |
| - # other is float, but can be cast to int |
50 |
| - ([61.0, 63.0], "int32", pd.Series([10, 61, 12], dtype="int32")), |
51 |
| - ([61.0, 63.0], "int64", pd.Series([10, 61, 12])), |
52 |
| - ([61.0, 63.0], float, pd.Series([10.0, 61.0, 12.0])), |
53 |
| - ([61.0, 63.0], object, pd.Series([10, 61.0, 12], dtype=object)), |
54 |
| - # others is float, cannot be cast to int |
55 |
| - ([61.1, 63.1], "int32", pd.Series([10.0, 61.1, 12.0])), |
56 |
| - ([61.1, 63.1], "int64", pd.Series([10.0, 61.1, 12.0])), |
57 |
| - ([61.1, 63.1], float, pd.Series([10.0, 61.1, 12.0])), |
58 |
| - ([61.1, 63.1], object, pd.Series([10, 61.1, 12], dtype=object)), |
59 |
| - # other is object, cannot be cast |
60 |
| - ([(61,), (63,)], "int32", pd.Series([10, (61,), 12])), |
61 |
| - ([(61,), (63,)], "int64", pd.Series([10, (61,), 12])), |
62 |
| - ([(61,), (63,)], float, pd.Series([10.0, (61,), 12.0])), |
63 |
| - ([(61,), (63,)], object, pd.Series([10, (61,), 12])), |
64 |
| - ], |
| 10 | + "dtype", ["float64", "int8", "uint8", "bool", "m8[ns]", "M8[ns]"] |
65 | 11 | )
|
66 |
| - def test_update_dtypes(self, other, dtype, expected): |
| 12 | + def test_concat_empty_series_dtypes_match_roundtrips(self, dtype): |
| 13 | + dtype = np.dtype(dtype) |
67 | 14 |
|
68 |
| - s = Series([10, 11, 12], dtype=dtype) |
69 |
| - other = Series(other, index=[1, 3]) |
70 |
| - s.update(other) |
| 15 | + result = pd.concat([Series(dtype=dtype)]) |
| 16 | + assert result.dtype == dtype |
71 | 17 |
|
72 |
| - tm.assert_series_equal(s, expected) |
| 18 | + result = pd.concat([Series(dtype=dtype), Series(dtype=dtype)]) |
| 19 | + assert result.dtype == dtype |
73 | 20 |
|
74 | 21 | def test_concat_empty_series_dtypes_roundtrips(self):
|
75 | 22 |
|
76 | 23 | # round-tripping with self & like self
|
77 | 24 | dtypes = map(np.dtype, ["float64", "int8", "uint8", "bool", "m8[ns]", "M8[ns]"])
|
78 | 25 |
|
79 |
| - for dtype in dtypes: |
80 |
| - assert pd.concat([Series(dtype=dtype)]).dtype == dtype |
81 |
| - assert pd.concat([Series(dtype=dtype), Series(dtype=dtype)]).dtype == dtype |
82 |
| - |
83 | 26 | def int_result_type(dtype, dtype2):
|
84 | 27 | typs = {dtype.kind, dtype2.kind}
|
85 | 28 | if not len(typs - {"i", "u", "b"}) and (
|
@@ -118,61 +61,45 @@ def get_result_type(dtype, dtype2):
|
118 | 61 | result = pd.concat([Series(dtype=dtype), Series(dtype=dtype2)]).dtype
|
119 | 62 | assert result.kind == expected
|
120 | 63 |
|
121 |
| - def test_concat_empty_series_dtypes(self): |
| 64 | + @pytest.mark.parametrize( |
| 65 | + "left,right,expected", |
| 66 | + [ |
| 67 | + # booleans |
| 68 | + (np.bool_, np.int32, np.int32), |
| 69 | + (np.bool_, np.float32, np.object_), |
| 70 | + # datetime-like |
| 71 | + ("m8[ns]", np.bool, np.object_), |
| 72 | + ("m8[ns]", np.int64, np.object_), |
| 73 | + ("M8[ns]", np.bool, np.object_), |
| 74 | + ("M8[ns]", np.int64, np.object_), |
| 75 | + # categorical |
| 76 | + ("category", "category", "category"), |
| 77 | + ("category", "object", "object"), |
| 78 | + ], |
| 79 | + ) |
| 80 | + def test_concat_empty_series_dtypes(self, left, right, expected): |
| 81 | + result = pd.concat([Series(dtype=left), Series(dtype=right)]) |
| 82 | + assert result.dtype == expected |
122 | 83 |
|
123 |
| - # booleans |
124 |
| - assert ( |
125 |
| - pd.concat([Series(dtype=np.bool_), Series(dtype=np.int32)]).dtype |
126 |
| - == np.int32 |
127 |
| - ) |
128 |
| - assert ( |
129 |
| - pd.concat([Series(dtype=np.bool_), Series(dtype=np.float32)]).dtype |
130 |
| - == np.object_ |
131 |
| - ) |
| 84 | + def test_concat_empty_series_dtypes_triple(self): |
132 | 85 |
|
133 |
| - # datetime-like |
134 |
| - assert ( |
135 |
| - pd.concat([Series(dtype="m8[ns]"), Series(dtype=np.bool)]).dtype |
136 |
| - == np.object_ |
137 |
| - ) |
138 |
| - assert ( |
139 |
| - pd.concat([Series(dtype="m8[ns]"), Series(dtype=np.int64)]).dtype |
140 |
| - == np.object_ |
141 |
| - ) |
142 |
| - assert ( |
143 |
| - pd.concat([Series(dtype="M8[ns]"), Series(dtype=np.bool)]).dtype |
144 |
| - == np.object_ |
145 |
| - ) |
146 |
| - assert ( |
147 |
| - pd.concat([Series(dtype="M8[ns]"), Series(dtype=np.int64)]).dtype |
148 |
| - == np.object_ |
149 |
| - ) |
150 | 86 | assert (
|
151 | 87 | pd.concat(
|
152 | 88 | [Series(dtype="M8[ns]"), Series(dtype=np.bool_), Series(dtype=np.int64)]
|
153 | 89 | ).dtype
|
154 | 90 | == np.object_
|
155 | 91 | )
|
156 | 92 |
|
157 |
| - # categorical |
158 |
| - assert ( |
159 |
| - pd.concat([Series(dtype="category"), Series(dtype="category")]).dtype |
160 |
| - == "category" |
161 |
| - ) |
| 93 | + def test_concat_empty_series_dtype_category_with_array(self): |
162 | 94 | # GH 18515
|
163 | 95 | assert (
|
164 | 96 | pd.concat(
|
165 | 97 | [Series(np.array([]), dtype="category"), Series(dtype="float64")]
|
166 | 98 | ).dtype
|
167 | 99 | == "float64"
|
168 | 100 | )
|
169 |
| - assert ( |
170 |
| - pd.concat([Series(dtype="category"), Series(dtype="object")]).dtype |
171 |
| - == "object" |
172 |
| - ) |
173 | 101 |
|
174 |
| - # sparse |
175 |
| - # TODO: move? |
| 102 | + def test_concat_empty_series_dtypes_sparse(self): |
176 | 103 | result = pd.concat(
|
177 | 104 | [
|
178 | 105 | Series(dtype="float64").astype("Sparse"),
|
|
0 commit comments