diff --git a/modules/base/pymod/table.py b/modules/base/pymod/table.py index 3a12c898f4358f22d777dbd669687ae0a4bbb01e..7f57dbc588b15c52967ebb0d8d762ec24d234967 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 ca6da29208d776b29753ee325ebc1faf565793f9..842f4d63375977ce901cef10f76e29e81dc3d2d6 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): '''