File size: 2,779 Bytes
7885a28 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
import numpy as np
import pytest
from pandas.core.apply import (
_make_unique_kwarg_list,
maybe_mangle_lambdas,
)
def test_maybe_mangle_lambdas_passthrough():
assert maybe_mangle_lambdas("mean") == "mean"
assert maybe_mangle_lambdas(lambda x: x).__name__ == "<lambda>"
# don't mangel single lambda.
assert maybe_mangle_lambdas([lambda x: x])[0].__name__ == "<lambda>"
def test_maybe_mangle_lambdas_listlike():
aggfuncs = [lambda x: 1, lambda x: 2]
result = maybe_mangle_lambdas(aggfuncs)
assert result[0].__name__ == "<lambda_0>"
assert result[1].__name__ == "<lambda_1>"
assert aggfuncs[0](None) == result[0](None)
assert aggfuncs[1](None) == result[1](None)
def test_maybe_mangle_lambdas():
func = {"A": [lambda x: 0, lambda x: 1]}
result = maybe_mangle_lambdas(func)
assert result["A"][0].__name__ == "<lambda_0>"
assert result["A"][1].__name__ == "<lambda_1>"
def test_maybe_mangle_lambdas_args():
func = {"A": [lambda x, a, b=1: (0, a, b), lambda x: 1]}
result = maybe_mangle_lambdas(func)
assert result["A"][0].__name__ == "<lambda_0>"
assert result["A"][1].__name__ == "<lambda_1>"
assert func["A"][0](0, 1) == (0, 1, 1)
assert func["A"][0](0, 1, 2) == (0, 1, 2)
assert func["A"][0](0, 2, b=3) == (0, 2, 3)
def test_maybe_mangle_lambdas_named():
func = {"C": np.mean, "D": {"foo": np.mean, "bar": np.mean}}
result = maybe_mangle_lambdas(func)
assert result == func
@pytest.mark.parametrize(
"order, expected_reorder",
[
(
[
("height", "<lambda>"),
("height", "max"),
("weight", "max"),
("height", "<lambda>"),
("weight", "<lambda>"),
],
[
("height", "<lambda>_0"),
("height", "max"),
("weight", "max"),
("height", "<lambda>_1"),
("weight", "<lambda>"),
],
),
(
[
("col2", "min"),
("col1", "<lambda>"),
("col1", "<lambda>"),
("col1", "<lambda>"),
],
[
("col2", "min"),
("col1", "<lambda>_0"),
("col1", "<lambda>_1"),
("col1", "<lambda>_2"),
],
),
(
[("col", "<lambda>"), ("col", "<lambda>"), ("col", "<lambda>")],
[("col", "<lambda>_0"), ("col", "<lambda>_1"), ("col", "<lambda>_2")],
),
],
)
def test_make_unique(order, expected_reorder):
# GH 27519, test if make_unique function reorders correctly
result = _make_unique_kwarg_list(order)
assert result == expected_reorder
|