summaryrefslogtreecommitdiff
path: root/tablib/packages
diff options
context:
space:
mode:
authorNorman Hooper <nhooper@dimagi.com>2018-09-12 20:27:10 +0200
committerIuri de Silvio <iurisilvio@gmail.com>2018-09-12 15:27:10 -0300
commit38486231cc4dc46d1ff055a38fe7c236cbe5c8aa (patch)
treecfa105d916117d8b43d87cbd331be46c947b3805 /tablib/packages
parent75f1bafd69add49d969b0f8e579baf3659f16100 (diff)
downloadtablib-38486231cc4dc46d1ff055a38fe7c236cbe5c8aa.tar.gz
reStructuredText (#336)
* median for Python 2 * More compat * Support reStructuredText * Tests
Diffstat (limited to 'tablib/packages')
-rw-r--r--tablib/packages/statistics.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/tablib/packages/statistics.py b/tablib/packages/statistics.py
new file mode 100644
index 0000000..e97a6c9
--- /dev/null
+++ b/tablib/packages/statistics.py
@@ -0,0 +1,24 @@
+from __future__ import division
+
+
+def median(data):
+ """
+ Return the median (middle value) of numeric data, using the common
+ "mean of middle two" method. If data is empty, ValueError is raised.
+
+ Mimics the behaviour of Python3's statistics.median
+
+ >>> median([1, 3, 5])
+ 3
+ >>> median([1, 3, 5, 7])
+ 4.0
+
+ """
+ data = sorted(data)
+ n = len(data)
+ if not n:
+ raise ValueError("No median for empty data")
+ i = n // 2
+ if n % 2:
+ return data[i]
+ return (data[i - 1] + data[i]) / 2