Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions percentify/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,9 @@ def split(total, weights, decimals: Optional[int] = 2):
shares = w.astype(float) / weight_sum * float(total)
if decimals is not None:
shares = shares.round(decimals)
remainder = round(float(total), decimals) - float(shares.sum())
if remainder:
shares.iloc[-1] = round(shares.iloc[-1] + remainder, decimals)
return shares if is_series else shares.tolist()


Expand Down
4 changes: 3 additions & 1 deletion tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,9 @@ def test_split_list():


def test_split_equal():
assert split(100, [1, 1, 1]) == [pytest.approx(33.33)] * 3
result = split(100, [1, 1, 1])
assert result == [33.33, 33.33, 33.34]
assert sum(result) == 100


def test_split_series_returns_series():
Expand Down
Loading