From 7e461a18d9f6928132afec6f48ce968b3e989ba6 Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Mon, 3 Dec 2018 17:43:52 +0100 Subject: [PATCH 1/5] remove \n from docstring --- pandas/core/arrays/datetimes.py | 26 +++++++++++++------------- pandas/core/arrays/timedeltas.py | 16 ++++++++-------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index cfe3afcf3730a..b3df505d56d78 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -82,7 +82,7 @@ def f(self): return result f.__name__ = name - f.__doc__ = docstring + f.__doc__ = "\n{}\n".format(docstring) return property(f) @@ -1072,19 +1072,19 @@ def date(self): return tslib.ints_to_pydatetime(timestamps, box="date") - year = _field_accessor('year', 'Y', "\n The year of the datetime\n") + year = _field_accessor('year', 'Y', "The year of the datetime") month = _field_accessor('month', 'M', - "\n The month as January=1, December=12 \n") - day = _field_accessor('day', 'D', "\nThe days of the datetime\n") - hour = _field_accessor('hour', 'h', "\nThe hours of the datetime\n") - minute = _field_accessor('minute', 'm', "\nThe minutes of the datetime\n") - second = _field_accessor('second', 's', "\nThe seconds of the datetime\n") + "The month as January=1, December=12") + day = _field_accessor('day', 'D', "The days of the datetime") + hour = _field_accessor('hour', 'h', "The hours of the datetime") + minute = _field_accessor('minute', 'm', "The minutes of the datetime") + second = _field_accessor('second', 's', "The seconds of the datetime") microsecond = _field_accessor('microsecond', 'us', - "\nThe microseconds of the datetime\n") + "The microseconds of the datetime") nanosecond = _field_accessor('nanosecond', 'ns', - "\nThe nanoseconds of the datetime\n") + "The nanoseconds of the datetime") weekofyear = _field_accessor('weekofyear', 'woy', - "\nThe week ordinal of the year\n") + "The week ordinal of the year") week = weekofyear _dayofweek_doc = """ The day of the week with Monday=0, Sunday=6. @@ -1129,12 +1129,12 @@ def date(self): "The name of day in a week (ex: Friday)\n\n.. deprecated:: 0.23.0") dayofyear = _field_accessor('dayofyear', 'doy', - "\nThe ordinal day of the year\n") - quarter = _field_accessor('quarter', 'q', "\nThe quarter of the date\n") + "The ordinal day of the year") + quarter = _field_accessor('quarter', 'q', "The quarter of the date") days_in_month = _field_accessor( 'days_in_month', 'dim', - "\nThe number of days in the month\n") + "The number of days in the month") daysinmonth = days_in_month _is_month_doc = """ Indicates whether the date is the {first_or_last} day of the month. diff --git a/pandas/core/arrays/timedeltas.py b/pandas/core/arrays/timedeltas.py index 830283d31a929..4afc9f5483c2a 100644 --- a/pandas/core/arrays/timedeltas.py +++ b/pandas/core/arrays/timedeltas.py @@ -59,7 +59,7 @@ def f(self): return result f.__name__ = name - f.__doc__ = docstring + f.__doc__ = "\n{}\n".format(docstring) return property(f) @@ -684,16 +684,16 @@ def to_pytimedelta(self): return tslibs.ints_to_pytimedelta(self.asi8) days = _field_accessor("days", "days", - "\nNumber of days for each element.\n") + "Number of days for each element.") seconds = _field_accessor("seconds", "seconds", - "\nNumber of seconds (>= 0 and less than 1 day) " - "for each element.\n") + "Number of seconds (>= 0 and less than 1 day) " + "for each element.") microseconds = _field_accessor("microseconds", "microseconds", - "\nNumber of microseconds (>= 0 and less " - "than 1 second) for each element.\n") + "Number of microseconds (>= 0 and less " + "than 1 second) for each element.") nanoseconds = _field_accessor("nanoseconds", "nanoseconds", - "\nNumber of nanoseconds (>= 0 and less " - "than 1 microsecond) for each element.\n") + "Number of nanoseconds (>= 0 and less " + "than 1 microsecond) for each element.") @property def components(self): From bffc9c8f322da625a99a5e64f6d84bef2aabfbbf Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Mon, 30 Dec 2019 22:27:15 +0100 Subject: [PATCH 2/5] Clean for ewm moments tests --- .../tests/window/moments/test_moments_ewm.py | 123 ++++++++++-------- 1 file changed, 72 insertions(+), 51 deletions(-) diff --git a/pandas/tests/window/moments/test_moments_ewm.py b/pandas/tests/window/moments/test_moments_ewm.py index bf2bd1420b7f4..3316de10043a7 100644 --- a/pandas/tests/window/moments/test_moments_ewm.py +++ b/pandas/tests/window/moments/test_moments_ewm.py @@ -216,6 +216,9 @@ def _check_ew(self, name=None, preserve_nan=False): if preserve_nan: assert result[self._nan_locs].isna().all() + @pytest.mark.parametrize("min_periods", [0, 1]) + @pytest.mark.parametrize("name", ["mean", "var", "vol"]) + def test_ew_min_periods(self, min_periods, name): # excluding NaNs correctly arr = randn(50) arr[:10] = np.NaN @@ -228,31 +231,30 @@ def _check_ew(self, name=None, preserve_nan=False): assert result[:11].isna().all() assert not result[11:].isna().any() - for min_periods in (0, 1): - result = getattr(s.ewm(com=50, min_periods=min_periods), name)() - if name == "mean": - assert result[:10].isna().all() - assert not result[10:].isna().any() - else: - # ewm.std, ewm.vol, ewm.var (with bias=False) require at least - # two values - assert result[:11].isna().all() - assert not result[11:].isna().any() - - # check series of length 0 - result = getattr( - Series(dtype=object).ewm(com=50, min_periods=min_periods), name - )() - tm.assert_series_equal(result, Series(dtype="float64")) - - # check series of length 1 - result = getattr(Series([1.0]).ewm(50, min_periods=min_periods), name)() - if name == "mean": - tm.assert_series_equal(result, Series([1.0])) - else: - # ewm.std, ewm.vol, ewm.var with bias=False require at least - # two values - tm.assert_series_equal(result, Series([np.NaN])) + result = getattr(s.ewm(com=50, min_periods=min_periods), name)() + if name == "mean": + assert result[:10].isna().all() + assert not result[10:].isna().any() + else: + # ewm.std, ewm.vol, ewm.var (with bias=False) require at least + # two values + assert result[:11].isna().all() + assert not result[11:].isna().any() + + # check series of length 0 + result = getattr( + Series(dtype=object).ewm(com=50, min_periods=min_periods), name + )() + tm.assert_series_equal(result, Series(dtype="float64")) + + # check series of length 1 + result = getattr(Series([1.0]).ewm(50, min_periods=min_periods), name)() + if name == "mean": + tm.assert_series_equal(result, Series([1.0])) + else: + # ewm.std, ewm.vol, ewm.var with bias=False require at least + # two values + tm.assert_series_equal(result, Series([np.NaN])) # pass in ints result2 = getattr(Series(np.arange(50)).ewm(span=10), name)() @@ -263,49 +265,68 @@ class TestEwmMomentsConsistency(ConsistencyBase): def setup_method(self, method): self._create_data() - def test_ewmcov(self): - self._check_binary_ew("cov") + def _create_binary_ew_data(self): + A = Series(randn(50), index=np.arange(50)) + B = A[2:] + randn(48) + + A[:10] = np.NaN + B[-10:] = np.NaN + return A, B + + @pytest.mark.parametrize("min_periods", [0, 1, 2]) + def test_ewmcov(self, min_periods): + A, B = self._create_binary_ew_data() + + self._check_binary_ew(name="cov", A=A, B=B) + self._check_binary_ew_min_periods("cov", min_periods, A, B) def test_ewmcov_pairwise(self): self._check_pairwise_moment("ewm", "cov", span=10, min_periods=5) - def test_ewmcorr(self): - self._check_binary_ew("corr") + @pytest.mark.parametrize("min_periods", [0, 1, 2]) + def test_ewmcorr(self, min_periods): + A, B = self._create_binary_ew_data() + + self._check_binary_ew(name="corr", A=A, B=B) + self._check_binary_ew_min_periods("corr", min_periods, A, B) def test_ewmcorr_pairwise(self): self._check_pairwise_moment("ewm", "corr", span=10, min_periods=5) - def _check_binary_ew(self, name): + def _check_binary_ew(self, name, A, B): def func(A, B, com, **kwargs): return getattr(A.ewm(com, **kwargs), name)(B) - A = Series(randn(50), index=np.arange(50)) - B = A[2:] + randn(48) - - A[:10] = np.NaN - B[-10:] = np.NaN - result = func(A, B, 20, min_periods=5) assert np.isnan(result.values[:14]).all() assert not np.isnan(result.values[14:]).any() + def _check_binary_ew_min_periods(self, name, min_periods, A, B): + def func(A, B, com, **kwargs): + return getattr(A.ewm(com, **kwargs), name)(B) + # GH 7898 - for min_periods in (0, 1, 2): - result = func(A, B, 20, min_periods=min_periods) - # binary functions (ewmcov, ewmcorr) with bias=False require at - # least two values - assert np.isnan(result.values[:11]).all() - assert not np.isnan(result.values[11:]).any() - - # check series of length 0 - empty = Series([], dtype=np.float64) - result = func(empty, empty, 50, min_periods=min_periods) - tm.assert_series_equal(result, empty) - - # check series of length 1 - result = func(Series([1.0]), Series([1.0]), 50, min_periods=min_periods) - tm.assert_series_equal(result, Series([np.NaN])) + result = func(A, B, 20, min_periods=min_periods) + # binary functions (ewmcov, ewmcorr) with bias=False require at + # least two values + assert np.isnan(result.values[:11]).all() + assert not np.isnan(result.values[11:]).any() + + # check series of length 0 + empty = Series([], dtype=np.float64) + result = func(empty, empty, 50, min_periods=min_periods) + tm.assert_series_equal(result, empty) + + # check series of length 1 + result = func(Series([1.0]), Series([1.0]), 50, min_periods=min_periods) + tm.assert_series_equal(result, Series([np.NaN])) + + @pytest.mark.parametrize("name", ["cov", "corr"]) + def test_different_input_array_raise_exception(self, name): + def func(A, B, com, **kwargs): + return getattr(A.ewm(com, **kwargs), name)(B) + A, _ = self._create_binary_ew_data() msg = "Input arrays must be of the same type!" # exception raised is Exception with pytest.raises(Exception, match=msg): From dae493b39b99da0e8eabe557fa946e668ca84844 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Thu, 2 Jan 2020 22:16:15 +0100 Subject: [PATCH 3/5] code change based on JR reviews --- pandas/tests/window/common.py | 3 ++ pandas/tests/window/moments/conftest.py | 20 +++++++++ .../tests/window/moments/test_moments_ewm.py | 41 ++++++------------- 3 files changed, 36 insertions(+), 28 deletions(-) create mode 100644 pandas/tests/window/moments/conftest.py diff --git a/pandas/tests/window/common.py b/pandas/tests/window/common.py index c3648bc619c50..af0558cc37c40 100644 --- a/pandas/tests/window/common.py +++ b/pandas/tests/window/common.py @@ -348,3 +348,6 @@ def get_result(obj, obj2=None): result.index = result.index.droplevel(1) expected = get_result(self.frame[1], self.frame[5]) tm.assert_series_equal(result, expected, check_names=False) + + def _ew_func(self, A, B, com, name, **kwargs): + return getattr(self, A.ewm(com, **kwargs), name)(B) diff --git a/pandas/tests/window/moments/conftest.py b/pandas/tests/window/moments/conftest.py new file mode 100644 index 0000000000000..2002f4d0bff43 --- /dev/null +++ b/pandas/tests/window/moments/conftest.py @@ -0,0 +1,20 @@ +import numpy as np +from numpy.random import randn +import pytest + +from pandas import Series + + +@pytest.fixture +def binary_ew_data(): + A = Series(randn(50), index=np.arange(50)) + B = A[2:] + randn(48) + + A[:10] = np.NaN + B[-10:] = np.NaN + return A, B + + +@pytest.fixture(params=[0, 1, 2]) +def min_periods(request): + return request.param diff --git a/pandas/tests/window/moments/test_moments_ewm.py b/pandas/tests/window/moments/test_moments_ewm.py index 3316de10043a7..6810afd8af9d7 100644 --- a/pandas/tests/window/moments/test_moments_ewm.py +++ b/pandas/tests/window/moments/test_moments_ewm.py @@ -265,17 +265,8 @@ class TestEwmMomentsConsistency(ConsistencyBase): def setup_method(self, method): self._create_data() - def _create_binary_ew_data(self): - A = Series(randn(50), index=np.arange(50)) - B = A[2:] + randn(48) - - A[:10] = np.NaN - B[-10:] = np.NaN - return A, B - - @pytest.mark.parametrize("min_periods", [0, 1, 2]) - def test_ewmcov(self, min_periods): - A, B = self._create_binary_ew_data() + def test_ewmcov(self, min_periods, binary_ew_data): + A, B = binary_ew_data() self._check_binary_ew(name="cov", A=A, B=B) self._check_binary_ew_min_periods("cov", min_periods, A, B) @@ -283,9 +274,8 @@ def test_ewmcov(self, min_periods): def test_ewmcov_pairwise(self): self._check_pairwise_moment("ewm", "cov", span=10, min_periods=5) - @pytest.mark.parametrize("min_periods", [0, 1, 2]) - def test_ewmcorr(self, min_periods): - A, B = self._create_binary_ew_data() + def test_ewmcorr(self, min_periods, binary_ew_data): + A, B = binary_ew_data() self._check_binary_ew(name="corr", A=A, B=B) self._check_binary_ew_min_periods("corr", min_periods, A, B) @@ -294,19 +284,14 @@ def test_ewmcorr_pairwise(self): self._check_pairwise_moment("ewm", "corr", span=10, min_periods=5) def _check_binary_ew(self, name, A, B): - def func(A, B, com, **kwargs): - return getattr(A.ewm(com, **kwargs), name)(B) - result = func(A, B, 20, min_periods=5) + result = self._ew_func(A, B, 20, name=name, min_periods=5) assert np.isnan(result.values[:14]).all() assert not np.isnan(result.values[14:]).any() def _check_binary_ew_min_periods(self, name, min_periods, A, B): - def func(A, B, com, **kwargs): - return getattr(A.ewm(com, **kwargs), name)(B) - # GH 7898 - result = func(A, B, 20, min_periods=min_periods) + result = self._ew_func(A, B, 20, name=name, min_periods=min_periods) # binary functions (ewmcov, ewmcorr) with bias=False require at # least two values assert np.isnan(result.values[:11]).all() @@ -314,23 +299,23 @@ def func(A, B, com, **kwargs): # check series of length 0 empty = Series([], dtype=np.float64) - result = func(empty, empty, 50, min_periods=min_periods) + result = self._ew_func(empty, empty, 50, name=name, min_periods=min_periods) tm.assert_series_equal(result, empty) # check series of length 1 - result = func(Series([1.0]), Series([1.0]), 50, min_periods=min_periods) + result = self._ew_func( + Series([1.0]), Series([1.0]), 50, name=name, min_periods=min_periods + ) tm.assert_series_equal(result, Series([np.NaN])) @pytest.mark.parametrize("name", ["cov", "corr"]) - def test_different_input_array_raise_exception(self, name): - def func(A, B, com, **kwargs): - return getattr(A.ewm(com, **kwargs), name)(B) + def test_different_input_array_raise_exception(self, name, binary_ew_data): - A, _ = self._create_binary_ew_data() + A, _ = binary_ew_data() msg = "Input arrays must be of the same type!" # exception raised is Exception with pytest.raises(Exception, match=msg): - func(A, randn(50), 20, min_periods=5) + self._ew_func(A, randn(50), 20, name=name, min_periods=5) @pytest.mark.slow @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4]) From 2624900c8c197c904919643e929edde9adc9632a Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Thu, 2 Jan 2020 23:01:23 +0100 Subject: [PATCH 4/5] fix error --- pandas/tests/window/common.py | 5 +++-- pandas/tests/window/moments/test_moments_ewm.py | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pandas/tests/window/common.py b/pandas/tests/window/common.py index af0558cc37c40..feaae9425a589 100644 --- a/pandas/tests/window/common.py +++ b/pandas/tests/window/common.py @@ -349,5 +349,6 @@ def get_result(obj, obj2=None): expected = get_result(self.frame[1], self.frame[5]) tm.assert_series_equal(result, expected, check_names=False) - def _ew_func(self, A, B, com, name, **kwargs): - return getattr(self, A.ewm(com, **kwargs), name)(B) + @staticmethod + def _ew_func(A, B, com, name, **kwargs): + return getattr(A.ewm(com, **kwargs), name)(B) diff --git a/pandas/tests/window/moments/test_moments_ewm.py b/pandas/tests/window/moments/test_moments_ewm.py index 6810afd8af9d7..218af185bde24 100644 --- a/pandas/tests/window/moments/test_moments_ewm.py +++ b/pandas/tests/window/moments/test_moments_ewm.py @@ -266,7 +266,7 @@ def setup_method(self, method): self._create_data() def test_ewmcov(self, min_periods, binary_ew_data): - A, B = binary_ew_data() + A, B = binary_ew_data self._check_binary_ew(name="cov", A=A, B=B) self._check_binary_ew_min_periods("cov", min_periods, A, B) @@ -275,7 +275,7 @@ def test_ewmcov_pairwise(self): self._check_pairwise_moment("ewm", "cov", span=10, min_periods=5) def test_ewmcorr(self, min_periods, binary_ew_data): - A, B = binary_ew_data() + A, B = binary_ew_data self._check_binary_ew(name="corr", A=A, B=B) self._check_binary_ew_min_periods("corr", min_periods, A, B) @@ -285,7 +285,7 @@ def test_ewmcorr_pairwise(self): def _check_binary_ew(self, name, A, B): - result = self._ew_func(A, B, 20, name=name, min_periods=5) + result = self._ew_func(A=A, B=B, com=20, name=name, min_periods=5) assert np.isnan(result.values[:14]).all() assert not np.isnan(result.values[14:]).any() @@ -311,7 +311,7 @@ def _check_binary_ew_min_periods(self, name, min_periods, A, B): @pytest.mark.parametrize("name", ["cov", "corr"]) def test_different_input_array_raise_exception(self, name, binary_ew_data): - A, _ = binary_ew_data() + A, _ = binary_ew_data msg = "Input arrays must be of the same type!" # exception raised is Exception with pytest.raises(Exception, match=msg): From ad959ee6bffd3914af3635fe5622e3e9f197bf04 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Fri, 3 Jan 2020 08:50:31 +0100 Subject: [PATCH 5/5] code change bsaed on JR reviews --- pandas/tests/window/common.py | 33 +++++++++++-- .../tests/window/moments/test_moments_ewm.py | 48 +++++-------------- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/pandas/tests/window/common.py b/pandas/tests/window/common.py index feaae9425a589..e1f322622de48 100644 --- a/pandas/tests/window/common.py +++ b/pandas/tests/window/common.py @@ -349,6 +349,33 @@ def get_result(obj, obj2=None): expected = get_result(self.frame[1], self.frame[5]) tm.assert_series_equal(result, expected, check_names=False) - @staticmethod - def _ew_func(A, B, com, name, **kwargs): - return getattr(A.ewm(com, **kwargs), name)(B) + +def ew_func(A, B, com, name, **kwargs): + return getattr(A.ewm(com, **kwargs), name)(B) + + +def check_binary_ew(name, A, B): + + result = ew_func(A=A, B=B, com=20, name=name, min_periods=5) + assert np.isnan(result.values[:14]).all() + assert not np.isnan(result.values[14:]).any() + + +def check_binary_ew_min_periods(name, min_periods, A, B): + # GH 7898 + result = ew_func(A, B, 20, name=name, min_periods=min_periods) + # binary functions (ewmcov, ewmcorr) with bias=False require at + # least two values + assert np.isnan(result.values[:11]).all() + assert not np.isnan(result.values[11:]).any() + + # check series of length 0 + empty = Series([], dtype=np.float64) + result = ew_func(empty, empty, 50, name=name, min_periods=min_periods) + tm.assert_series_equal(result, empty) + + # check series of length 1 + result = ew_func( + Series([1.0]), Series([1.0]), 50, name=name, min_periods=min_periods + ) + tm.assert_series_equal(result, Series([np.NaN])) diff --git a/pandas/tests/window/moments/test_moments_ewm.py b/pandas/tests/window/moments/test_moments_ewm.py index 218af185bde24..46cd503e95e11 100644 --- a/pandas/tests/window/moments/test_moments_ewm.py +++ b/pandas/tests/window/moments/test_moments_ewm.py @@ -4,7 +4,13 @@ import pandas as pd from pandas import DataFrame, Series, concat -from pandas.tests.window.common import Base, ConsistencyBase +from pandas.tests.window.common import ( + Base, + ConsistencyBase, + check_binary_ew, + check_binary_ew_min_periods, + ew_func, +) import pandas.util.testing as tm @@ -265,49 +271,19 @@ class TestEwmMomentsConsistency(ConsistencyBase): def setup_method(self, method): self._create_data() - def test_ewmcov(self, min_periods, binary_ew_data): - A, B = binary_ew_data - - self._check_binary_ew(name="cov", A=A, B=B) - self._check_binary_ew_min_periods("cov", min_periods, A, B) - def test_ewmcov_pairwise(self): self._check_pairwise_moment("ewm", "cov", span=10, min_periods=5) - def test_ewmcorr(self, min_periods, binary_ew_data): + @pytest.mark.parametrize("name", ["cov", "corr"]) + def test_ewm_corr_cov(self, name, min_periods, binary_ew_data): A, B = binary_ew_data - self._check_binary_ew(name="corr", A=A, B=B) - self._check_binary_ew_min_periods("corr", min_periods, A, B) + check_binary_ew(name="corr", A=A, B=B) + check_binary_ew_min_periods("corr", min_periods, A, B) def test_ewmcorr_pairwise(self): self._check_pairwise_moment("ewm", "corr", span=10, min_periods=5) - def _check_binary_ew(self, name, A, B): - - result = self._ew_func(A=A, B=B, com=20, name=name, min_periods=5) - assert np.isnan(result.values[:14]).all() - assert not np.isnan(result.values[14:]).any() - - def _check_binary_ew_min_periods(self, name, min_periods, A, B): - # GH 7898 - result = self._ew_func(A, B, 20, name=name, min_periods=min_periods) - # binary functions (ewmcov, ewmcorr) with bias=False require at - # least two values - assert np.isnan(result.values[:11]).all() - assert not np.isnan(result.values[11:]).any() - - # check series of length 0 - empty = Series([], dtype=np.float64) - result = self._ew_func(empty, empty, 50, name=name, min_periods=min_periods) - tm.assert_series_equal(result, empty) - - # check series of length 1 - result = self._ew_func( - Series([1.0]), Series([1.0]), 50, name=name, min_periods=min_periods - ) - tm.assert_series_equal(result, Series([np.NaN])) - @pytest.mark.parametrize("name", ["cov", "corr"]) def test_different_input_array_raise_exception(self, name, binary_ew_data): @@ -315,7 +291,7 @@ def test_different_input_array_raise_exception(self, name, binary_ew_data): msg = "Input arrays must be of the same type!" # exception raised is Exception with pytest.raises(Exception, match=msg): - self._ew_func(A, randn(50), 20, name=name, min_periods=5) + ew_func(A, randn(50), 20, name=name, min_periods=5) @pytest.mark.slow @pytest.mark.parametrize("min_periods", [0, 1, 2, 3, 4])