Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #28 implementation #30

Merged
merged 10 commits into from
Sep 10, 2023
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
18 changes: 18 additions & 0 deletions src/general.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import typing

import pandas as pd


def summary(data: typing.Iterable, percentiles_list=None) -> pd.DataFrame:
"""
Calculates summary statistics of observed levels
:param percentiles_list: the percentiles to include in the output,
should fall between 0 and 1
:param data: list-like or array-like object with numerical data
:return: pandas DataFrame with statistics
"""
if percentiles_list is None:
percentiles_list = [0.25, 0.5, 0.75]
df = pd.DataFrame(data).describe(percentiles=percentiles_list)
df.columns = ["summary"]
return df
21 changes: 21 additions & 0 deletions unit_tests/test_02_general.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pandas as pd

from src.general import summary


def test_on_example():
dataset = [2, 1, 4, 3, 1, 5, 3, 3, 4, 2, 1,
1, 3, 3, 4, 5, 5, 4, 3, 2, 2, 1, 2, 1]
summary_stats = summary(dataset)
column_name = 'summary'

assert isinstance(summary_stats, pd.DataFrame)
assert summary_stats.loc["count", column_name] == len(dataset)
assert summary_stats.loc["min", column_name] == 1
assert summary_stats.loc["max", column_name] == 5
assert round(summary_stats.loc["mean", column_name]) \
== round(sum(dataset) / len(dataset))
assert round(summary_stats.loc["std", column_name], 2) == 1.37
assert round(summary_stats.loc["25%", column_name], 2) == 1.75
assert round(summary_stats.loc["50%", column_name], 2) == 3.00
assert round(summary_stats.loc["75%", column_name], 2) == 4.00