diff --git a/tests/units/feature_extraction/test_feature_calculations.py b/tests/units/feature_extraction/test_feature_calculations.py index 129e9c3b8..0ed1c6aab 100644 --- a/tests/units/feature_extraction/test_feature_calculations.py +++ b/tests/units/feature_extraction/test_feature_calculations.py @@ -318,6 +318,9 @@ def test_mean_abs_change(self): def test_mean_change(self): self.assertEqualOnAllArrayTypes(mean_change, [-2, 2, 5], 3.5) self.assertEqualOnAllArrayTypes(mean_change, [1, 2, -1], -1) + self.assertEqualOnAllArrayTypes(mean_change, [10, 20], 10) + self.assertIsNanOnAllArrayTypes(mean_change, [1]) + self.assertIsNanOnAllArrayTypes(mean_change, []) def test_mean_second_derivate_central(self): self.assertEqualOnAllArrayTypes(mean_second_derivative_central, list(range(10)), 0) diff --git a/tsfresh/feature_extraction/feature_calculators.py b/tsfresh/feature_extraction/feature_calculators.py index b5c882391..fdbc8b0d6 100644 --- a/tsfresh/feature_extraction/feature_calculators.py +++ b/tsfresh/feature_extraction/feature_calculators.py @@ -531,14 +531,15 @@ def mean_change(x): .. math:: - \\frac{1}{n} \\sum_{i=1,\ldots, n-1} x_{i+1} - x_{i} + \\frac{1}{n-1} \\sum_{i=1,\ldots, n-1} x_{i+1} - x_{i} = \\frac{1}{n-1} (x_{n} - x_{1}) :param x: the time series to calculate the feature of :type x: numpy.ndarray :return: the value of this feature :return type: float """ - return np.mean(np.diff(x)) + x = np.asarray(x) + return (x[-1] - x[0]) / (len(x) - 1) if len(x) > 1 else np.NaN @set_property("fctype", "simple")