Skip to content

Commit 0de0eb7

Browse files
author
samuel.oranyeli
committed
get rid of nested dictionary
1 parent 2738809 commit 0de0eb7

File tree

2 files changed

+15
-103
lines changed

2 files changed

+15
-103
lines changed

janitor/functions/summarize.py

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,8 @@ def summarize(
115115
check(
116116
f"func for {col} in argument {num}",
117117
func,
118-
[str, callable, dict],
118+
[str, callable],
119119
)
120-
if isinstance(func, dict):
121-
for _, funcn in func.items():
122-
check(
123-
f"func in nested dictionary for "
124-
f"{col} in argument {num}",
125-
funcn,
126-
[str, callable],
127-
)
128-
129120
else:
130121
if len(arg) < 2:
131122
raise ValueError(
@@ -171,24 +162,12 @@ def summarize(
171162
for arg in args:
172163
if isinstance(arg, dict):
173164
for col, func in arg.items():
174-
if by_is_true:
175-
val = grp[col]
176-
else:
177-
val = df[col]
178-
if isinstance(func, dict):
179-
for key, funcn in func.items():
180-
try:
181-
outcome = val.agg(funcn)
182-
except (ValueError, AttributeError):
183-
outcome = funcn(val)
184-
aggs[key] = outcome
185-
else:
186-
try:
187-
outcome = val.agg(func)
188-
except (ValueError, AttributeError):
189-
outcome = func(val)
190-
aggs[col] = outcome
191-
165+
val = grp if by_is_true else df
166+
try:
167+
outcome = val.agg(func)
168+
except (ValueError, AttributeError):
169+
outcome = func(val)
170+
aggs[col] = outcome
192171
else:
193172
columns, func, names = SD(*arg)
194173
columns = _select_index([columns], df, axis="columns")
@@ -221,10 +200,7 @@ def summarize(
221200
counts = None
222201
func_names = tuple(zip(func_names, func))
223202
for col in columns:
224-
if by_is_true:
225-
val = grp[col]
226-
else:
227-
val = df[col]
203+
val = grp[col] if by_is_true else df[col]
228204
for name, funcn in func_names:
229205
if names:
230206
name = names.format(_col=col, _fn=name)

tests/functions/test_summarize.py

Lines changed: 7 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,9 @@ def test_dict_args_val_error(dataframe):
2626
dataframe.summarize({"a": 1})
2727

2828

29-
@pytest.mark.functions
30-
def test_dict_nested_error(dataframe):
31-
"""
32-
Raise if func in nested dict
33-
is a wrong type
34-
"""
35-
with pytest.raises(
36-
TypeError, match="func in nested dictionary for a in argument 0.+"
37-
):
38-
dataframe.summarize({"a": {"b": 1}})
39-
40-
4129
func = lambda grp: grp.Revenue.sum() / grp.Quantity.sum() # noqa: E731
4230

4331

44-
@pytest.mark.functions
45-
def test_nested_dict_agg_error(dataframe):
46-
"""
47-
Raise if func triggers an attributeerror/valueerror
48-
"""
49-
with pytest.raises(AttributeError):
50-
dataframe.summarize({"a": {"b": func}})
51-
52-
5332
@pytest.mark.functions
5433
def test_dict_agg_error(dataframe):
5534
"""
@@ -115,7 +94,12 @@ def test_tuple_func_seq_error(dataframe):
11594
dataframe.summarize(("a", [np.sum, 1], "name"))
11695

11796

118-
args = [{"a": "sum"}, ("a", "sum"), ("a", np.sum), {"a": np.sum}]
97+
args = [
98+
{"a": lambda df: df.a.sum()},
99+
("a", "sum"),
100+
("a", np.sum),
101+
{"a": lambda f: np.sum(f.a)},
102+
]
119103

120104

121105
@pytest.mark.parametrize("test_input", args)
@@ -127,54 +111,6 @@ def test_args_various(dataframe, test_input):
127111
assert_frame_equal(expected, actual)
128112

129113

130-
args = [
131-
({"a": "sum"}, "decorated-elephant"),
132-
({"a": np.sum}, "decorated-elephant"),
133-
(("a", "sum"), "decorated-elephant"),
134-
]
135-
136-
137-
@pytest.mark.parametrize("test_input,by", args)
138-
@pytest.mark.functions
139-
def test_args_various_grouped(dataframe, test_input, by):
140-
"""Test output for various arguments"""
141-
expected = dataframe.groupby("decorated-elephant").agg({"a": "sum"})
142-
actual = dataframe.summarize(test_input, by=by)
143-
assert_frame_equal(expected, actual)
144-
145-
146-
@pytest.mark.functions
147-
def test_dict_nested(dataframe):
148-
"""Test output for dict"""
149-
expected = (
150-
dataframe.agg({"a": ["sum"]})
151-
.rename(columns={"a": "a_sum"})
152-
.reset_index(drop=True)
153-
)
154-
actual = dataframe.summarize({"a": {"a_sum": "sum"}})
155-
assert_frame_equal(expected, actual)
156-
157-
158-
@pytest.mark.functions
159-
def test_dict_nested_grouped_str(dataframe):
160-
"""Test output for dict on a groupby"""
161-
expected = dataframe.groupby("decorated-elephant").agg(a_sum=("a", "sum"))
162-
actual = dataframe.summarize(
163-
{"a": {"a_sum": "sum"}}, by="decorated-elephant"
164-
)
165-
assert_frame_equal(expected, actual)
166-
167-
168-
@pytest.mark.functions
169-
def test_dict_nested_grouped_callable(dataframe):
170-
"""Test output for dict on a groupby"""
171-
expected = dataframe.groupby("decorated-elephant").agg(a_sum=("a", "sum"))
172-
actual = dataframe.summarize(
173-
{"a": {"a_sum": np.sum}}, by={"by": "decorated-elephant"}
174-
)
175-
assert_frame_equal(expected, actual)
176-
177-
178114
args = [("a", "sum", "{_col}_{_fn}"), ("a", np.sum, "{_col}_{_fn}")]
179115

180116

@@ -231,7 +167,7 @@ def test_tuple_func_list_grouped_dupes(dataframe):
231167
grp = dataframe.groupby("decorated-elephant")
232168
expected = grp.agg(a_sum0=("a", "sum"), a_sum1=("a", "sum"))
233169
actual = dataframe.summarize(
234-
("a", ["sum", np.sum], "{_col}_{_fn}"), by="decorated-elephant"
170+
("a", ["sum", np.sum], "{_col}_{_fn}"), by={"by": "decorated-elephant"}
235171
)
236172
assert_frame_equal(expected, actual)
237173

0 commit comments

Comments
 (0)