Skip to content

Commit fc61aa9

Browse files
authored
[TST]: Groupy raised ValueError for ffill with duplicate column names (#36326)
1 parent 4c4db19 commit fc61aa9

File tree

3 files changed

+84
-65
lines changed

3 files changed

+84
-65
lines changed

pandas/tests/groupby/test_function.py

-45
Original file line numberDiff line numberDiff line change
@@ -495,51 +495,6 @@ def test_idxmin_idxmax_returns_int_types(func, values):
495495
tm.assert_frame_equal(result, expected)
496496

497497

498-
def test_fill_consistency():
499-
500-
# GH9221
501-
# pass thru keyword arguments to the generated wrapper
502-
# are set if the passed kw is None (only)
503-
df = DataFrame(
504-
index=pd.MultiIndex.from_product(
505-
[["value1", "value2"], date_range("2014-01-01", "2014-01-06")]
506-
),
507-
columns=Index(["1", "2"], name="id"),
508-
)
509-
df["1"] = [
510-
np.nan,
511-
1,
512-
np.nan,
513-
np.nan,
514-
11,
515-
np.nan,
516-
np.nan,
517-
2,
518-
np.nan,
519-
np.nan,
520-
22,
521-
np.nan,
522-
]
523-
df["2"] = [
524-
np.nan,
525-
3,
526-
np.nan,
527-
np.nan,
528-
33,
529-
np.nan,
530-
np.nan,
531-
4,
532-
np.nan,
533-
np.nan,
534-
44,
535-
np.nan,
536-
]
537-
538-
expected = df.groupby(level=0, axis=0).fillna(method="ffill")
539-
result = df.T.groupby(level=0, axis=1).fillna(method="ffill").T
540-
tm.assert_frame_equal(result, expected)
541-
542-
543498
def test_groupby_cumprod():
544499
# GH 4095
545500
df = pd.DataFrame({"key": ["b"] * 10, "value": 2})

pandas/tests/groupby/test_groupby.py

-20
Original file line numberDiff line numberDiff line change
@@ -1961,13 +1961,6 @@ def test_shift_bfill_ffill_tz(tz_naive_fixture, op, expected):
19611961
tm.assert_frame_equal(result, expected)
19621962

19631963

1964-
def test_ffill_missing_arguments():
1965-
# GH 14955
1966-
df = pd.DataFrame({"a": [1, 2], "b": [1, 1]})
1967-
with pytest.raises(ValueError, match="Must specify a fill"):
1968-
df.groupby("b").fillna()
1969-
1970-
19711964
def test_groupby_only_none_group():
19721965
# see GH21624
19731966
# this was crashing with "ValueError: Length of passed values is 1, index implies 0"
@@ -2133,16 +2126,3 @@ def test_groupby_column_index_name_lost(func):
21332126
df_grouped = df.groupby([1])
21342127
result = getattr(df_grouped, func)().columns
21352128
tm.assert_index_equal(result, expected)
2136-
2137-
2138-
@pytest.mark.parametrize("func", ["ffill", "bfill"])
2139-
def test_groupby_column_index_name_lost_fill_funcs(func):
2140-
# GH: 29764 groupby loses index sometimes
2141-
df = pd.DataFrame(
2142-
[[1, 1.0, -1.0], [1, np.nan, np.nan], [1, 2.0, -2.0]],
2143-
columns=pd.Index(["type", "a", "b"], name="idx"),
2144-
)
2145-
df_grouped = df.groupby(["type"])[["a", "b"]]
2146-
result = getattr(df_grouped, func)().columns
2147-
expected = pd.Index(["a", "b"], name="idx")
2148-
tm.assert_index_equal(result, expected)

pandas/tests/groupby/test_missing.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import numpy as np
2+
import pytest
3+
4+
import pandas as pd
5+
from pandas import DataFrame, Index, date_range
6+
import pandas._testing as tm
7+
8+
9+
@pytest.mark.parametrize("func", ["ffill", "bfill"])
10+
def test_groupby_column_index_name_lost_fill_funcs(func):
11+
# GH: 29764 groupby loses index sometimes
12+
df = pd.DataFrame(
13+
[[1, 1.0, -1.0], [1, np.nan, np.nan], [1, 2.0, -2.0]],
14+
columns=pd.Index(["type", "a", "b"], name="idx"),
15+
)
16+
df_grouped = df.groupby(["type"])[["a", "b"]]
17+
result = getattr(df_grouped, func)().columns
18+
expected = pd.Index(["a", "b"], name="idx")
19+
tm.assert_index_equal(result, expected)
20+
21+
22+
@pytest.mark.parametrize("func", ["ffill", "bfill"])
23+
def test_groupby_fill_duplicate_column_names(func):
24+
# GH: 25610 ValueError with duplicate column names
25+
df1 = pd.DataFrame({"field1": [1, 3, 4], "field2": [1, 3, 4]})
26+
df2 = pd.DataFrame({"field1": [1, np.nan, 4]})
27+
df_grouped = pd.concat([df1, df2], axis=1).groupby(by=["field2"])
28+
expected = pd.DataFrame(
29+
[[1, 1.0], [3, np.nan], [4, 4.0]], columns=["field1", "field1"]
30+
)
31+
result = getattr(df_grouped, func)()
32+
tm.assert_frame_equal(result, expected)
33+
34+
35+
def test_ffill_missing_arguments():
36+
# GH 14955
37+
df = pd.DataFrame({"a": [1, 2], "b": [1, 1]})
38+
with pytest.raises(ValueError, match="Must specify a fill"):
39+
df.groupby("b").fillna()
40+
41+
42+
def test_fill_consistency():
43+
44+
# GH9221
45+
# pass thru keyword arguments to the generated wrapper
46+
# are set if the passed kw is None (only)
47+
df = DataFrame(
48+
index=pd.MultiIndex.from_product(
49+
[["value1", "value2"], date_range("2014-01-01", "2014-01-06")]
50+
),
51+
columns=Index(["1", "2"], name="id"),
52+
)
53+
df["1"] = [
54+
np.nan,
55+
1,
56+
np.nan,
57+
np.nan,
58+
11,
59+
np.nan,
60+
np.nan,
61+
2,
62+
np.nan,
63+
np.nan,
64+
22,
65+
np.nan,
66+
]
67+
df["2"] = [
68+
np.nan,
69+
3,
70+
np.nan,
71+
np.nan,
72+
33,
73+
np.nan,
74+
np.nan,
75+
4,
76+
np.nan,
77+
np.nan,
78+
44,
79+
np.nan,
80+
]
81+
82+
expected = df.groupby(level=0, axis=0).fillna(method="ffill")
83+
result = df.T.groupby(level=0, axis=1).fillna(method="ffill").T
84+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)