From 5df10d16b371ec37a144bdcb847290acc2f9b2a3 Mon Sep 17 00:00:00 2001 From: Josh Renaud Date: Wed, 6 Oct 2021 14:31:50 -0500 Subject: [PATCH 1/3] Potential revision for mean.py to add support for TimeDelta. https://github.com/wireservice/agate/issues/761 --- agate/aggregations/mean.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/agate/aggregations/mean.py b/agate/aggregations/mean.py index 689b674f..ac0de983 100644 --- a/agate/aggregations/mean.py +++ b/agate/aggregations/mean.py @@ -3,7 +3,7 @@ from agate.aggregations.base import Aggregation from agate.aggregations.has_nulls import HasNulls from agate.aggregations.sum import Sum -from agate.data_types import Number +from agate.data_types import Number, TimeDelta from agate.exceptions import DataTypeError from agate.warns import warn_null_calculation @@ -20,13 +20,16 @@ def __init__(self, column_name): self._sum = Sum(column_name) def get_aggregate_data_type(self, table): - return Number() + column = table.columns[self._column_name] + + if isinstance(column.data_type, (Number, TimeDelta)): + return column.data_type def validate(self, table): column = table.columns[self._column_name] - if not isinstance(column.data_type, Number): - raise DataTypeError('Mean can only be applied to columns containing Number data.') + if not isinstance(column.data_type, (Number, TimeDelta)): + raise DataTypeError('Sum can only be applied to columns containing Number or TimeDelta data.') has_nulls = HasNulls(self._column_name).run(table) From 9d14c21f7210cf789764b5d464549d2812054058 Mon Sep 17 00:00:00 2001 From: Josh Renaud Date: Wed, 6 Oct 2021 14:34:06 -0500 Subject: [PATCH 2/3] Fix Mean() DataTypeError message --- agate/aggregations/mean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agate/aggregations/mean.py b/agate/aggregations/mean.py index ac0de983..c71957e9 100644 --- a/agate/aggregations/mean.py +++ b/agate/aggregations/mean.py @@ -29,7 +29,7 @@ def validate(self, table): column = table.columns[self._column_name] if not isinstance(column.data_type, (Number, TimeDelta)): - raise DataTypeError('Sum can only be applied to columns containing Number or TimeDelta data.') + raise DataTypeError('Mean can only be applied to columns containing Number or TimeDelta data.') has_nulls = HasNulls(self._column_name).run(table) From d0abc3fc4ba069a4b80e59331da8ee04a0f6ad30 Mon Sep 17 00:00:00 2001 From: Josh Renaud Date: Fri, 17 Jun 2022 14:34:53 -0500 Subject: [PATCH 3/3] Allow null values in the slug computation --- agate/computations/slug.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/agate/computations/slug.py b/agate/computations/slug.py index 68a00fd4..b3041362 100644 --- a/agate/computations/slug.py +++ b/agate/computations/slug.py @@ -6,6 +6,11 @@ from agate.exceptions import DataTypeError from agate.utils import issequence, slugify +def denonify(v): + if v == None: + return '' + return v + class Slug(Computation): """ @@ -40,8 +45,9 @@ def validate(self, table): if not isinstance(column.data_type, Text): raise DataTypeError('Slug column must contain Text data.') - if HasNulls(column_name).run(table): - raise ValueError('Slug column cannot contain `None`.') + # JOSH CHANGE: Sure a slug can have None. We'll just replace it with an empty string. + # if HasNulls(column_name).run(table): + # raise ValueError('Slug column cannot contain `None`.') def run(self, table): """ @@ -54,10 +60,10 @@ def run(self, table): if issequence(self._column_name): column_value = '' for column_name in self._column_name: - column_value = column_value + ' ' + row[column_name] + column_value = column_value + ' ' + denonify(row[column_name]) new_column.append(column_value) else: - new_column.append(row[self._column_name]) + new_column.append(denonify(row[self._column_name])) return slugify(new_column, ensure_unique=self._ensure_unique, **self._slug_args)