From 7e461a18d9f6928132afec6f48ce968b3e989ba6 Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Mon, 3 Dec 2018 17:43:52 +0100 Subject: [PATCH 1/9] 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 dea38f24c0067ae3fe9484b837c9649714213bba Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Tue, 14 Jan 2020 21:26:31 +0100 Subject: [PATCH 2/9] fix issue 17038 --- pandas/core/reshape/pivot.py | 4 +++- pandas/tests/reshape/test_pivot.py | 20 ++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index b443ba142369c..9743d90f4dd04 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -117,7 +117,9 @@ def pivot_table( agged[v] = maybe_downcast_to_dtype(agged[v], data[v].dtype) table = agged - if table.index.nlevels > 1: + + # GH 17038, this check should only happen if index is specified + if table.index.nlevels > 1 and index: # Related GH #17123 # If index_names are integers, determine whether the integers refer # to the level position or name. diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 743fc50c87e96..46a05123c9fdd 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -896,12 +896,6 @@ def _check_output( totals = table.loc[("All", ""), value_col] assert totals == self.data[value_col].mean() - # no rows - rtable = self.data.pivot_table( - columns=["AA", "BB"], margins=True, aggfunc=np.mean - ) - assert isinstance(rtable, Series) - table = self.data.pivot_table(index=["AA", "BB"], margins=True, aggfunc="mean") for item in ["DD", "EE", "FF"]: totals = table.loc[("All", ""), item] @@ -972,6 +966,20 @@ def test_pivot_integer_columns(self): tm.assert_frame_equal(table, table2, check_names=False) + @pytest.mark.parametrize("cols", [(1, 2), ("a", "b"), (1, "b"), ("a", 1)]) + def test_pivot_table_multiindex_only(self, cols): + # GH 17038 + df2 = DataFrame({cols[0]: [1, 2, 3], cols[1]: [1, 2, 3], "v": [4, 5, 6]}) + + result = df2.pivot_table(values="v", columns=cols) + expected = DataFrame( + [[4, 5, 6]], + columns=MultiIndex.from_tuples([(1, 1), (2, 2), (3, 3)], names=cols), + index=Index(["v"]), + ) + + tm.assert_frame_equal(result, expected) + def test_pivot_no_level_overlap(self): # GH #1181 From cd9e7ac3f31ffaf95cd628863df911dea9fa1248 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Tue, 14 Jan 2020 21:29:43 +0100 Subject: [PATCH 3/9] revert change --- pandas/core/reshape/pivot.py | 3 +-- pandas/tests/reshape/test_pivot.py | 20 ++++++-------------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index 9743d90f4dd04..a7cdbb0da7a4e 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -118,8 +118,7 @@ def pivot_table( table = agged - # GH 17038, this check should only happen if index is specified - if table.index.nlevels > 1 and index: + if table.index.nlevels > 1: # Related GH #17123 # If index_names are integers, determine whether the integers refer # to the level position or name. diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 46a05123c9fdd..743fc50c87e96 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -896,6 +896,12 @@ def _check_output( totals = table.loc[("All", ""), value_col] assert totals == self.data[value_col].mean() + # no rows + rtable = self.data.pivot_table( + columns=["AA", "BB"], margins=True, aggfunc=np.mean + ) + assert isinstance(rtable, Series) + table = self.data.pivot_table(index=["AA", "BB"], margins=True, aggfunc="mean") for item in ["DD", "EE", "FF"]: totals = table.loc[("All", ""), item] @@ -966,20 +972,6 @@ def test_pivot_integer_columns(self): tm.assert_frame_equal(table, table2, check_names=False) - @pytest.mark.parametrize("cols", [(1, 2), ("a", "b"), (1, "b"), ("a", 1)]) - def test_pivot_table_multiindex_only(self, cols): - # GH 17038 - df2 = DataFrame({cols[0]: [1, 2, 3], cols[1]: [1, 2, 3], "v": [4, 5, 6]}) - - result = df2.pivot_table(values="v", columns=cols) - expected = DataFrame( - [[4, 5, 6]], - columns=MultiIndex.from_tuples([(1, 1), (2, 2), (3, 3)], names=cols), - index=Index(["v"]), - ) - - tm.assert_frame_equal(result, expected) - def test_pivot_no_level_overlap(self): # GH #1181 From e5e912be0f596943067a7df812442764d311a086 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Tue, 14 Jan 2020 21:30:16 +0100 Subject: [PATCH 4/9] revert change --- pandas/core/reshape/pivot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index a7cdbb0da7a4e..b443ba142369c 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -117,7 +117,6 @@ def pivot_table( agged[v] = maybe_downcast_to_dtype(agged[v], data[v].dtype) table = agged - if table.index.nlevels > 1: # Related GH #17123 # If index_names are integers, determine whether the integers refer From 9ccc684a91a8ced8d42294b75fac9a30ac94c5b0 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Mon, 11 May 2020 19:24:31 +0200 Subject: [PATCH 5/9] deprivate pairwise moment --- pandas/tests/window/common.py | 21 ++++++++++--------- .../tests/window/moments/test_moments_ewm.py | 5 +++-- .../window/moments/test_moments_rolling.py | 5 +++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/pandas/tests/window/common.py b/pandas/tests/window/common.py index 25989191ae657..c63ab4655a02e 100644 --- a/pandas/tests/window/common.py +++ b/pandas/tests/window/common.py @@ -28,16 +28,17 @@ class ConsistencyBase(Base): def _create_data(self): super()._create_data() - def _check_pairwise_moment(self, dispatch, name, **kwargs): - def get_result(obj, obj2=None): - return getattr(getattr(obj, dispatch)(**kwargs), name)(obj2) - - result = get_result(self.frame) - result = result.loc[(slice(None), 1), 5] - result.index = result.index.droplevel(1) - expected = get_result(self.frame[1], self.frame[5]) - expected.index = expected.index._with_freq(None) - tm.assert_series_equal(result, expected, check_names=False) + +def check_pairwise_moment(frame, dispatch, name, **kwargs): + def get_result(obj, obj2=None): + return getattr(getattr(obj, dispatch)(**kwargs), name)(obj2) + + result = get_result(frame) + result = result.loc[(slice(None), 1), 5] + result.index = result.index.droplevel(1) + expected = get_result(frame[1], frame[5]) + expected.index = expected.index._with_freq(None) + tm.assert_series_equal(result, expected, check_names=False) def ew_func(A, B, com, name, **kwargs): diff --git a/pandas/tests/window/moments/test_moments_ewm.py b/pandas/tests/window/moments/test_moments_ewm.py index 99f1842babc77..b61c3e2e86f92 100644 --- a/pandas/tests/window/moments/test_moments_ewm.py +++ b/pandas/tests/window/moments/test_moments_ewm.py @@ -9,6 +9,7 @@ Base, ConsistencyBase, check_binary_ew, + check_pairwise_moment, check_binary_ew_min_periods, ew_func, moments_consistency_cov_data, @@ -279,10 +280,10 @@ def setup_method(self, method): self._create_data() def test_ewmcov_pairwise(self): - self._check_pairwise_moment("ewm", "cov", span=10, min_periods=5) + check_pairwise_moment(self.frame, "ewm", "cov", span=10, min_periods=5) def test_ewmcorr_pairwise(self): - self._check_pairwise_moment("ewm", "corr", span=10, min_periods=5) + check_pairwise_moment(self.frame, "ewm", "corr", span=10, min_periods=5) @pytest.mark.parametrize("name", ["cov", "corr"]) diff --git a/pandas/tests/window/moments/test_moments_rolling.py b/pandas/tests/window/moments/test_moments_rolling.py index 28ad2b8663efe..e661b7bd65e22 100644 --- a/pandas/tests/window/moments/test_moments_rolling.py +++ b/pandas/tests/window/moments/test_moments_rolling.py @@ -15,6 +15,7 @@ from pandas.tests.window.common import ( Base, ConsistencyBase, + check_pairwise_moment, moments_consistency_cov_data, moments_consistency_is_constant, moments_consistency_mock_mean, @@ -955,7 +956,7 @@ def test_rolling_cov(self): tm.assert_almost_equal(result[-1], np.cov(A[-50:], B[-50:])[0, 1]) def test_rolling_cov_pairwise(self): - self._check_pairwise_moment("rolling", "cov", window=10, min_periods=5) + check_pairwise_moment(self.frame, "rolling", "cov", window=10, min_periods=5) def test_rolling_corr(self): A = self.series @@ -974,7 +975,7 @@ def test_rolling_corr(self): tm.assert_almost_equal(result[-1], a.corr(b)) def test_rolling_corr_pairwise(self): - self._check_pairwise_moment("rolling", "corr", window=10, min_periods=5) + check_pairwise_moment(self.frame, "rolling", "corr", window=10, min_periods=5) @pytest.mark.parametrize("method", ["corr", "cov"]) def test_flex_binary_frame(self, method): From 63c3c6fba6cd9bed42994411bed76ad1c8cd8325 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Mon, 11 May 2020 19:29:00 +0200 Subject: [PATCH 6/9] isort --- pandas/tests/window/moments/test_moments_ewm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/window/moments/test_moments_ewm.py b/pandas/tests/window/moments/test_moments_ewm.py index b61c3e2e86f92..026c4c331c8d1 100644 --- a/pandas/tests/window/moments/test_moments_ewm.py +++ b/pandas/tests/window/moments/test_moments_ewm.py @@ -9,8 +9,8 @@ Base, ConsistencyBase, check_binary_ew, - check_pairwise_moment, check_binary_ew_min_periods, + check_pairwise_moment, ew_func, moments_consistency_cov_data, moments_consistency_is_constant, From 8091030b998ae9ace5e816a065df5c336d78f138 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Mon, 11 May 2020 19:33:54 +0200 Subject: [PATCH 7/9] remove consistency base --- pandas/tests/window/common.py | 5 ----- pandas/tests/window/moments/test_moments_ewm.py | 3 +-- pandas/tests/window/moments/test_moments_expanding.py | 3 +-- pandas/tests/window/moments/test_moments_rolling.py | 3 +-- 4 files changed, 3 insertions(+), 11 deletions(-) diff --git a/pandas/tests/window/common.py b/pandas/tests/window/common.py index c63ab4655a02e..5dca26df49930 100644 --- a/pandas/tests/window/common.py +++ b/pandas/tests/window/common.py @@ -24,11 +24,6 @@ def _create_data(self): self.frame = DataFrame(randn(N, K), index=self.rng, columns=np.arange(K)) -class ConsistencyBase(Base): - def _create_data(self): - super()._create_data() - - def check_pairwise_moment(frame, dispatch, name, **kwargs): def get_result(obj, obj2=None): return getattr(getattr(obj, dispatch)(**kwargs), name)(obj2) diff --git a/pandas/tests/window/moments/test_moments_ewm.py b/pandas/tests/window/moments/test_moments_ewm.py index 026c4c331c8d1..caf06f9e1ac9c 100644 --- a/pandas/tests/window/moments/test_moments_ewm.py +++ b/pandas/tests/window/moments/test_moments_ewm.py @@ -7,7 +7,6 @@ import pandas._testing as tm from pandas.tests.window.common import ( Base, - ConsistencyBase, check_binary_ew, check_binary_ew_min_periods, check_pairwise_moment, @@ -275,7 +274,7 @@ def test_ew_min_periods(self, min_periods, name): assert result2.dtype == np.float_ -class TestEwmMomentsConsistency(ConsistencyBase): +class TestEwmMomentsConsistency(Base): def setup_method(self, method): self._create_data() diff --git a/pandas/tests/window/moments/test_moments_expanding.py b/pandas/tests/window/moments/test_moments_expanding.py index 6aff5b78b4ada..f40f940aa8e9f 100644 --- a/pandas/tests/window/moments/test_moments_expanding.py +++ b/pandas/tests/window/moments/test_moments_expanding.py @@ -7,7 +7,6 @@ from pandas import DataFrame, Index, MultiIndex, Series, isna, notna import pandas._testing as tm from pandas.tests.window.common import ( - ConsistencyBase, moments_consistency_cov_data, moments_consistency_is_constant, moments_consistency_mock_mean, @@ -18,7 +17,7 @@ ) -class TestExpandingMomentsConsistency(ConsistencyBase): +class TestExpandingMomentsConsistency(Base): def setup_method(self, method): self._create_data() diff --git a/pandas/tests/window/moments/test_moments_rolling.py b/pandas/tests/window/moments/test_moments_rolling.py index e661b7bd65e22..60aed8565c360 100644 --- a/pandas/tests/window/moments/test_moments_rolling.py +++ b/pandas/tests/window/moments/test_moments_rolling.py @@ -14,7 +14,6 @@ from pandas.core.window.common import _flex_binary_moment from pandas.tests.window.common import ( Base, - ConsistencyBase, check_pairwise_moment, moments_consistency_cov_data, moments_consistency_is_constant, @@ -943,7 +942,7 @@ def _rolling_consistency_cases(): yield window, min_periods, center -class TestRollingMomentsConsistency(ConsistencyBase): +class TestRollingMomentsConsistency(Base): def setup_method(self, method): self._create_data() From 837e6b12399e9c14e499b7a539581fc244241c93 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Mon, 11 May 2020 19:35:40 +0200 Subject: [PATCH 8/9] import base --- pandas/tests/window/moments/test_moments_expanding.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/window/moments/test_moments_expanding.py b/pandas/tests/window/moments/test_moments_expanding.py index f40f940aa8e9f..09cd2ff218c2b 100644 --- a/pandas/tests/window/moments/test_moments_expanding.py +++ b/pandas/tests/window/moments/test_moments_expanding.py @@ -7,6 +7,7 @@ from pandas import DataFrame, Index, MultiIndex, Series, isna, notna import pandas._testing as tm from pandas.tests.window.common import ( + Base, moments_consistency_cov_data, moments_consistency_is_constant, moments_consistency_mock_mean, From bd8478b209525fd984d0faa47dd97aae761724e5 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Mon, 11 May 2020 20:08:04 +0200 Subject: [PATCH 9/9] parametrize --- pandas/tests/window/moments/test_moments_ewm.py | 8 +++----- pandas/tests/window/moments/test_moments_rolling.py | 8 +++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/pandas/tests/window/moments/test_moments_ewm.py b/pandas/tests/window/moments/test_moments_ewm.py index caf06f9e1ac9c..17d497e6e1320 100644 --- a/pandas/tests/window/moments/test_moments_ewm.py +++ b/pandas/tests/window/moments/test_moments_ewm.py @@ -278,11 +278,9 @@ class TestEwmMomentsConsistency(Base): def setup_method(self, method): self._create_data() - def test_ewmcov_pairwise(self): - check_pairwise_moment(self.frame, "ewm", "cov", span=10, min_periods=5) - - def test_ewmcorr_pairwise(self): - check_pairwise_moment(self.frame, "ewm", "corr", span=10, min_periods=5) + @pytest.mark.parametrize("func", ["cov", "corr"]) + def test_ewm_pairwise_cov_corr(self, func): + check_pairwise_moment(self.frame, "ewm", func, span=10, min_periods=5) @pytest.mark.parametrize("name", ["cov", "corr"]) diff --git a/pandas/tests/window/moments/test_moments_rolling.py b/pandas/tests/window/moments/test_moments_rolling.py index 60aed8565c360..43618d7676731 100644 --- a/pandas/tests/window/moments/test_moments_rolling.py +++ b/pandas/tests/window/moments/test_moments_rolling.py @@ -954,9 +954,6 @@ def test_rolling_cov(self): result = A.rolling(window=50, min_periods=25).cov(B) tm.assert_almost_equal(result[-1], np.cov(A[-50:], B[-50:])[0, 1]) - def test_rolling_cov_pairwise(self): - check_pairwise_moment(self.frame, "rolling", "cov", window=10, min_periods=5) - def test_rolling_corr(self): A = self.series B = A + randn(len(A)) @@ -973,8 +970,9 @@ def test_rolling_corr(self): result = a.rolling(window=len(a), min_periods=1).corr(b) tm.assert_almost_equal(result[-1], a.corr(b)) - def test_rolling_corr_pairwise(self): - check_pairwise_moment(self.frame, "rolling", "corr", window=10, min_periods=5) + @pytest.mark.parametrize("func", ["cov", "corr"]) + def test_rolling_pairwise_cov_corr(self, func): + check_pairwise_moment(self.frame, "rolling", func, window=10, min_periods=5) @pytest.mark.parametrize("method", ["corr", "cov"]) def test_flex_binary_frame(self, method):