Skip to content

Commit b17cedd

Browse files
authored
TST: Split / parameterize reshaping tests (#45278)
1 parent 1810631 commit b17cedd

File tree

4 files changed

+96
-89
lines changed

4 files changed

+96
-89
lines changed

pandas/tests/reshape/test_melt.py

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ def test_value_vars(self):
7575
)
7676
tm.assert_frame_equal(result4, expected4)
7777

78-
def test_value_vars_types(self):
78+
@pytest.mark.parametrize("type_", (tuple, list, np.array))
79+
def test_value_vars_types(self, type_):
7980
# GH 15348
8081
expected = DataFrame(
8182
{
@@ -86,10 +87,8 @@ def test_value_vars_types(self):
8687
},
8788
columns=["id1", "id2", "variable", "value"],
8889
)
89-
90-
for type_ in (tuple, list, np.array):
91-
result = self.df.melt(id_vars=["id1", "id2"], value_vars=type_(("A", "B")))
92-
tm.assert_frame_equal(result, expected)
90+
result = self.df.melt(id_vars=["id1", "id2"], value_vars=type_(("A", "B")))
91+
tm.assert_frame_equal(result, expected)
9392

9493
def test_vars_work_with_multiindex(self):
9594
expected = DataFrame(
@@ -140,23 +139,21 @@ def test_single_vars_work_with_multiindex(
140139
result = self.df1.melt(id_vars, value_vars, col_level=col_level)
141140
tm.assert_frame_equal(result, expected)
142141

143-
def test_tuple_vars_fail_with_multiindex(self):
142+
@pytest.mark.parametrize(
143+
"id_vars, value_vars",
144+
[
145+
[("A", "a"), [("B", "b")]],
146+
[[("A", "a")], ("B", "b")],
147+
[("A", "a"), ("B", "b")],
148+
],
149+
)
150+
def test_tuple_vars_fail_with_multiindex(self, id_vars, value_vars):
144151
# melt should fail with an informative error message if
145152
# the columns have a MultiIndex and a tuple is passed
146153
# for id_vars or value_vars.
147-
tuple_a = ("A", "a")
148-
list_a = [tuple_a]
149-
tuple_b = ("B", "b")
150-
list_b = [tuple_b]
151-
152154
msg = r"(id|value)_vars must be a list of tuples when columns are a MultiIndex"
153-
for id_vars, value_vars in (
154-
(tuple_a, list_b),
155-
(list_a, tuple_b),
156-
(tuple_a, tuple_b),
157-
):
158-
with pytest.raises(ValueError, match=msg):
159-
self.df1.melt(id_vars=id_vars, value_vars=value_vars)
155+
with pytest.raises(ValueError, match=msg):
156+
self.df1.melt(id_vars=id_vars, value_vars=value_vars)
160157

161158
def test_custom_var_name(self):
162159
result5 = self.df.melt(var_name=self.var_name)
@@ -261,11 +258,10 @@ def test_custom_var_and_value_name(self):
261258
result20 = df20.melt()
262259
assert result20.columns.tolist() == ["foo", "value"]
263260

264-
def test_col_level(self):
265-
res1 = self.df1.melt(col_level=0)
266-
res2 = self.df1.melt(col_level="CAP")
267-
assert res1.columns.tolist() == ["CAP", "value"]
268-
assert res2.columns.tolist() == ["CAP", "value"]
261+
@pytest.mark.parametrize("col_level", [0, "CAP"])
262+
def test_col_level(self, col_level):
263+
res = self.df1.melt(col_level=col_level)
264+
assert res.columns.tolist() == ["CAP", "value"]
269265

270266
def test_multiindex(self):
271267
res = self.df1.melt()
@@ -633,7 +629,7 @@ def test_pairs(self):
633629
tm.assert_frame_equal(result, exp)
634630

635631
with tm.assert_produces_warning(FutureWarning):
636-
result = lreshape(df, spec, dropna=False, label="foo")
632+
lreshape(df, spec, dropna=False, label="foo")
637633

638634
spec = {
639635
"visitdt": [f"visitdt{i:d}" for i in range(1, 3)],

pandas/tests/reshape/test_pivot.py

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ def test_pivot_with_non_observable_dropna(self, dropna):
260260

261261
tm.assert_frame_equal(result, expected)
262262

263+
def test_pivot_with_non_observable_dropna_multi_cat(self, dropna):
263264
# gh-21378
264265
df = DataFrame(
265266
{
@@ -493,6 +494,8 @@ def test_pivot_index_with_nan(self, method):
493494
tm.assert_frame_equal(result, expected)
494495
tm.assert_frame_equal(df.pivot("b", "a", "c"), expected.T)
495496

497+
@pytest.mark.parametrize("method", [True, False])
498+
def test_pivot_index_with_nan_dates(self, method):
496499
# GH9491
497500
df = DataFrame(
498501
{
@@ -501,8 +504,8 @@ def test_pivot_index_with_nan(self, method):
501504
}
502505
)
503506
df["b"] = df["a"] - pd.Timestamp("2014-02-02")
504-
df.loc[1, "a"] = df.loc[3, "a"] = nan
505-
df.loc[1, "b"] = df.loc[4, "b"] = nan
507+
df.loc[1, "a"] = df.loc[3, "a"] = np.nan
508+
df.loc[1, "b"] = df.loc[4, "b"] = np.nan
506509

507510
if method:
508511
pv = df.pivot("a", "b", "c")
@@ -851,33 +854,31 @@ def test_pivot_with_tuple_of_values(self, method):
851854
else:
852855
pd.pivot(df, index="zoo", columns="foo", values=("bar", "baz"))
853856

854-
def test_margins(self):
855-
def _check_output(
856-
result, values_col, index=["A", "B"], columns=["C"], margins_col="All"
857-
):
858-
col_margins = result.loc[result.index[:-1], margins_col]
859-
expected_col_margins = self.data.groupby(index)[values_col].mean()
860-
tm.assert_series_equal(col_margins, expected_col_margins, check_names=False)
861-
assert col_margins.name == margins_col
862-
863-
result = result.sort_index()
864-
index_margins = result.loc[(margins_col, "")].iloc[:-1]
865-
866-
expected_ix_margins = self.data.groupby(columns)[values_col].mean()
867-
tm.assert_series_equal(
868-
index_margins, expected_ix_margins, check_names=False
869-
)
870-
assert index_margins.name == (margins_col, "")
857+
def _check_output(
858+
self, result, values_col, index=["A", "B"], columns=["C"], margins_col="All"
859+
):
860+
col_margins = result.loc[result.index[:-1], margins_col]
861+
expected_col_margins = self.data.groupby(index)[values_col].mean()
862+
tm.assert_series_equal(col_margins, expected_col_margins, check_names=False)
863+
assert col_margins.name == margins_col
871864

872-
grand_total_margins = result.loc[(margins_col, ""), margins_col]
873-
expected_total_margins = self.data[values_col].mean()
874-
assert grand_total_margins == expected_total_margins
865+
result = result.sort_index()
866+
index_margins = result.loc[(margins_col, "")].iloc[:-1]
875867

868+
expected_ix_margins = self.data.groupby(columns)[values_col].mean()
869+
tm.assert_series_equal(index_margins, expected_ix_margins, check_names=False)
870+
assert index_margins.name == (margins_col, "")
871+
872+
grand_total_margins = result.loc[(margins_col, ""), margins_col]
873+
expected_total_margins = self.data[values_col].mean()
874+
assert grand_total_margins == expected_total_margins
875+
876+
def test_margins(self):
876877
# column specified
877878
result = self.data.pivot_table(
878879
values="D", index=["A", "B"], columns="C", margins=True, aggfunc=np.mean
879880
)
880-
_check_output(result, "D")
881+
self._check_output(result, "D")
881882

882883
# Set a different margins_name (not 'All')
883884
result = self.data.pivot_table(
@@ -888,15 +889,16 @@ def _check_output(
888889
aggfunc=np.mean,
889890
margins_name="Totals",
890891
)
891-
_check_output(result, "D", margins_col="Totals")
892+
self._check_output(result, "D", margins_col="Totals")
892893

893894
# no column specified
894895
table = self.data.pivot_table(
895896
index=["A", "B"], columns="C", margins=True, aggfunc=np.mean
896897
)
897898
for value_col in table.columns.levels[0]:
898-
_check_output(table[value_col], value_col)
899+
self._check_output(table[value_col], value_col)
899900

901+
def test_no_col(self):
900902
# no col
901903

902904
# to help with a buglet
@@ -1353,6 +1355,7 @@ def test_pivot_timegrouper(self, using_array_manager):
13531355
aggfunc=np.sum,
13541356
)
13551357

1358+
def test_pivot_timegrouper_double(self):
13561359
# double grouper
13571360
df = DataFrame(
13581361
{
@@ -1633,7 +1636,8 @@ def test_pivot_dtaccessor(self):
16331636
)
16341637
tm.assert_frame_equal(result, expected)
16351638

1636-
def test_daily(self):
1639+
@pytest.mark.parametrize("i", range(1, 367))
1640+
def test_daily(self, i):
16371641
rng = date_range("1/1/2000", "12/31/2004", freq="D")
16381642
ts = Series(np.random.randn(len(rng)), index=rng)
16391643

@@ -1644,28 +1648,27 @@ def test_daily(self):
16441648

16451649
doy = np.asarray(ts.index.dayofyear)
16461650

1647-
for i in range(1, 367):
1648-
subset = ts[doy == i]
1649-
subset.index = subset.index.year
1651+
subset = ts[doy == i]
1652+
subset.index = subset.index.year
16501653

1651-
result = annual[i].dropna()
1652-
tm.assert_series_equal(result, subset, check_names=False)
1653-
assert result.name == i
1654+
result = annual[i].dropna()
1655+
tm.assert_series_equal(result, subset, check_names=False)
1656+
assert result.name == i
16541657

1655-
def test_monthly(self):
1658+
@pytest.mark.parametrize("i", range(1, 13))
1659+
def test_monthly(self, i):
16561660
rng = date_range("1/1/2000", "12/31/2004", freq="M")
16571661
ts = Series(np.random.randn(len(rng)), index=rng)
16581662

16591663
annual = pivot_table(DataFrame(ts), index=ts.index.year, columns=ts.index.month)
16601664
annual.columns = annual.columns.droplevel(0)
16611665

16621666
month = ts.index.month
1663-
for i in range(1, 13):
1664-
subset = ts[month == i]
1665-
subset.index = subset.index.year
1666-
result = annual[i].dropna()
1667-
tm.assert_series_equal(result, subset, check_names=False)
1668-
assert result.name == i
1667+
subset = ts[month == i]
1668+
subset.index = subset.index.year
1669+
result = annual[i].dropna()
1670+
tm.assert_series_equal(result, subset, check_names=False)
1671+
assert result.name == i
16691672

16701673
def test_pivot_table_with_iterator_values(self):
16711674
# GH 12017

pandas/tests/reshape/test_union_categoricals.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414

1515
class TestUnionCategoricals:
16-
def test_union_categorical(self):
17-
# GH 13361
18-
data = [
16+
@pytest.mark.parametrize(
17+
"a, b, combined",
18+
[
1919
(list("abc"), list("abd"), list("abcabd")),
2020
([0, 1, 2], [2, 3, 4], [0, 1, 2, 2, 3, 4]),
2121
([0, 1.2, 2], [2, 3.4, 4], [0, 1.2, 2, 2, 3.4, 4]),
@@ -39,14 +39,16 @@ def test_union_categorical(self):
3939
pd.period_range("2014-01-06", "2014-01-07"),
4040
pd.period_range("2014-01-01", "2014-01-07"),
4141
),
42-
]
43-
44-
for a, b, combined in data:
45-
for box in [Categorical, CategoricalIndex, Series]:
46-
result = union_categoricals([box(Categorical(a)), box(Categorical(b))])
47-
expected = Categorical(combined)
48-
tm.assert_categorical_equal(result, expected)
42+
],
43+
)
44+
@pytest.mark.parametrize("box", [Categorical, CategoricalIndex, Series])
45+
def test_union_categorical(self, a, b, combined, box):
46+
# GH 13361
47+
result = union_categoricals([box(Categorical(a)), box(Categorical(b))])
48+
expected = Categorical(combined)
49+
tm.assert_categorical_equal(result, expected)
4950

51+
def test_union_categorical_ordered_appearance(self):
5052
# new categories ordered by appearance
5153
s = Categorical(["x", "y", "z"])
5254
s2 = Categorical(["a", "b", "c"])
@@ -56,19 +58,22 @@ def test_union_categorical(self):
5658
)
5759
tm.assert_categorical_equal(result, expected)
5860

61+
def test_union_categorical_ordered_true(self):
5962
s = Categorical([0, 1.2, 2], ordered=True)
6063
s2 = Categorical([0, 1.2, 2], ordered=True)
6164
result = union_categoricals([s, s2])
6265
expected = Categorical([0, 1.2, 2, 0, 1.2, 2], ordered=True)
6366
tm.assert_categorical_equal(result, expected)
6467

68+
def test_union_categorical_match_types(self):
6569
# must exactly match types
6670
s = Categorical([0, 1.2, 2])
6771
s2 = Categorical([2, 3, 4])
6872
msg = "dtype of categories must be the same"
6973
with pytest.raises(TypeError, match=msg):
7074
union_categoricals([s, s2])
7175

76+
def test_union_categorical_empty(self):
7277
msg = "No Categoricals to union"
7378
with pytest.raises(ValueError, match=msg):
7479
union_categoricals([])
@@ -117,14 +122,11 @@ def test_union_categoricals_nan(self):
117122
exp = Categorical([np.nan, np.nan, np.nan, np.nan])
118123
tm.assert_categorical_equal(res, exp)
119124

120-
def test_union_categoricals_empty(self):
125+
@pytest.mark.parametrize("val", [[], ["1"]])
126+
def test_union_categoricals_empty(self, val):
121127
# GH 13759
122-
res = union_categoricals([Categorical([]), Categorical([])])
123-
exp = Categorical([])
124-
tm.assert_categorical_equal(res, exp)
125-
126-
res = union_categoricals([Categorical([]), Categorical(["1"])])
127-
exp = Categorical(["1"])
128+
res = union_categoricals([Categorical([]), Categorical(val)])
129+
exp = Categorical(val)
128130
tm.assert_categorical_equal(res, exp)
129131

130132
def test_union_categorical_same_category(self):
@@ -135,6 +137,7 @@ def test_union_categorical_same_category(self):
135137
exp = Categorical([1, 2, 3, 4, 3, 2, 1, np.nan], categories=[1, 2, 3, 4])
136138
tm.assert_categorical_equal(res, exp)
137139

140+
def test_union_categorical_same_category_str(self):
138141
c1 = Categorical(["z", "z", "z"], categories=["x", "y", "z"])
139142
c2 = Categorical(["x", "x", "x"], categories=["x", "y", "z"])
140143
res = union_categoricals([c1, c2])
@@ -293,38 +296,44 @@ def test_union_categoricals_sort_false(self):
293296
)
294297
tm.assert_categorical_equal(result, expected)
295298

299+
def test_union_categoricals_sort_false_fastpath(self):
296300
# fastpath
297301
c1 = Categorical(["a", "b"], categories=["b", "a", "c"])
298302
c2 = Categorical(["b", "c"], categories=["b", "a", "c"])
299303
result = union_categoricals([c1, c2], sort_categories=False)
300304
expected = Categorical(["a", "b", "b", "c"], categories=["b", "a", "c"])
301305
tm.assert_categorical_equal(result, expected)
302306

307+
def test_union_categoricals_sort_false_skipresort(self):
303308
# fastpath - skip resort
304309
c1 = Categorical(["a", "b"], categories=["a", "b", "c"])
305310
c2 = Categorical(["b", "c"], categories=["a", "b", "c"])
306311
result = union_categoricals([c1, c2], sort_categories=False)
307312
expected = Categorical(["a", "b", "b", "c"], categories=["a", "b", "c"])
308313
tm.assert_categorical_equal(result, expected)
309314

315+
def test_union_categoricals_sort_false_one_nan(self):
310316
c1 = Categorical(["x", np.nan])
311317
c2 = Categorical([np.nan, "b"])
312318
result = union_categoricals([c1, c2], sort_categories=False)
313319
expected = Categorical(["x", np.nan, np.nan, "b"], categories=["x", "b"])
314320
tm.assert_categorical_equal(result, expected)
315321

322+
def test_union_categoricals_sort_false_only_nan(self):
316323
c1 = Categorical([np.nan])
317324
c2 = Categorical([np.nan])
318325
result = union_categoricals([c1, c2], sort_categories=False)
319326
expected = Categorical([np.nan, np.nan])
320327
tm.assert_categorical_equal(result, expected)
321328

329+
def test_union_categoricals_sort_false_empty(self):
322330
c1 = Categorical([])
323331
c2 = Categorical([])
324332
result = union_categoricals([c1, c2], sort_categories=False)
325333
expected = Categorical([])
326334
tm.assert_categorical_equal(result, expected)
327335

336+
def test_union_categoricals_sort_false_ordered_true(self):
328337
c1 = Categorical(["b", "a"], categories=["b", "a", "c"], ordered=True)
329338
c2 = Categorical(["a", "c"], categories=["b", "a", "c"], ordered=True)
330339
result = union_categoricals([c1, c2], sort_categories=False)

pandas/tests/reshape/test_util.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,16 @@ def test_tzaware_retained_categorical(self):
4444
expected = x.repeat(2)
4545
tm.assert_index_equal(result1, expected)
4646

47-
def test_empty(self):
47+
@pytest.mark.parametrize("x, y", [[[], []], [[0, 1], []], [[], ["a", "b", "c"]]])
48+
def test_empty(self, x, y):
4849
# product of empty factors
49-
X = [[], [0, 1], []]
50-
Y = [[], [], ["a", "b", "c"]]
51-
for x, y in zip(X, Y):
52-
expected1 = np.array([], dtype=np.asarray(x).dtype)
53-
expected2 = np.array([], dtype=np.asarray(y).dtype)
54-
result1, result2 = cartesian_product([x, y])
55-
tm.assert_numpy_array_equal(result1, expected1)
56-
tm.assert_numpy_array_equal(result2, expected2)
50+
expected1 = np.array([], dtype=np.asarray(x).dtype)
51+
expected2 = np.array([], dtype=np.asarray(y).dtype)
52+
result1, result2 = cartesian_product([x, y])
53+
tm.assert_numpy_array_equal(result1, expected1)
54+
tm.assert_numpy_array_equal(result2, expected2)
5755

56+
def test_empty_input(self):
5857
# empty product (empty input):
5958
result = cartesian_product([])
6059
expected = []

0 commit comments

Comments
 (0)