Skip to content

Commit 1bcc368

Browse files
authored
REF/TST: method-specific files for combine, update; parametrize (#32228)
1 parent cf993fd commit 1bcc368

File tree

3 files changed

+105
-103
lines changed

3 files changed

+105
-103
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from pandas import Series
2+
import pandas._testing as tm
3+
4+
5+
class TestCombine:
6+
def test_combine_scalar(self):
7+
# GH#21248
8+
# Note - combine() with another Series is tested elsewhere because
9+
# it is used when testing operators
10+
ser = Series([i * 10 for i in range(5)])
11+
result = ser.combine(3, lambda x, y: x + y)
12+
expected = Series([i * 10 + 3 for i in range(5)])
13+
tm.assert_series_equal(result, expected)
14+
15+
result = ser.combine(22, lambda x, y: min(x, y))
16+
expected = Series([min(i * 10, 22) for i in range(5)])
17+
tm.assert_series_equal(result, expected)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import DataFrame, Series
5+
import pandas._testing as tm
6+
7+
8+
class TestUpdate:
9+
def test_update(self):
10+
s = Series([1.5, np.nan, 3.0, 4.0, np.nan])
11+
s2 = Series([np.nan, 3.5, np.nan, 5.0])
12+
s.update(s2)
13+
14+
expected = Series([1.5, 3.5, 3.0, 5.0, np.nan])
15+
tm.assert_series_equal(s, expected)
16+
17+
# GH 3217
18+
df = DataFrame([{"a": 1}, {"a": 3, "b": 2}])
19+
df["c"] = np.nan
20+
21+
df["c"].update(Series(["foo"], index=[0]))
22+
expected = DataFrame(
23+
[[1, np.nan, "foo"], [3, 2.0, np.nan]], columns=["a", "b", "c"]
24+
)
25+
tm.assert_frame_equal(df, expected)
26+
27+
@pytest.mark.parametrize(
28+
"other, dtype, expected",
29+
[
30+
# other is int
31+
([61, 63], "int32", Series([10, 61, 12], dtype="int32")),
32+
([61, 63], "int64", Series([10, 61, 12])),
33+
([61, 63], float, Series([10.0, 61.0, 12.0])),
34+
([61, 63], object, Series([10, 61, 12], dtype=object)),
35+
# other is float, but can be cast to int
36+
([61.0, 63.0], "int32", Series([10, 61, 12], dtype="int32")),
37+
([61.0, 63.0], "int64", Series([10, 61, 12])),
38+
([61.0, 63.0], float, Series([10.0, 61.0, 12.0])),
39+
([61.0, 63.0], object, Series([10, 61.0, 12], dtype=object)),
40+
# others is float, cannot be cast to int
41+
([61.1, 63.1], "int32", Series([10.0, 61.1, 12.0])),
42+
([61.1, 63.1], "int64", Series([10.0, 61.1, 12.0])),
43+
([61.1, 63.1], float, Series([10.0, 61.1, 12.0])),
44+
([61.1, 63.1], object, Series([10, 61.1, 12], dtype=object)),
45+
# other is object, cannot be cast
46+
([(61,), (63,)], "int32", Series([10, (61,), 12])),
47+
([(61,), (63,)], "int64", Series([10, (61,), 12])),
48+
([(61,), (63,)], float, Series([10.0, (61,), 12.0])),
49+
([(61,), (63,)], object, Series([10, (61,), 12])),
50+
],
51+
)
52+
def test_update_dtypes(self, other, dtype, expected):
53+
54+
ser = Series([10, 11, 12], dtype=dtype)
55+
other = Series(other, index=[1, 3])
56+
ser.update(other)
57+
58+
tm.assert_series_equal(ser, expected)

pandas/tests/series/test_combine_concat.py

Lines changed: 30 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -2,84 +2,27 @@
22
import pytest
33

44
import pandas as pd
5-
from pandas import DataFrame, Series
6-
import pandas._testing as tm
5+
from pandas import Series
76

87

98
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-
419
@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]"]
6511
)
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)
6714

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
7117

72-
tm.assert_series_equal(s, expected)
18+
result = pd.concat([Series(dtype=dtype), Series(dtype=dtype)])
19+
assert result.dtype == dtype
7320

7421
def test_concat_empty_series_dtypes_roundtrips(self):
7522

7623
# round-tripping with self & like self
7724
dtypes = map(np.dtype, ["float64", "int8", "uint8", "bool", "m8[ns]", "M8[ns]"])
7825

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-
8326
def int_result_type(dtype, dtype2):
8427
typs = {dtype.kind, dtype2.kind}
8528
if not len(typs - {"i", "u", "b"}) and (
@@ -118,61 +61,45 @@ def get_result_type(dtype, dtype2):
11861
result = pd.concat([Series(dtype=dtype), Series(dtype=dtype2)]).dtype
11962
assert result.kind == expected
12063

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
12283

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):
13285

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-
)
15086
assert (
15187
pd.concat(
15288
[Series(dtype="M8[ns]"), Series(dtype=np.bool_), Series(dtype=np.int64)]
15389
).dtype
15490
== np.object_
15591
)
15692

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):
16294
# GH 18515
16395
assert (
16496
pd.concat(
16597
[Series(np.array([]), dtype="category"), Series(dtype="float64")]
16698
).dtype
16799
== "float64"
168100
)
169-
assert (
170-
pd.concat([Series(dtype="category"), Series(dtype="object")]).dtype
171-
== "object"
172-
)
173101

174-
# sparse
175-
# TODO: move?
102+
def test_concat_empty_series_dtypes_sparse(self):
176103
result = pd.concat(
177104
[
178105
Series(dtype="float64").astype("Sparse"),

0 commit comments

Comments
 (0)