From 7e461a18d9f6928132afec6f48ce968b3e989ba6 Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Mon, 3 Dec 2018 17:43:52 +0100 Subject: [PATCH 1/3] 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 203b856585808935186d7574153b8ec52d05f6bf Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Fri, 3 Jan 2020 18:52:08 +0100 Subject: [PATCH 2/3] follow up to clean tests --- .../frame/methods/test_drop_duplicates.py | 22 +++----- pandas/tests/frame/methods/test_sort_index.py | 51 +++++++++++-------- .../tests/frame/methods/test_sort_values.py | 23 ++++----- .../tests/series/methods/test_sort_index.py | 30 +++++------ .../tests/series/methods/test_sort_values.py | 24 ++++----- 5 files changed, 72 insertions(+), 78 deletions(-) diff --git a/pandas/tests/frame/methods/test_drop_duplicates.py b/pandas/tests/frame/methods/test_drop_duplicates.py index 29ab2e1bfd512..0856ed6885978 100644 --- a/pandas/tests/frame/methods/test_drop_duplicates.py +++ b/pandas/tests/frame/methods/test_drop_duplicates.py @@ -393,6 +393,7 @@ def test_drop_duplicates_inplace(): tm.assert_frame_equal(result, expected) +@pytest.mark.parametrize("inplace", [True, False]) @pytest.mark.parametrize( "origin_dict, output_dict, ignore_index, output_index", [ @@ -403,24 +404,17 @@ def test_drop_duplicates_inplace(): ], ) def test_drop_duplicates_ignore_index( - origin_dict, output_dict, ignore_index, output_index + inplace, origin_dict, output_dict, ignore_index, output_index ): # GH 30114 df = DataFrame(origin_dict) expected = DataFrame(output_dict, index=output_index) - # Test when inplace is False - result = df.drop_duplicates(ignore_index=ignore_index) - tm.assert_frame_equal(result, expected) - - # to verify original dataframe is not mutated - tm.assert_frame_equal(df, DataFrame(origin_dict)) - - # Test when inplace is True - copied_df = df.copy() - - copied_df.drop_duplicates(ignore_index=ignore_index, inplace=True) - tm.assert_frame_equal(copied_df, expected) + if inplace: + result_df = df.copy() + result_df.drop_duplicates(ignore_index=ignore_index, inplace=inplace) + else: + result_df = df.drop_duplicates(ignore_index=ignore_index, inplace=inplace) - # to verify that input is unchanged + tm.assert_frame_equal(result_df, expected) tm.assert_frame_equal(df, DataFrame(origin_dict)) diff --git a/pandas/tests/frame/methods/test_sort_index.py b/pandas/tests/frame/methods/test_sort_index.py index 6866aab11d2fa..29a52de66e100 100644 --- a/pandas/tests/frame/methods/test_sort_index.py +++ b/pandas/tests/frame/methods/test_sort_index.py @@ -230,6 +230,7 @@ def test_sort_index_intervalindex(self): result = result.columns.levels[1].categories tm.assert_index_equal(result, expected) + @pytest.mark.parametrize("inplace", [True, False]) @pytest.mark.parametrize( "original_dict, sorted_dict, ascending, ignore_index, output_index", [ @@ -240,25 +241,28 @@ def test_sort_index_intervalindex(self): ], ) def test_sort_index_ignore_index( - self, original_dict, sorted_dict, ascending, ignore_index, output_index + self, inplace, original_dict, sorted_dict, ascending, ignore_index, output_index ): # GH 30114 original_index = [2, 5, 3] df = DataFrame(original_dict, index=original_index) expected_df = DataFrame(sorted_dict, index=output_index) - - sorted_df = df.sort_index(ascending=ascending, ignore_index=ignore_index) - tm.assert_frame_equal(sorted_df, expected_df) - tm.assert_frame_equal(df, DataFrame(original_dict, index=original_index)) - - # Test when inplace is True - copied_df = df.copy() - copied_df.sort_index( - ascending=ascending, ignore_index=ignore_index, inplace=True - ) - tm.assert_frame_equal(copied_df, expected_df) + kwargs = { + "ascending": ascending, + "ignore_index": ignore_index, + "inplace": inplace, + } + + if inplace: + result_df = df.copy() + result_df.sort_index(**kwargs) + else: + result_df = df.sort_index(**kwargs) + + tm.assert_frame_equal(result_df, expected_df) tm.assert_frame_equal(df, DataFrame(original_dict, index=original_index)) + @pytest.mark.parametrize("inplace", [True, False]) @pytest.mark.parametrize( "original_dict, sorted_dict, ascending, ignore_index, output_index", [ @@ -293,21 +297,24 @@ def test_sort_index_ignore_index( ], ) def test_sort_index_ignore_index_multi_index( - self, original_dict, sorted_dict, ascending, ignore_index, output_index + self, inplace, original_dict, sorted_dict, ascending, ignore_index, output_index ): # GH 30114, this is to test ignore_index on MulitIndex of index mi = MultiIndex.from_tuples([[2, 1], [3, 4]], names=list("AB")) df = DataFrame(original_dict, index=mi) expected_df = DataFrame(sorted_dict, index=output_index) - sorted_df = df.sort_index(ascending=ascending, ignore_index=ignore_index) - tm.assert_frame_equal(sorted_df, expected_df) - tm.assert_frame_equal(df, DataFrame(original_dict, index=mi)) + kwargs = { + "ascending": ascending, + "ignore_index": ignore_index, + "inplace": inplace, + } - # Test when inplace is True - copied_df = df.copy() - copied_df.sort_index( - ascending=ascending, ignore_index=ignore_index, inplace=True - ) - tm.assert_frame_equal(copied_df, expected_df) + if inplace: + result_df = df.copy() + result_df.sort_index(**kwargs) + else: + result_df = df.sort_index(**kwargs) + + tm.assert_frame_equal(result_df, expected_df) tm.assert_frame_equal(df, DataFrame(original_dict, index=mi)) diff --git a/pandas/tests/frame/methods/test_sort_values.py b/pandas/tests/frame/methods/test_sort_values.py index e733c01e01740..ecf2fcf90dd2d 100644 --- a/pandas/tests/frame/methods/test_sort_values.py +++ b/pandas/tests/frame/methods/test_sort_values.py @@ -461,6 +461,7 @@ def test_sort_values_na_position_with_categories_raises(self): with pytest.raises(ValueError): df.sort_values(by="c", ascending=False, na_position="bad_position") + @pytest.mark.parametrize("inplace", [True, False]) @pytest.mark.parametrize( "original_dict, sorted_dict, ignore_index, output_index", [ @@ -481,24 +482,18 @@ def test_sort_values_na_position_with_categories_raises(self): ], ) def test_sort_values_ignore_index( - self, original_dict, sorted_dict, ignore_index, output_index + self, inplace, original_dict, sorted_dict, ignore_index, output_index ): # GH 30114 df = DataFrame(original_dict) expected = DataFrame(sorted_dict, index=output_index) + kwargs = {"ignore_index": ignore_index, "inplace": inplace} - # Test when inplace is False - sorted_df = df.sort_values("A", ascending=False, ignore_index=ignore_index) - tm.assert_frame_equal(sorted_df, expected) - - tm.assert_frame_equal(df, DataFrame(original_dict)) - - # Test when inplace is True - copied_df = df.copy() - - copied_df.sort_values( - "A", ascending=False, ignore_index=ignore_index, inplace=True - ) - tm.assert_frame_equal(copied_df, expected) + if inplace: + result_df = df.copy() + result_df.sort_values("A", ascending=False, **kwargs) + else: + result_df = df.sort_values("A", ascending=False, **kwargs) + tm.assert_frame_equal(result_df, expected) tm.assert_frame_equal(df, DataFrame(original_dict)) diff --git a/pandas/tests/series/methods/test_sort_index.py b/pandas/tests/series/methods/test_sort_index.py index a9b73c2344681..0dd8da0ce9af8 100644 --- a/pandas/tests/series/methods/test_sort_index.py +++ b/pandas/tests/series/methods/test_sort_index.py @@ -136,6 +136,7 @@ def test_sort_index_intervals(self): ) tm.assert_series_equal(result, expected) + @pytest.mark.parametrize("inplace", [True, False]) @pytest.mark.parametrize( "original_list, sorted_list, ascending, ignore_index, output_index", [ @@ -146,23 +147,22 @@ def test_sort_index_intervals(self): ], ) def test_sort_index_ignore_index( - self, original_list, sorted_list, ascending, ignore_index, output_index + self, inplace, original_list, sorted_list, ascending, ignore_index, output_index ): # GH 30114 ser = Series(original_list) expected = Series(sorted_list, index=output_index) - - # Test when inplace is False - sorted_sr = ser.sort_index(ascending=ascending, ignore_index=ignore_index) - tm.assert_series_equal(sorted_sr, expected) - - tm.assert_series_equal(ser, Series(original_list)) - - # Test when inplace is True - copied_sr = ser.copy() - copied_sr.sort_index( - ascending=ascending, ignore_index=ignore_index, inplace=True - ) - tm.assert_series_equal(copied_sr, expected) - + kwargs = { + "ascending": ascending, + "ignore_index": ignore_index, + "inplace": inplace, + } + + if inplace: + result_df = ser.copy() + result_df.sort_index(**kwargs) + else: + result_df = ser.sort_index(**kwargs) + + tm.assert_series_equal(result_df, expected) tm.assert_series_equal(ser, Series(original_list)) diff --git a/pandas/tests/series/methods/test_sort_values.py b/pandas/tests/series/methods/test_sort_values.py index 2cea6f061de76..6dc63986ef144 100644 --- a/pandas/tests/series/methods/test_sort_values.py +++ b/pandas/tests/series/methods/test_sort_values.py @@ -157,6 +157,7 @@ def test_sort_values_categorical(self): expected = df.iloc[[2, 1, 5, 4, 3, 0]] tm.assert_frame_equal(result, expected) + @pytest.mark.parametrize("inplace", [True, False]) @pytest.mark.parametrize( "original_list, sorted_list, ignore_index, output_index", [ @@ -165,21 +166,18 @@ def test_sort_values_categorical(self): ], ) def test_sort_values_ignore_index( - self, original_list, sorted_list, ignore_index, output_index + self, inplace, original_list, sorted_list, ignore_index, output_index ): # GH 30114 - sr = Series(original_list) + ser = Series(original_list) expected = Series(sorted_list, index=output_index) + kwargs = {"ignore_index": ignore_index, "inplace": inplace} - # Test when inplace is False - sorted_sr = sr.sort_values(ascending=False, ignore_index=ignore_index) - tm.assert_series_equal(sorted_sr, expected) + if inplace: + result_ser = ser.copy() + result_ser.sort_values(ascending=False, **kwargs) + else: + result_ser = ser.sort_values(ascending=False, **kwargs) - tm.assert_series_equal(sr, Series(original_list)) - - # Test when inplace is True - copied_sr = sr.copy() - copied_sr.sort_values(ascending=False, ignore_index=ignore_index, inplace=True) - tm.assert_series_equal(copied_sr, expected) - - tm.assert_series_equal(sr, Series(original_list)) + tm.assert_series_equal(result_ser, expected) + tm.assert_series_equal(ser, Series(original_list)) From 3853a7f36d297e6b4c689e1d4fd448735d5d6241 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Fri, 3 Jan 2020 18:56:51 +0100 Subject: [PATCH 3/3] Correct names --- pandas/tests/series/methods/test_sort_index.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/series/methods/test_sort_index.py b/pandas/tests/series/methods/test_sort_index.py index 0dd8da0ce9af8..5d47c54c9c336 100644 --- a/pandas/tests/series/methods/test_sort_index.py +++ b/pandas/tests/series/methods/test_sort_index.py @@ -159,10 +159,10 @@ def test_sort_index_ignore_index( } if inplace: - result_df = ser.copy() - result_df.sort_index(**kwargs) + result_ser = ser.copy() + result_ser.sort_index(**kwargs) else: - result_df = ser.sort_index(**kwargs) + result_ser = ser.sort_index(**kwargs) - tm.assert_series_equal(result_df, expected) + tm.assert_series_equal(result_ser, expected) tm.assert_series_equal(ser, Series(original_list))