From 9c20e82ad507206489ab7e8c1c4487d816e227a2 Mon Sep 17 00:00:00 2001 From: Tobias Schmidt <tobias.schmidt@unibas.ch> Date: Wed, 23 May 2012 14:27:15 +0200 Subject: [PATCH] TableClass: mean can also be computed on bool type columns --- modules/base/pymod/table.py | 19 +++++++++++++------ modules/base/tests/test_table.py | 6 ++++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/modules/base/pymod/table.py b/modules/base/pymod/table.py index 3a12c898f..7f57dbc58 100644 --- a/modules/base/pymod/table.py +++ b/modules/base/pymod/table.py @@ -983,7 +983,8 @@ class Table(object): def Sum(self, col): """ Returns the sum of the given column. Cells with None are ignored. Returns - 0.0, if the column doesn't contain any elements. + 0.0, if the column doesn't contain any elements. Col must be of numeric + column type ('float', 'int'). """ idx = self.GetColIndex(col) col_type = self.col_types[idx] @@ -998,12 +999,16 @@ class Table(object): def Mean(self, col): """ Returns the mean of the given column. Cells with None are ignored. Returns - None, if the column doesn't contain any elements. + None, if the column doesn't contain any elements. Col must be of numeric + ('float', 'int') or boolean column type. + + If column type is *bool*, the function returns the ratio of + number of 'Trues' by total number of elements. """ idx = self.GetColIndex(col) col_type = self.col_types[idx] - if col_type!='int' and col_type!='float': - raise TypeError("Mean can only be used on numeric column types") + if col_type!='int' and col_type!='float' and col_type!='bool': + raise TypeError("Mean can only be used on numeric or bool column types") vals=[] for v in self[col]: @@ -1082,7 +1087,8 @@ class Table(object): def Median(self, col): """ Returns the median of the given column. Cells with None are ignored. Returns - None, if the column doesn't contain any elements. + None, if the column doesn't contain any elements. Col must be of numeric + column type ('float', 'int'). """ idx = self.GetColIndex(col) col_type = self.col_types[idx] @@ -1102,7 +1108,8 @@ class Table(object): def StdDev(self, col): """ Returns the standard deviation of the given column. Cells with None are - ignored. Returns None, if the column doesn't contain any elements. + ignored. Returns None, if the column doesn't contain any elements. Col must + be of numeric column type ('float', 'int'). """ idx = self.GetColIndex(col) col_type = self.col_types[idx] diff --git a/modules/base/tests/test_table.py b/modules/base/tests/test_table.py index ca6da2920..842f4d633 100644 --- a/modules/base/tests/test_table.py +++ b/modules/base/tests/test_table.py @@ -941,12 +941,14 @@ class TestTable(unittest.TestCase): def testMeanTable(self): tab = self.CreateTestTable() tab.AddCol('fourth','bool',[False,True,False]) + tab.AddCol('fifth','string',['foo','bar',None]) self.assertRaises(TypeError,tab.Mean,'first') self.assertAlmostEquals(tab.Mean('second'),6.0) self.assertAlmostEquals(tab.Mean('third'),2.75) - self.assertRaises(TypeError,tab.Mean,'fourth') - self.assertRaises(ValueError,tab.Mean,'fifth') + self.assertAlmostEquals(tab.Mean('fourth'),0.33333333) + self.assertRaises(TypeError,tab.Mean,'fifth') + self.assertRaises(ValueError,tab.Mean,'sixth') def testRowMeanTable(self): ''' -- GitLab