From 28ae2aed39059ac942119c34b36a35f5397fdd7c Mon Sep 17 00:00:00 2001 From: KD-dev-lab Date: Thu, 4 Jun 2020 23:49:45 +0200 Subject: [PATCH 1/9] TST: Added tests to check the arithmetic behaviour of adding and subtracting a series of DateOffsets to / from a series of Timestamps (test_series_add_daytime_offset, test_series_add_daytime_offset) --- pandas/tests/series/test_arithmetic.py | 33 ++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index 5c8a0d224c4f9..47e8ca4006a24 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -11,6 +11,7 @@ from pandas import Categorical, Index, Series, bdate_range, date_range, isna import pandas._testing as tm from pandas.core import nanops, ops +import warnings def _permute(obj): @@ -679,6 +680,38 @@ def test_series_add_aware_naive_raises(self): with pytest.raises(Exception, match=msg): ser_utc + ser + def test_series_add_daytime_offset(self): + # GH#19211 + # Ignore PerformanceWarning for this test case + warnings.simplefilter(action="ignore", category=pd.errors.PerformanceWarning) + ser_daytime = pd.Series( + [pd.Timestamp("2000-01-01"), pd.Timestamp("2000-02-01")] + ) + ser_offset = pd.Series( + [pd.offsets.DateOffset(years=1), pd.offsets.DateOffset(months=2)] + ) + + result = ser_daytime + ser_offset + + expected = pd.Series([pd.Timestamp("2001-01-01"), pd.Timestamp("2000-04-01")]) + tm.assert_series_equal(result, expected) + + def test_series_sub_daytime_offset(self): + # GH#19211 + # Ignore PerformanceWarning for this test case + warnings.simplefilter(action="ignore", category=pd.errors.PerformanceWarning) + ser_daytime = pd.Series( + [pd.Timestamp("2000-01-01"), pd.Timestamp("2000-03-29")] + ) + ser_offset = pd.Series( + [pd.offsets.DateOffset(years=1), pd.offsets.DateOffset(months=2)] + ) + + result = ser_daytime - ser_offset + + expected = pd.Series([pd.Timestamp("1999-1-1"), pd.Timestamp("2000-1-29")]) + tm.assert_series_equal(result, expected) + def test_datetime_understood(self): # Ensures it doesn't fail to create the right series # reported in issue#16726 From b9b04e64c7603da7a3f22e371078effc8758972c Mon Sep 17 00:00:00 2001 From: KD-dev-lab Date: Fri, 5 Jun 2020 00:24:27 +0200 Subject: [PATCH 2/9] TST: Run isort for test_arithmetic.py --- pandas/tests/series/test_arithmetic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index 47e8ca4006a24..1ad0caa42e2e1 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -1,5 +1,6 @@ from datetime import timedelta import operator +import warnings import numpy as np import pytest @@ -11,7 +12,6 @@ from pandas import Categorical, Index, Series, bdate_range, date_range, isna import pandas._testing as tm from pandas.core import nanops, ops -import warnings def _permute(obj): From 5f8aaed142a09701ab317dc128d957e29c871877 Mon Sep 17 00:00:00 2001 From: KD-dev-lab Date: Sat, 6 Jun 2020 11:32:33 +0200 Subject: [PATCH 3/9] TST: Reworked tests for GH#19211 after review and added several tests considering Index and DaytimeIndex. Included pd.offsets.MonthEnd() and pd.offsets.MonthStart() in offset Series. --- pandas/tests/series/test_arithmetic.py | 172 +++++++++++++++++++++++-- 1 file changed, 163 insertions(+), 9 deletions(-) diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index 1ad0caa42e2e1..61e2976306be2 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -596,7 +596,6 @@ def test_comp_ops_df_compat(self): s4 = pd.Series([2, 2, 2, 2], index=list("ABCD"), name="x") for left, right in [(s1, s2), (s2, s1), (s3, s4), (s4, s3)]: - msg = "Can only compare identically-labeled Series objects" with pytest.raises(ValueError, match=msg): left == right @@ -685,31 +684,186 @@ def test_series_add_daytime_offset(self): # Ignore PerformanceWarning for this test case warnings.simplefilter(action="ignore", category=pd.errors.PerformanceWarning) ser_daytime = pd.Series( - [pd.Timestamp("2000-01-01"), pd.Timestamp("2000-02-01")] + [ + pd.Timestamp("2000-01-01"), + pd.Timestamp("2000-02-01"), + pd.Timestamp("2000-05-01"), + ] ) ser_offset = pd.Series( - [pd.offsets.DateOffset(years=1), pd.offsets.DateOffset(months=2)] + [ + pd.offsets.DateOffset(years=1), + pd.offsets.DateOffset(months=2), + pd.offsets.MonthEnd(), + ] ) - result = ser_daytime + ser_offset + result1 = ser_offset + ser_daytime + result2 = ser_daytime + ser_offset - expected = pd.Series([pd.Timestamp("2001-01-01"), pd.Timestamp("2000-04-01")]) - tm.assert_series_equal(result, expected) + expected = pd.Series( + [ + pd.Timestamp("2001-01-01"), + pd.Timestamp("2000-04-01"), + pd.Timestamp("2000-05-31"), + ] + ) + + tm.assert_series_equal(result1, expected) + tm.assert_series_equal(result2, expected) + + def test_series_add_daytime_offset_index(self): + # GH#19211 + # Ignore PerformanceWarning for this test case + warnings.simplefilter(action="ignore", category=pd.errors.PerformanceWarning) + ser_daytime = pd.Series( + [ + pd.Timestamp("2000-01-01"), + pd.Timestamp("2000-02-01"), + pd.Timestamp("2000-05-01"), + ] + ) + ser_offset = pd.Series( + [ + pd.offsets.DateOffset(years=1), + pd.offsets.DateOffset(months=2), + pd.offsets.MonthEnd(), + ] + ) + + result1 = pd.Index(ser_offset) + ser_daytime + result2 = ser_daytime + pd.Index(ser_offset) + + expected = pd.Series( + [ + pd.Timestamp("2001-01-01"), + pd.Timestamp("2000-04-01"), + pd.Timestamp("2000-05-31"), + ] + ) + tm.assert_series_equal(result1, expected) + tm.assert_series_equal(result2, expected) + + def test_series_add_daytime_index_offset(self): + # GH#19211 + # Ignore PerformanceWarning for this test case + warnings.simplefilter(action="ignore", category=pd.errors.PerformanceWarning) + ser_daytime = pd.Series( + [ + pd.Timestamp("2000-01-01"), + pd.Timestamp("2000-02-01"), + pd.Timestamp("2000-05-01"), + ] + ) + ser_offset = pd.Series( + [ + pd.offsets.DateOffset(years=1), + pd.offsets.DateOffset(months=2), + pd.offsets.MonthEnd(), + ] + ) + + result1 = ser_offset + pd.DatetimeIndex(ser_daytime) + result2 = pd.DatetimeIndex(ser_daytime) + ser_offset + + expected = pd.Series( + [ + pd.Timestamp("2001-01-01"), + pd.Timestamp("2000-04-01"), + pd.Timestamp("2000-05-31"), + ] + ) + tm.assert_series_equal(result1, expected) + tm.assert_series_equal(result2, expected) def test_series_sub_daytime_offset(self): # GH#19211 # Ignore PerformanceWarning for this test case warnings.simplefilter(action="ignore", category=pd.errors.PerformanceWarning) ser_daytime = pd.Series( - [pd.Timestamp("2000-01-01"), pd.Timestamp("2000-03-29")] + [ + pd.Timestamp("2000-01-01"), + pd.Timestamp("2000-03-29"), + pd.Timestamp("2000-05-15"), + ] ) ser_offset = pd.Series( - [pd.offsets.DateOffset(years=1), pd.offsets.DateOffset(months=2)] + [ + pd.offsets.DateOffset(years=1), + pd.offsets.DateOffset(months=2), + pd.offsets.MonthBegin(), + ] ) result = ser_daytime - ser_offset - expected = pd.Series([pd.Timestamp("1999-1-1"), pd.Timestamp("2000-1-29")]) + expected = pd.Series( + [ + pd.Timestamp("1999-1-1"), + pd.Timestamp("2000-1-29"), + pd.Timestamp("2000-05-01"), + ] + ) + tm.assert_series_equal(result, expected) + + def test_series_sub_daytime_offset_index(self): + # GH#19211 + # Ignore PerformanceWarning for this test case + warnings.simplefilter(action="ignore", category=pd.errors.PerformanceWarning) + ser_daytime = pd.Series( + [ + pd.Timestamp("2000-01-01"), + pd.Timestamp("2000-03-29"), + pd.Timestamp("2000-05-15"), + ] + ) + ser_offset = pd.Series( + [ + pd.offsets.DateOffset(years=1), + pd.offsets.DateOffset(months=2), + pd.offsets.MonthBegin(), + ] + ) + + result = ser_daytime - pd.Index(ser_offset) + + expected = pd.Series( + [ + pd.Timestamp("1999-1-1"), + pd.Timestamp("2000-1-29"), + pd.Timestamp("2000-05-01"), + ] + ) + tm.assert_series_equal(result, expected) + + def test_series_sub_daytime_index_offset(self): + # GH#19211 + # Ignore PerformanceWarning for this test case + warnings.simplefilter(action="ignore", category=pd.errors.PerformanceWarning) + ser_daytime = pd.Series( + [ + pd.Timestamp("2000-01-01"), + pd.Timestamp("2000-03-29"), + pd.Timestamp("2000-05-15"), + ] + ) + ser_offset = pd.Series( + [ + pd.offsets.DateOffset(years=1), + pd.offsets.DateOffset(months=2), + pd.offsets.MonthBegin(), + ] + ) + + result = pd.DatetimeIndex(ser_daytime) - ser_offset + + expected = pd.Series( + [ + pd.Timestamp("1999-1-1"), + pd.Timestamp("2000-1-29"), + pd.Timestamp("2000-05-01"), + ] + ) tm.assert_series_equal(result, expected) def test_datetime_understood(self): From aace93ca1868e3a2a87a4ec706e213c09897ac88 Mon Sep 17 00:00:00 2001 From: KD-dev-lab Date: Sat, 6 Jun 2020 17:19:22 +0200 Subject: [PATCH 4/9] TST: Reworked tests for GH#19211 after review. Replaced warnings.simplefilter with @pytest.mark.filterwarnings. --- pandas/tests/series/test_arithmetic.py | 31 +++++++++++++++----------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index 61e2976306be2..51decd86fcc9a 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -1,6 +1,5 @@ from datetime import timedelta import operator -import warnings import numpy as np import pytest @@ -679,10 +678,11 @@ def test_series_add_aware_naive_raises(self): with pytest.raises(Exception, match=msg): ser_utc + ser + @pytest.mark.filterwarnings( + "ignore:Adding/subtracting object-dtype array to DatetimeArray" + ) def test_series_add_daytime_offset(self): # GH#19211 - # Ignore PerformanceWarning for this test case - warnings.simplefilter(action="ignore", category=pd.errors.PerformanceWarning) ser_daytime = pd.Series( [ pd.Timestamp("2000-01-01"), @@ -712,10 +712,11 @@ def test_series_add_daytime_offset(self): tm.assert_series_equal(result1, expected) tm.assert_series_equal(result2, expected) + @pytest.mark.filterwarnings( + "ignore:Adding/subtracting object-dtype array to DatetimeArray" + ) def test_series_add_daytime_offset_index(self): # GH#19211 - # Ignore PerformanceWarning for this test case - warnings.simplefilter(action="ignore", category=pd.errors.PerformanceWarning) ser_daytime = pd.Series( [ pd.Timestamp("2000-01-01"), @@ -744,10 +745,11 @@ def test_series_add_daytime_offset_index(self): tm.assert_series_equal(result1, expected) tm.assert_series_equal(result2, expected) + @pytest.mark.filterwarnings( + "ignore:Adding/subtracting object-dtype array to DatetimeArray" + ) def test_series_add_daytime_index_offset(self): # GH#19211 - # Ignore PerformanceWarning for this test case - warnings.simplefilter(action="ignore", category=pd.errors.PerformanceWarning) ser_daytime = pd.Series( [ pd.Timestamp("2000-01-01"), @@ -776,10 +778,11 @@ def test_series_add_daytime_index_offset(self): tm.assert_series_equal(result1, expected) tm.assert_series_equal(result2, expected) + @pytest.mark.filterwarnings( + "ignore:Adding/subtracting object-dtype array to DatetimeArray" + ) def test_series_sub_daytime_offset(self): # GH#19211 - # Ignore PerformanceWarning for this test case - warnings.simplefilter(action="ignore", category=pd.errors.PerformanceWarning) ser_daytime = pd.Series( [ pd.Timestamp("2000-01-01"), @@ -806,10 +809,11 @@ def test_series_sub_daytime_offset(self): ) tm.assert_series_equal(result, expected) + @pytest.mark.filterwarnings( + "ignore:Adding/subtracting object-dtype array to DatetimeArray" + ) def test_series_sub_daytime_offset_index(self): # GH#19211 - # Ignore PerformanceWarning for this test case - warnings.simplefilter(action="ignore", category=pd.errors.PerformanceWarning) ser_daytime = pd.Series( [ pd.Timestamp("2000-01-01"), @@ -836,10 +840,11 @@ def test_series_sub_daytime_offset_index(self): ) tm.assert_series_equal(result, expected) + @pytest.mark.filterwarnings( + "ignore:Adding/subtracting object-dtype array to DatetimeArray" + ) def test_series_sub_daytime_index_offset(self): # GH#19211 - # Ignore PerformanceWarning for this test case - warnings.simplefilter(action="ignore", category=pd.errors.PerformanceWarning) ser_daytime = pd.Series( [ pd.Timestamp("2000-01-01"), From 6c81b5ba40aec34477dd090c14384fa3c64443fd Mon Sep 17 00:00:00 2001 From: KD-dev-lab Date: Sat, 6 Jun 2020 17:28:41 +0200 Subject: [PATCH 5/9] TST: Restored accidentally deleted blank line --- pandas/tests/series/test_arithmetic.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index 51decd86fcc9a..ca04b990e6b39 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -595,6 +595,7 @@ def test_comp_ops_df_compat(self): s4 = pd.Series([2, 2, 2, 2], index=list("ABCD"), name="x") for left, right in [(s1, s2), (s2, s1), (s3, s4), (s4, s3)]: + msg = "Can only compare identically-labeled Series objects" with pytest.raises(ValueError, match=msg): left == right From f40b14343e1dc3daff79ab9cc48262bb1d1fce5b Mon Sep 17 00:00:00 2001 From: KD-dev-lab Date: Sun, 7 Jun 2020 19:30:41 +0200 Subject: [PATCH 6/9] TST: Parametrized tests to avoid code duplication. --- pandas/tests/series/test_arithmetic.py | 216 +++++++------------------ 1 file changed, 61 insertions(+), 155 deletions(-) diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index ca04b990e6b39..5ab571d1e8cad 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -595,7 +595,6 @@ def test_comp_ops_df_compat(self): s4 = pd.Series([2, 2, 2, 2], index=list("ABCD"), name="x") for left, right in [(s1, s2), (s2, s1), (s3, s4), (s4, s3)]: - msg = "Can only compare identically-labeled Series objects" with pytest.raises(ValueError, match=msg): left == right @@ -682,195 +681,102 @@ def test_series_add_aware_naive_raises(self): @pytest.mark.filterwarnings( "ignore:Adding/subtracting object-dtype array to DatetimeArray" ) - def test_series_add_daytime_offset(self): - # GH#19211 - ser_daytime = pd.Series( + @pytest.mark.parametrize( + "daytime", + [ [ pd.Timestamp("2000-01-01"), pd.Timestamp("2000-02-01"), pd.Timestamp("2000-05-01"), ] - ) - ser_offset = pd.Series( - [ - pd.offsets.DateOffset(years=1), - pd.offsets.DateOffset(months=2), - pd.offsets.MonthEnd(), - ] - ) - - result1 = ser_offset + ser_daytime - result2 = ser_daytime + ser_offset - - expected = pd.Series( - [ - pd.Timestamp("2001-01-01"), - pd.Timestamp("2000-04-01"), - pd.Timestamp("2000-05-31"), - ] - ) - - tm.assert_series_equal(result1, expected) - tm.assert_series_equal(result2, expected) - - @pytest.mark.filterwarnings( - "ignore:Adding/subtracting object-dtype array to DatetimeArray" + ], ) - def test_series_add_daytime_offset_index(self): - # GH#19211 - ser_daytime = pd.Series( - [ - pd.Timestamp("2000-01-01"), - pd.Timestamp("2000-02-01"), - pd.Timestamp("2000-05-01"), - ] - ) - ser_offset = pd.Series( + @pytest.mark.parametrize( + "offset", + [ [ pd.offsets.DateOffset(years=1), pd.offsets.DateOffset(months=2), pd.offsets.MonthEnd(), ] - ) - - result1 = pd.Index(ser_offset) + ser_daytime - result2 = ser_daytime + pd.Index(ser_offset) - - expected = pd.Series( - [ - pd.Timestamp("2001-01-01"), - pd.Timestamp("2000-04-01"), - pd.Timestamp("2000-05-31"), - ] - ) - tm.assert_series_equal(result1, expected) - tm.assert_series_equal(result2, expected) - - @pytest.mark.filterwarnings( - "ignore:Adding/subtracting object-dtype array to DatetimeArray" + ], ) - def test_series_add_daytime_index_offset(self): - # GH#19211 - ser_daytime = pd.Series( - [ - pd.Timestamp("2000-01-01"), - pd.Timestamp("2000-02-01"), - pd.Timestamp("2000-05-01"), - ] - ) - ser_offset = pd.Series( - [ - pd.offsets.DateOffset(years=1), - pd.offsets.DateOffset(months=2), - pd.offsets.MonthEnd(), - ] - ) - - result1 = ser_offset + pd.DatetimeIndex(ser_daytime) - result2 = pd.DatetimeIndex(ser_daytime) + ser_offset - - expected = pd.Series( + @pytest.mark.parametrize( + "expected", + [ [ pd.Timestamp("2001-01-01"), pd.Timestamp("2000-04-01"), pd.Timestamp("2000-05-31"), ] - ) - tm.assert_series_equal(result1, expected) - tm.assert_series_equal(result2, expected) - - @pytest.mark.filterwarnings( - "ignore:Adding/subtracting object-dtype array to DatetimeArray" + ], + ) + @pytest.mark.parametrize( + "constr_daytime, constr_offset, constr_expected", + [ + (pd.Series, pd.Series, pd.Series), + (pd.Series, pd.Index, pd.Series), + (pd.DatetimeIndex, pd.Series, pd.Series), + ], ) - def test_series_sub_daytime_offset(self): + def test_add_daytime_offset( + self, daytime, offset, expected, constr_daytime, constr_offset, constr_expected + ): # GH#19211 - ser_daytime = pd.Series( - [ - pd.Timestamp("2000-01-01"), - pd.Timestamp("2000-03-29"), - pd.Timestamp("2000-05-15"), - ] - ) - ser_offset = pd.Series( - [ - pd.offsets.DateOffset(years=1), - pd.offsets.DateOffset(months=2), - pd.offsets.MonthBegin(), - ] - ) - - result = ser_daytime - ser_offset - - expected = pd.Series( - [ - pd.Timestamp("1999-1-1"), - pd.Timestamp("2000-1-29"), - pd.Timestamp("2000-05-01"), - ] - ) - tm.assert_series_equal(result, expected) + result1 = constr_offset(offset) + constr_daytime(daytime) + result2 = constr_daytime(daytime) + constr_offset(offset) + tm.assert_series_equal(result1, constr_expected(expected)) + tm.assert_series_equal(result2, constr_expected(expected)) @pytest.mark.filterwarnings( "ignore:Adding/subtracting object-dtype array to DatetimeArray" ) - def test_series_sub_daytime_offset_index(self): - # GH#19211 - ser_daytime = pd.Series( + @pytest.mark.parametrize( + "daytime", + [ [ pd.Timestamp("2000-01-01"), pd.Timestamp("2000-03-29"), pd.Timestamp("2000-05-15"), ] - ) - ser_offset = pd.Series( + ], + ) + @pytest.mark.parametrize( + "offset", + [ [ pd.offsets.DateOffset(years=1), pd.offsets.DateOffset(months=2), pd.offsets.MonthBegin(), ] - ) - - result = ser_daytime - pd.Index(ser_offset) - - expected = pd.Series( - [ - pd.Timestamp("1999-1-1"), - pd.Timestamp("2000-1-29"), - pd.Timestamp("2000-05-01"), - ] - ) - tm.assert_series_equal(result, expected) - - @pytest.mark.filterwarnings( - "ignore:Adding/subtracting object-dtype array to DatetimeArray" + ], ) - def test_series_sub_daytime_index_offset(self): + @pytest.mark.parametrize( + "expected", + [ + pd.Series( + [ + pd.Timestamp("1999-1-1"), + pd.Timestamp("2000-1-29"), + pd.Timestamp("2000-05-01"), + ] + ) + ], + ) + @pytest.mark.parametrize( + "constr_daytime, constr_offset, constr_expected", + [ + (pd.Series, pd.Series, pd.Series), + (pd.Series, pd.Index, pd.Series), + (pd.DatetimeIndex, pd.Series, pd.Series), + ], + ) + def test_sub_daytime_offset( + self, daytime, offset, expected, constr_daytime, constr_offset, constr_expected + ): # GH#19211 - ser_daytime = pd.Series( - [ - pd.Timestamp("2000-01-01"), - pd.Timestamp("2000-03-29"), - pd.Timestamp("2000-05-15"), - ] - ) - ser_offset = pd.Series( - [ - pd.offsets.DateOffset(years=1), - pd.offsets.DateOffset(months=2), - pd.offsets.MonthBegin(), - ] - ) - - result = pd.DatetimeIndex(ser_daytime) - ser_offset - - expected = pd.Series( - [ - pd.Timestamp("1999-1-1"), - pd.Timestamp("2000-1-29"), - pd.Timestamp("2000-05-01"), - ] - ) - tm.assert_series_equal(result, expected) + result = constr_daytime(daytime) - constr_offset(offset) + tm.assert_series_equal(result, constr_expected(expected)) def test_datetime_understood(self): # Ensures it doesn't fail to create the right series From d4fd4e6a575b921a47e184c5ee1d836fdb9cbd26 Mon Sep 17 00:00:00 2001 From: KD-dev-lab Date: Sun, 7 Jun 2020 20:01:45 +0200 Subject: [PATCH 7/9] TST: Moved tests to pandas/tests/arithmetic/test_datetime64.py and renamed them. --- pandas/tests/arithmetic/test_datetime64.py | 102 ++++++++++++++++++++- pandas/tests/series/test_arithmetic.py | 101 +------------------- 2 files changed, 102 insertions(+), 101 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index b3f4d5f5d9ee5..b24087d626034 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -747,7 +747,7 @@ class TestDatetime64Arithmetic: # over DataFrame/Series/Index/DatetimeArray # ------------------------------------------------------------- - # Addition/Subtraction of timedelta-like + # Addition/Subtraction of timedelta-like scalars def test_dt64arr_add_timedeltalike_scalar( self, tz_naive_fixture, two_hours, box_with_array @@ -778,6 +778,56 @@ def test_dt64arr_iadd_timedeltalike_scalar( rng += two_hours tm.assert_equal(rng, expected) + @pytest.mark.filterwarnings( + "ignore:Adding/subtracting object-dtype array to DatetimeArray" + ) + @pytest.mark.parametrize( + "dt64arr", + [ + [ + pd.Timestamp("2000-01-01"), + pd.Timestamp("2000-02-01"), + pd.Timestamp("2000-05-01"), + ] + ], + ) + @pytest.mark.parametrize( + "offset_arr", + [ + [ + pd.offsets.DateOffset(years=1), + pd.offsets.DateOffset(months=2), + pd.offsets.MonthEnd(), + ] + ], + ) + @pytest.mark.parametrize( + "expected", + [ + [ + pd.Timestamp("2001-01-01"), + pd.Timestamp("2000-04-01"), + pd.Timestamp("2000-05-31"), + ] + ], + ) + @pytest.mark.parametrize( + "constr_dt, constr_offset, constr_expected", + [ + (pd.Series, pd.Series, pd.Series), + (pd.Series, pd.Index, pd.Series), + (pd.DatetimeIndex, pd.Series, pd.Series), + ], + ) + def test_dt64arr_add_offset_scalar( + self, dt64arr, offset_arr, expected, constr_dt, constr_offset, constr_expected, + ): + # GH#19211 + result1 = constr_offset(offset_arr) + constr_dt(dt64arr) + result2 = constr_dt(dt64arr) + constr_offset(offset_arr) + tm.assert_series_equal(result1, constr_expected(expected)) + tm.assert_series_equal(result2, constr_expected(expected)) + def test_dt64arr_sub_timedeltalike_scalar( self, tz_naive_fixture, two_hours, box_with_array ): @@ -806,6 +856,56 @@ def test_dt64arr_isub_timedeltalike_scalar( rng -= two_hours tm.assert_equal(rng, expected) + @pytest.mark.filterwarnings( + "ignore:Adding/subtracting object-dtype array to DatetimeArray" + ) + @pytest.mark.parametrize( + "dt64arr", + [ + [ + pd.Timestamp("2000-01-01"), + pd.Timestamp("2000-03-29"), + pd.Timestamp("2000-05-15"), + ] + ], + ) + @pytest.mark.parametrize( + "offset_arr", + [ + [ + pd.offsets.DateOffset(years=1), + pd.offsets.DateOffset(months=2), + pd.offsets.MonthBegin(), + ] + ], + ) + @pytest.mark.parametrize( + "expected", + [ + pd.Series( + [ + pd.Timestamp("1999-1-1"), + pd.Timestamp("2000-1-29"), + pd.Timestamp("2000-05-01"), + ] + ) + ], + ) + @pytest.mark.parametrize( + "constr_dt, constr_offset, constr_expected", + [ + (pd.Series, pd.Series, pd.Series), + (pd.Series, pd.Index, pd.Series), + (pd.DatetimeIndex, pd.Series, pd.Series), + ], + ) + def test_dt64arr_sub_offset_scalar( + self, dt64arr, offset_arr, expected, constr_dt, constr_offset, constr_expected, + ): + # GH#19211 + result = constr_dt(dt64arr) - constr_offset(offset_arr) + tm.assert_series_equal(result, constr_expected(expected)) + # TODO: redundant with test_dt64arr_add_timedeltalike_scalar def test_dt64arr_add_td64_scalar(self, box_with_array): # scalar timedeltas/np.timedelta64 objects diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index 5ab571d1e8cad..5c8a0d224c4f9 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -595,6 +595,7 @@ def test_comp_ops_df_compat(self): s4 = pd.Series([2, 2, 2, 2], index=list("ABCD"), name="x") for left, right in [(s1, s2), (s2, s1), (s3, s4), (s4, s3)]: + msg = "Can only compare identically-labeled Series objects" with pytest.raises(ValueError, match=msg): left == right @@ -678,106 +679,6 @@ def test_series_add_aware_naive_raises(self): with pytest.raises(Exception, match=msg): ser_utc + ser - @pytest.mark.filterwarnings( - "ignore:Adding/subtracting object-dtype array to DatetimeArray" - ) - @pytest.mark.parametrize( - "daytime", - [ - [ - pd.Timestamp("2000-01-01"), - pd.Timestamp("2000-02-01"), - pd.Timestamp("2000-05-01"), - ] - ], - ) - @pytest.mark.parametrize( - "offset", - [ - [ - pd.offsets.DateOffset(years=1), - pd.offsets.DateOffset(months=2), - pd.offsets.MonthEnd(), - ] - ], - ) - @pytest.mark.parametrize( - "expected", - [ - [ - pd.Timestamp("2001-01-01"), - pd.Timestamp("2000-04-01"), - pd.Timestamp("2000-05-31"), - ] - ], - ) - @pytest.mark.parametrize( - "constr_daytime, constr_offset, constr_expected", - [ - (pd.Series, pd.Series, pd.Series), - (pd.Series, pd.Index, pd.Series), - (pd.DatetimeIndex, pd.Series, pd.Series), - ], - ) - def test_add_daytime_offset( - self, daytime, offset, expected, constr_daytime, constr_offset, constr_expected - ): - # GH#19211 - result1 = constr_offset(offset) + constr_daytime(daytime) - result2 = constr_daytime(daytime) + constr_offset(offset) - tm.assert_series_equal(result1, constr_expected(expected)) - tm.assert_series_equal(result2, constr_expected(expected)) - - @pytest.mark.filterwarnings( - "ignore:Adding/subtracting object-dtype array to DatetimeArray" - ) - @pytest.mark.parametrize( - "daytime", - [ - [ - pd.Timestamp("2000-01-01"), - pd.Timestamp("2000-03-29"), - pd.Timestamp("2000-05-15"), - ] - ], - ) - @pytest.mark.parametrize( - "offset", - [ - [ - pd.offsets.DateOffset(years=1), - pd.offsets.DateOffset(months=2), - pd.offsets.MonthBegin(), - ] - ], - ) - @pytest.mark.parametrize( - "expected", - [ - pd.Series( - [ - pd.Timestamp("1999-1-1"), - pd.Timestamp("2000-1-29"), - pd.Timestamp("2000-05-01"), - ] - ) - ], - ) - @pytest.mark.parametrize( - "constr_daytime, constr_offset, constr_expected", - [ - (pd.Series, pd.Series, pd.Series), - (pd.Series, pd.Index, pd.Series), - (pd.DatetimeIndex, pd.Series, pd.Series), - ], - ) - def test_sub_daytime_offset( - self, daytime, offset, expected, constr_daytime, constr_offset, constr_expected - ): - # GH#19211 - result = constr_daytime(daytime) - constr_offset(offset) - tm.assert_series_equal(result, constr_expected(expected)) - def test_datetime_understood(self): # Ensures it doesn't fail to create the right series # reported in issue#16726 From 09e6ba1e965d3fe1a074eb38c7f8da12b1ac9030 Mon Sep 17 00:00:00 2001 From: KD-dev-lab Date: Tue, 9 Jun 2020 00:05:58 +0200 Subject: [PATCH 8/9] TST: Grouped dependent input/output parameters at @parametrize. --- pandas/tests/arithmetic/test_datetime64.py | 80 +++++++++------------- 1 file changed, 31 insertions(+), 49 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index b24087d626034..816f6f2fb385c 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -782,33 +782,25 @@ def test_dt64arr_iadd_timedeltalike_scalar( "ignore:Adding/subtracting object-dtype array to DatetimeArray" ) @pytest.mark.parametrize( - "dt64arr", + "dt64arr, offset_arr, expected", [ - [ - pd.Timestamp("2000-01-01"), - pd.Timestamp("2000-02-01"), - pd.Timestamp("2000-05-01"), - ] - ], - ) - @pytest.mark.parametrize( - "offset_arr", - [ - [ - pd.offsets.DateOffset(years=1), - pd.offsets.DateOffset(months=2), - pd.offsets.MonthEnd(), - ] - ], - ) - @pytest.mark.parametrize( - "expected", - [ - [ - pd.Timestamp("2001-01-01"), - pd.Timestamp("2000-04-01"), - pd.Timestamp("2000-05-31"), - ] + ( + [ + pd.Timestamp("2000-01-01"), + pd.Timestamp("2000-02-01"), + pd.Timestamp("2000-05-01"), + ], + [ + pd.offsets.DateOffset(years=1), + pd.offsets.DateOffset(months=2), + pd.offsets.MonthEnd(), + ], + [ + pd.Timestamp("2001-01-01"), + pd.Timestamp("2000-04-01"), + pd.Timestamp("2000-05-31"), + ], + ) ], ) @pytest.mark.parametrize( @@ -860,34 +852,24 @@ def test_dt64arr_isub_timedeltalike_scalar( "ignore:Adding/subtracting object-dtype array to DatetimeArray" ) @pytest.mark.parametrize( - "dt64arr", + "dt64arr, offset_arr, expected", [ - [ - pd.Timestamp("2000-01-01"), - pd.Timestamp("2000-03-29"), - pd.Timestamp("2000-05-15"), - ] - ], - ) - @pytest.mark.parametrize( - "offset_arr", - [ - [ - pd.offsets.DateOffset(years=1), - pd.offsets.DateOffset(months=2), - pd.offsets.MonthBegin(), - ] - ], - ) - @pytest.mark.parametrize( - "expected", - [ - pd.Series( + ( + [ + pd.Timestamp("2000-01-01"), + pd.Timestamp("2000-03-29"), + pd.Timestamp("2000-05-15"), + ], + [ + pd.offsets.DateOffset(years=1), + pd.offsets.DateOffset(months=2), + pd.offsets.MonthBegin(), + ], [ pd.Timestamp("1999-1-1"), pd.Timestamp("2000-1-29"), pd.Timestamp("2000-05-01"), - ] + ], ) ], ) From 1519abbc71c51e90191e1d44304a0eb6a3b63eb1 Mon Sep 17 00:00:00 2001 From: KD-dev-lab Date: Wed, 10 Jun 2020 02:34:54 +0200 Subject: [PATCH 9/9] TST: Adapted test test_dti_addsub_offset_arraylike. Changed index_or_series to box to also cover pd.DataFrame. --- pandas/tests/arithmetic/test_datetime64.py | 92 +--------------------- 1 file changed, 4 insertions(+), 88 deletions(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 816f6f2fb385c..f7c12cbe881ea 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -747,7 +747,7 @@ class TestDatetime64Arithmetic: # over DataFrame/Series/Index/DatetimeArray # ------------------------------------------------------------- - # Addition/Subtraction of timedelta-like scalars + # Addition/Subtraction of timedelta-like def test_dt64arr_add_timedeltalike_scalar( self, tz_naive_fixture, two_hours, box_with_array @@ -778,48 +778,6 @@ def test_dt64arr_iadd_timedeltalike_scalar( rng += two_hours tm.assert_equal(rng, expected) - @pytest.mark.filterwarnings( - "ignore:Adding/subtracting object-dtype array to DatetimeArray" - ) - @pytest.mark.parametrize( - "dt64arr, offset_arr, expected", - [ - ( - [ - pd.Timestamp("2000-01-01"), - pd.Timestamp("2000-02-01"), - pd.Timestamp("2000-05-01"), - ], - [ - pd.offsets.DateOffset(years=1), - pd.offsets.DateOffset(months=2), - pd.offsets.MonthEnd(), - ], - [ - pd.Timestamp("2001-01-01"), - pd.Timestamp("2000-04-01"), - pd.Timestamp("2000-05-31"), - ], - ) - ], - ) - @pytest.mark.parametrize( - "constr_dt, constr_offset, constr_expected", - [ - (pd.Series, pd.Series, pd.Series), - (pd.Series, pd.Index, pd.Series), - (pd.DatetimeIndex, pd.Series, pd.Series), - ], - ) - def test_dt64arr_add_offset_scalar( - self, dt64arr, offset_arr, expected, constr_dt, constr_offset, constr_expected, - ): - # GH#19211 - result1 = constr_offset(offset_arr) + constr_dt(dt64arr) - result2 = constr_dt(dt64arr) + constr_offset(offset_arr) - tm.assert_series_equal(result1, constr_expected(expected)) - tm.assert_series_equal(result2, constr_expected(expected)) - def test_dt64arr_sub_timedeltalike_scalar( self, tz_naive_fixture, two_hours, box_with_array ): @@ -848,46 +806,6 @@ def test_dt64arr_isub_timedeltalike_scalar( rng -= two_hours tm.assert_equal(rng, expected) - @pytest.mark.filterwarnings( - "ignore:Adding/subtracting object-dtype array to DatetimeArray" - ) - @pytest.mark.parametrize( - "dt64arr, offset_arr, expected", - [ - ( - [ - pd.Timestamp("2000-01-01"), - pd.Timestamp("2000-03-29"), - pd.Timestamp("2000-05-15"), - ], - [ - pd.offsets.DateOffset(years=1), - pd.offsets.DateOffset(months=2), - pd.offsets.MonthBegin(), - ], - [ - pd.Timestamp("1999-1-1"), - pd.Timestamp("2000-1-29"), - pd.Timestamp("2000-05-01"), - ], - ) - ], - ) - @pytest.mark.parametrize( - "constr_dt, constr_offset, constr_expected", - [ - (pd.Series, pd.Series, pd.Series), - (pd.Series, pd.Index, pd.Series), - (pd.DatetimeIndex, pd.Series, pd.Series), - ], - ) - def test_dt64arr_sub_offset_scalar( - self, dt64arr, offset_arr, expected, constr_dt, constr_offset, constr_expected, - ): - # GH#19211 - result = constr_dt(dt64arr) - constr_offset(offset_arr) - tm.assert_series_equal(result, constr_expected(expected)) - # TODO: redundant with test_dt64arr_add_timedeltalike_scalar def test_dt64arr_add_td64_scalar(self, box_with_array): # scalar timedeltas/np.timedelta64 objects @@ -2458,12 +2376,10 @@ def test_dti_add_series(self, tz_naive_fixture, names): tm.assert_index_equal(result4, expected) @pytest.mark.parametrize("op", [operator.add, roperator.radd, operator.sub]) - def test_dti_addsub_offset_arraylike( - self, tz_naive_fixture, names, op, index_or_series - ): - # GH#18849, GH#19744 + def test_dti_addsub_offset_arraylike(self, tz_naive_fixture, names, op, box): + # GH#18849, GH#19744, GH#19211 box = pd.Index - other_box = index_or_series + other_box = box tz = tz_naive_fixture dti = pd.date_range("2017-01-01", periods=2, tz=tz, name=names[0])