From 7e461a18d9f6928132afec6f48ce968b3e989ba6 Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Mon, 3 Dec 2018 17:43:52 +0100 Subject: [PATCH 01/13] 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 02/13] 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 03/13] 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 04/13] 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 f3513b0a125905292c0d7d48452ebfcb601a8324 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Mon, 6 Apr 2020 21:30:39 +0200 Subject: [PATCH 05/13] Add stacked in doc for bar/h --- pandas/plotting/_core.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index e466a215091ea..1c7571808c84f 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -954,6 +954,9 @@ def line(self, x=None, y=None, **kwargs): @Appender( """ + stacked: bool, default is False + If stacked is set to True, stacked bar charts will be plotted. + See Also -------- DataFrame.plot.barh : Horizontal bar plot. @@ -1019,6 +1022,15 @@ def line(self, x=None, y=None, **kwargs): :context: close-figs >>> ax = df.plot.bar(x='lifespan', rot=0) + + Plot stacked bar chars for the DataFrame + + .. plot:: + :context: close-figs + + >>> index = ["a", "b", "c", "d"] + >>> df = pd.DataFrame({"UK": [10, 3, 4], "NL": [1, 5, 6]}, index=index) + >>> ax = df.plot.bar(stacked=True) """ ) @Substitution(kind="bar") @@ -1037,6 +1049,9 @@ def bar(self, x=None, y=None, **kwargs): @Appender( """ + stacked: bool, default is False + If stacked is set to True, stacked bar(h) charts will be plotted. + See Also -------- DataFrame.plot.bar: Vertical bar plot. @@ -1098,6 +1113,15 @@ def bar(self, x=None, y=None, **kwargs): >>> df = pd.DataFrame({'speed': speed, ... 'lifespan': lifespan}, index=index) >>> ax = df.plot.barh(x='lifespan') + + Plot stacked horizontal bar chars for the DataFrame + + .. plot:: + :context: close-figs + + >>> index = ["a", "b", "c", "d"] + >>> df = pd.DataFrame({"UK": [10, 3, 4], "NL": [1, 5, 6]}, index=index) + >>> ax = df.plot.barh(stacked=True) """ ) @Substitution(kind="bar") From aced1835e1c429098c3d454a88220aff2edc0da7 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Mon, 6 Apr 2020 21:33:21 +0200 Subject: [PATCH 06/13] fixup --- pandas/plotting/_core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 1c7571808c84f..d56cdae8c1b12 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -1050,7 +1050,7 @@ def bar(self, x=None, y=None, **kwargs): @Appender( """ stacked: bool, default is False - If stacked is set to True, stacked bar(h) charts will be plotted. + If stacked is set to True, stacked horizontal bar charts will be plotted. See Also -------- From ae796b6e2c56b857c726d817089d618ee1a675a6 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Mon, 6 Apr 2020 21:36:07 +0200 Subject: [PATCH 07/13] fix pep8 --- pandas/plotting/_core.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index d56cdae8c1b12..814b18ca7886a 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -1022,12 +1022,12 @@ def line(self, x=None, y=None, **kwargs): :context: close-figs >>> ax = df.plot.bar(x='lifespan', rot=0) - + Plot stacked bar chars for the DataFrame - + .. plot:: :context: close-figs - + >>> index = ["a", "b", "c", "d"] >>> df = pd.DataFrame({"UK": [10, 3, 4], "NL": [1, 5, 6]}, index=index) >>> ax = df.plot.bar(stacked=True) From 11d7644662d1e411a2f81f9ab250b4b5a0bbf474 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Mon, 6 Apr 2020 22:16:10 +0200 Subject: [PATCH 08/13] better example --- pandas/plotting/_core.py | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 814b18ca7886a..a3579e5435c88 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -988,6 +988,13 @@ def line(self, x=None, y=None, **kwargs): ... 'lifespan': lifespan}, index=index) >>> ax = df.plot.bar(rot=0) + Plot stacked bar chars for the DataFrame + + .. plot:: + :context: close-figs + + >>> ax = df.plot.bar(stacked=True) + Instead of nesting, the figure can be split by column with ``subplots=True``. In this case, a :class:`numpy.ndarray` of :class:`matplotlib.axes.Axes` are returned. @@ -1022,15 +1029,6 @@ def line(self, x=None, y=None, **kwargs): :context: close-figs >>> ax = df.plot.bar(x='lifespan', rot=0) - - Plot stacked bar chars for the DataFrame - - .. plot:: - :context: close-figs - - >>> index = ["a", "b", "c", "d"] - >>> df = pd.DataFrame({"UK": [10, 3, 4], "NL": [1, 5, 6]}, index=index) - >>> ax = df.plot.bar(stacked=True) """ ) @Substitution(kind="bar") @@ -1080,6 +1078,13 @@ def bar(self, x=None, y=None, **kwargs): >>> df = pd.DataFrame({'speed': speed, ... 'lifespan': lifespan}, index=index) >>> ax = df.plot.barh() + + Plot stacked bar chars for the DataFrame + + .. plot:: + :context: close-figs + + >>> ax = df.plot.bar(stacked=True) We can specify colors for each column @@ -1113,15 +1118,6 @@ def bar(self, x=None, y=None, **kwargs): >>> df = pd.DataFrame({'speed': speed, ... 'lifespan': lifespan}, index=index) >>> ax = df.plot.barh(x='lifespan') - - Plot stacked horizontal bar chars for the DataFrame - - .. plot:: - :context: close-figs - - >>> index = ["a", "b", "c", "d"] - >>> df = pd.DataFrame({"UK": [10, 3, 4], "NL": [1, 5, 6]}, index=index) - >>> ax = df.plot.barh(stacked=True) """ ) @Substitution(kind="bar") From d1c5a3ff3acd9731762b162a71f2e5a63324337e Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Mon, 6 Apr 2020 22:42:17 +0200 Subject: [PATCH 09/13] fix linting --- pandas/plotting/_core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index a3579e5435c88..0fef234f41892 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -1078,7 +1078,7 @@ def bar(self, x=None, y=None, **kwargs): >>> df = pd.DataFrame({'speed': speed, ... 'lifespan': lifespan}, index=index) >>> ax = df.plot.barh() - + Plot stacked bar chars for the DataFrame .. plot:: From 8d462f9a449f7fc3b9c3634ba4565eed90d7bee4 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Tue, 7 Apr 2020 22:49:25 +0200 Subject: [PATCH 10/13] spaces --- pandas/plotting/_core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 0fef234f41892..c85f07939148c 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -954,7 +954,7 @@ def line(self, x=None, y=None, **kwargs): @Appender( """ - stacked: bool, default is False + stacked : bool, default False If stacked is set to True, stacked bar charts will be plotted. See Also @@ -1047,7 +1047,7 @@ def bar(self, x=None, y=None, **kwargs): @Appender( """ - stacked: bool, default is False + stacked : bool, default False If stacked is set to True, stacked horizontal bar charts will be plotted. See Also From 0ba8e2f6fa2f7ec8706dd58cd5ccac4b1bf6665f Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Sat, 11 Apr 2020 13:02:56 +0200 Subject: [PATCH 11/13] fix typo --- pandas/plotting/_core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index c85f07939148c..0e46858891030 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -988,7 +988,7 @@ def line(self, x=None, y=None, **kwargs): ... 'lifespan': lifespan}, index=index) >>> ax = df.plot.bar(rot=0) - Plot stacked bar chars for the DataFrame + Plot stacked bar charts for the DataFrame .. plot:: :context: close-figs @@ -1079,7 +1079,7 @@ def bar(self, x=None, y=None, **kwargs): ... 'lifespan': lifespan}, index=index) >>> ax = df.plot.barh() - Plot stacked bar chars for the DataFrame + Plot stacked bar charts for the DataFrame .. plot:: :context: close-figs From 15b4319f9ed3a31e139ed963ea42a83f9303c43b Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Sat, 11 Apr 2020 13:04:20 +0200 Subject: [PATCH 12/13] fix typo --- pandas/plotting/_core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 0e46858891030..6311ddb35c73c 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -1079,12 +1079,12 @@ def bar(self, x=None, y=None, **kwargs): ... 'lifespan': lifespan}, index=index) >>> ax = df.plot.barh() - Plot stacked bar charts for the DataFrame + Plot stacked barh charts for the DataFrame .. plot:: :context: close-figs - >>> ax = df.plot.bar(stacked=True) + >>> ax = df.plot.barh(stacked=True) We can specify colors for each column From c18fc213261d47a52e97ce2e7e00fcb504543692 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Fri, 1 May 2020 20:08:33 +0200 Subject: [PATCH 13/13] remove doc --- pandas/plotting/_core.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 6311ddb35c73c..b3c4d3138e915 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -954,9 +954,6 @@ def line(self, x=None, y=None, **kwargs): @Appender( """ - stacked : bool, default False - If stacked is set to True, stacked bar charts will be plotted. - See Also -------- DataFrame.plot.barh : Horizontal bar plot. @@ -1047,9 +1044,6 @@ def bar(self, x=None, y=None, **kwargs): @Appender( """ - stacked : bool, default False - If stacked is set to True, stacked horizontal bar charts will be plotted. - See Also -------- DataFrame.plot.bar: Vertical bar plot.