From 7e461a18d9f6928132afec6f48ce968b3e989ba6 Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Mon, 3 Dec 2018 17:43:52 +0100 Subject: [PATCH 1/7] 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 4895497ac7dfafa4ab51c870bb2a922f64d1a739 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Sat, 11 Jan 2020 17:32:31 +0100 Subject: [PATCH 2/7] correct error message --- pandas/core/reshape/pivot.py | 3 +++ pandas/tests/reshape/test_pivot.py | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index b443ba142369c..40756466c277f 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -427,6 +427,9 @@ def _convert_by(by): @Substitution("\ndata : DataFrame") @Appender(_shared_docs["pivot"], indents=1) def pivot(data: "DataFrame", index=None, columns=None, values=None) -> "DataFrame": + if columns is None: + raise ValueError("`columns` is not optional.") + if values is None: cols = [columns] if index is None else [index, columns] append = index is None diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 743fc50c87e96..ed4db1ff92f2f 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -781,6 +781,15 @@ def test_pivot_with_list_like_values_nans(self, values, method): expected = DataFrame(data=data, index=index, columns=columns, dtype="object") tm.assert_frame_equal(result, expected) + def test_pivot_columns_none_raise_error(self): + # GH 30924 + df = pd.DataFrame( + {"col1": ["a", "b", "c"], "col2": [1, 2, 3], "col3": [1, 2, 3]} + ) + msg = "`columns` is not optional." + with pytest.raises(ValueError, match=msg): + df.pivot(index="col1", values="col3") + @pytest.mark.xfail( reason="MultiIndexed unstack with tuple names fails with KeyError GH#19966" ) From f4380cda9ee4120c82ce64df93fbcbffe3d4a526 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Mon, 13 Jan 2020 18:05:33 +0100 Subject: [PATCH 3/7] code change on Simon review --- pandas/core/reshape/pivot.py | 2 +- pandas/tests/reshape/test_pivot.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index 40756466c277f..53bfcd5c5827e 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -428,7 +428,7 @@ def _convert_by(by): @Appender(_shared_docs["pivot"], indents=1) def pivot(data: "DataFrame", index=None, columns=None, values=None) -> "DataFrame": if columns is None: - raise ValueError("`columns` is not optional.") + raise ValueError("pivot() missing 1 required argument: 'columns'") if values is None: cols = [columns] if index is None else [index, columns] diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index ed4db1ff92f2f..6320f093a9c96 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -786,7 +786,7 @@ def test_pivot_columns_none_raise_error(self): df = pd.DataFrame( {"col1": ["a", "b", "c"], "col2": [1, 2, 3], "col3": [1, 2, 3]} ) - msg = "`columns` is not optional." + msg = 'pivot["(][")] missing 1 required argument: ["\']columns["\']' with pytest.raises(ValueError, match=msg): df.pivot(index="col1", values="col3") From e60ffee9721c96c2e8fad8d901dbfca72df1e6ce Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Mon, 13 Jan 2020 19:03:55 +0100 Subject: [PATCH 4/7] update test --- pandas/tests/reshape/test_pivot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 6320f093a9c96..1b19a61a1e973 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -1,5 +1,6 @@ from datetime import date, datetime, timedelta from itertools import product +import re import numpy as np import pytest @@ -786,7 +787,7 @@ def test_pivot_columns_none_raise_error(self): df = pd.DataFrame( {"col1": ["a", "b", "c"], "col2": [1, 2, 3], "col3": [1, 2, 3]} ) - msg = 'pivot["(][")] missing 1 required argument: ["\']columns["\']' + msg = re.escape("pivot() missing 1 required argument: 'columns'") with pytest.raises(ValueError, match=msg): df.pivot(index="col1", values="col3") From d03452f91d176ca20c7588e45ba7a4306ab50987 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Mon, 13 Jan 2020 22:22:00 +0100 Subject: [PATCH 5/7] add whatsnew --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index c3ee72f6442fc..e9fd813eeae94 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -139,7 +139,7 @@ Groupby/resample/rolling Reshaping ^^^^^^^^^ -- +- Correct incorrect error message in :meth:`DataFrame.pivot` when ``columns`` is set to ``None``. (:issue:`30924`) - Sparse From c650a052f61212a866457487028ae6c7dae675b5 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Sat, 18 Jan 2020 11:33:05 +0100 Subject: [PATCH 6/7] better whatsnew --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index fc7519d3b57f8..203d2d5ec0f40 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -142,7 +142,7 @@ Reshaping - - Bug in :meth:`DataFrame.pivot_table` when only MultiIndexed columns is set (:issue:`17038`) -- Correct incorrect error message in :meth:`DataFrame.pivot` when ``columns`` is set to ``None``. (:issue:`30924`) +- Fix incorrect error message in :meth:`DataFrame.pivot` when ``columns`` is set to ``None``. (:issue:`30924`) - Bug in :func:`crosstab` when inputs are two Series and have tuple names, the output will keep dummy MultiIndex as columns. (:issue:`18321`) From 16478e54b3bfafcde6df62a9ccbe96a7d54402bd Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Sat, 18 Jan 2020 11:35:40 +0100 Subject: [PATCH 7/7] remove re --- pandas/tests/reshape/test_pivot.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index 1633524f32dfd..44073f56abfa1 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -1,6 +1,5 @@ from datetime import date, datetime, timedelta from itertools import product -import re import numpy as np import pytest @@ -787,7 +786,7 @@ def test_pivot_columns_none_raise_error(self): df = pd.DataFrame( {"col1": ["a", "b", "c"], "col2": [1, 2, 3], "col3": [1, 2, 3]} ) - msg = re.escape("pivot() missing 1 required argument: 'columns'") + msg = r"pivot\(\) missing 1 required argument: 'columns'" with pytest.raises(TypeError, match=msg): df.pivot(index="col1", values="col3")