From 43851287d1a0f1674c101c47d6b7d047c822e0fb Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Thu, 29 Nov 2018 11:22:35 +0100 Subject: [PATCH 01/14] pytest for slice type in setitem --- pandas/tests/arrays/test_period.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pandas/tests/arrays/test_period.py b/pandas/tests/arrays/test_period.py index 63b34db13705e..30e8889d4e3c6 100644 --- a/pandas/tests/arrays/test_period.py +++ b/pandas/tests/arrays/test_period.py @@ -195,3 +195,21 @@ def test_sub_period(): other = pd.Period("2000", freq="M") with pytest.raises(IncompatibleFrequency, match="freq"): arr - other + + +@pytest.mark.parametrize('data', [ + period_array(['2001', '2002', '2003', '2004', '2005'], freq="D") +]) +def test_setitem_slice_mismatch_length_raises(data): + arr = data[:5] + with pytest.raises(ValueError): + arr[:1] = arr[:2] + + +@pytest.mark.parametrize('data', [ + period_array(['2001', '2002', '2003', '2004', '2005'], freq="D") +]) +def test_setitem_slice_array(data): + arr = data[:5].copy() + arr[:5] = data[-5:] + tm.assert_extension_array_equal(arr, data[-5:]) From aacb76968ad54e9da916a7457f4fe8a479a956e6 Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Thu, 29 Nov 2018 13:26:55 +0100 Subject: [PATCH 02/14] remove wrong pytests --- pandas/tests/arrays/test_period.py | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/pandas/tests/arrays/test_period.py b/pandas/tests/arrays/test_period.py index 30e8889d4e3c6..63b34db13705e 100644 --- a/pandas/tests/arrays/test_period.py +++ b/pandas/tests/arrays/test_period.py @@ -195,21 +195,3 @@ def test_sub_period(): other = pd.Period("2000", freq="M") with pytest.raises(IncompatibleFrequency, match="freq"): arr - other - - -@pytest.mark.parametrize('data', [ - period_array(['2001', '2002', '2003', '2004', '2005'], freq="D") -]) -def test_setitem_slice_mismatch_length_raises(data): - arr = data[:5] - with pytest.raises(ValueError): - arr[:1] = arr[:2] - - -@pytest.mark.parametrize('data', [ - period_array(['2001', '2002', '2003', '2004', '2005'], freq="D") -]) -def test_setitem_slice_array(data): - arr = data[:5].copy() - arr[:5] = data[-5:] - tm.assert_extension_array_equal(arr, data[-5:]) From e19c381e71055b45c3ed360472a81c10eb562b3f Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Thu, 29 Nov 2018 13:27:26 +0100 Subject: [PATCH 03/14] new pytest in TestSetitem --- pandas/tests/extension/test_period.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pandas/tests/extension/test_period.py b/pandas/tests/extension/test_period.py index 2e629ccb2981e..4e55221022617 100644 --- a/pandas/tests/extension/test_period.py +++ b/pandas/tests/extension/test_period.py @@ -8,7 +8,7 @@ import pandas as pd from pandas.core.arrays import PeriodArray from pandas.tests.extension import base - +import pandas.util.testing as tm @pytest.fixture def dtype(): @@ -147,7 +147,16 @@ class TestReshaping(BasePeriodTests, base.BaseReshapingTests): class TestSetitem(BasePeriodTests, base.BaseSetitemTests): - pass + + def test_setitem_slice_mismatch_length_raises(self, data): + arr = data[:5] + with pytest.raises(ValueError): + arr[:1] = arr[:2] + + def test_setitem_slice_array(self, data): + arr = data[:5].copy() + arr[:5] = data[-5:] + tm.assert_extension_array_equal(arr, data[-5:]) class TestGroupby(BasePeriodTests, base.BaseGroupbyTests): From b12f0bcfbfcd630d3c4138b2ce15825eca6804ca Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Thu, 29 Nov 2018 19:03:18 +0100 Subject: [PATCH 04/14] fix pytest error --- pandas/core/arrays/period.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 53629dca4d391..5cb909367ed43 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -356,12 +356,16 @@ def __setitem__( # by period_array, which includes things like ndarray[object], # ndarray[datetime64ns]. I think ndarray[int] / ndarray[str] won't # work, since the freq can't be inferred. + if is_list_like(value): - if len(key) != len(value) and not com.is_bool_indexer(key): + is_slice = isinstance(key, slice) + if (not is_slice + and len(key) != len(value) + and not com.is_bool_indexer(key)): msg = ("shape mismatch: value array of length '{}' does not " "match indexing result of length '{}'.") raise ValueError(msg.format(len(key), len(value))) - if len(key) == 0: + if not is_slice and len(key) == 0: return value = period_array(value) From 6ca1f1ae1c3fe10990f7f69ca86907f95bfec1cb Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Thu, 29 Nov 2018 21:12:21 +0100 Subject: [PATCH 05/14] remove a blank line to see if error disappear --- pandas/core/arrays/period.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 5cb909367ed43..e05810b14892b 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -356,7 +356,6 @@ def __setitem__( # by period_array, which includes things like ndarray[object], # ndarray[datetime64ns]. I think ndarray[int] / ndarray[str] won't # work, since the freq can't be inferred. - if is_list_like(value): is_slice = isinstance(key, slice) if (not is_slice From f3db6c1f797d639bed92c52df64b8ae9294dc9cd Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Thu, 29 Nov 2018 21:17:45 +0100 Subject: [PATCH 06/14] resort imports --- pandas/tests/extension/test_period.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/tests/extension/test_period.py b/pandas/tests/extension/test_period.py index 4e55221022617..1f2894219b598 100644 --- a/pandas/tests/extension/test_period.py +++ b/pandas/tests/extension/test_period.py @@ -6,9 +6,12 @@ from pandas.core.dtypes.dtypes import PeriodDtype import pandas as pd + +import pandas.util.testing as tm + from pandas.core.arrays import PeriodArray from pandas.tests.extension import base -import pandas.util.testing as tm + @pytest.fixture def dtype(): From fe74ea13aae867fd375414140885a69a3fad56ea Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Fri, 30 Nov 2018 09:20:52 +0100 Subject: [PATCH 07/14] fix linting failure --- pandas/tests/extension/test_period.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/extension/test_period.py b/pandas/tests/extension/test_period.py index 1f2894219b598..3e904f2e8e88e 100644 --- a/pandas/tests/extension/test_period.py +++ b/pandas/tests/extension/test_period.py @@ -6,7 +6,6 @@ from pandas.core.dtypes.dtypes import PeriodDtype import pandas as pd - import pandas.util.testing as tm from pandas.core.arrays import PeriodArray From 0e19fdb9371c49be0c758bbc2a0b446d41fdb0ae Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Fri, 30 Nov 2018 10:37:20 +0100 Subject: [PATCH 08/14] fix linting error --- pandas/core/arrays/period.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index e05810b14892b..dd79c9bddac15 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -347,7 +347,7 @@ def __repr__(self): def __setitem__( self, - key, # type: Union[int, Sequence[int], Sequence[bool]] + key, # type: Union[int, Sequence[int], Sequence[bool], slice] value # type: Union[NaTType, Period, Sequence[Period]] ): # type: (...) -> None From 3e76a61da05f5fbdf3d159bd7a56e1ba3933a877 Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Fri, 30 Nov 2018 11:50:40 +0100 Subject: [PATCH 09/14] resort imports --- pandas/tests/extension/test_period.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/extension/test_period.py b/pandas/tests/extension/test_period.py index 3e904f2e8e88e..86bacf1704296 100644 --- a/pandas/tests/extension/test_period.py +++ b/pandas/tests/extension/test_period.py @@ -6,8 +6,8 @@ from pandas.core.dtypes.dtypes import PeriodDtype import pandas as pd -import pandas.util.testing as tm +import pandas.util.testing as tm from pandas.core.arrays import PeriodArray from pandas.tests.extension import base From ec8f68eab703d70c58c92267b0aca940049986b2 Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Fri, 30 Nov 2018 11:54:33 +0100 Subject: [PATCH 10/14] resort imports using isort --- pandas/tests/extension/test_period.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/extension/test_period.py b/pandas/tests/extension/test_period.py index 86bacf1704296..3192c9baa8af4 100644 --- a/pandas/tests/extension/test_period.py +++ b/pandas/tests/extension/test_period.py @@ -7,9 +7,9 @@ import pandas as pd -import pandas.util.testing as tm from pandas.core.arrays import PeriodArray from pandas.tests.extension import base +import pandas.util.testing as tm @pytest.fixture From 217146f1e80cc58f7010e744cfe81d47fd525f89 Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Fri, 30 Nov 2018 12:55:59 +0100 Subject: [PATCH 11/14] remove redundant blank line --- pandas/tests/extension/test_period.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/extension/test_period.py b/pandas/tests/extension/test_period.py index 3192c9baa8af4..589919b9f5d1f 100644 --- a/pandas/tests/extension/test_period.py +++ b/pandas/tests/extension/test_period.py @@ -6,7 +6,6 @@ from pandas.core.dtypes.dtypes import PeriodDtype import pandas as pd - from pandas.core.arrays import PeriodArray from pandas.tests.extension import base import pandas.util.testing as tm From 74ea7b84077c752068216e72b0887ec102335e3c Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Sat, 1 Dec 2018 21:40:05 +0100 Subject: [PATCH 12/14] move to basesetitem --- pandas/tests/extension/base/setitem.py | 10 ++++++++++ pandas/tests/extension/test_period.py | 10 +--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pandas/tests/extension/base/setitem.py b/pandas/tests/extension/base/setitem.py index 3d798b2af5c43..479ab71676315 100644 --- a/pandas/tests/extension/base/setitem.py +++ b/pandas/tests/extension/base/setitem.py @@ -173,3 +173,13 @@ def test_setitem_tuple_index(self, data): expected = pd.Series(data.take([1, 1]), index=s.index) s[(0, 1)] = data[1] self.assert_series_equal(s, expected) + + def test_setitem_slice_mismatch_length_raises(self, data): + arr = data[:5] + with pytest.raises(ValueError): + arr[:1] = arr[:2] + + def test_setitem_slice_array(self, data): + arr = data[:5].copy() + arr[:5] = data[-5:] + self.assert_extension_array_equal(arr, data[-5:]) diff --git a/pandas/tests/extension/test_period.py b/pandas/tests/extension/test_period.py index 589919b9f5d1f..03d43d3f40a6b 100644 --- a/pandas/tests/extension/test_period.py +++ b/pandas/tests/extension/test_period.py @@ -149,15 +149,7 @@ class TestReshaping(BasePeriodTests, base.BaseReshapingTests): class TestSetitem(BasePeriodTests, base.BaseSetitemTests): - def test_setitem_slice_mismatch_length_raises(self, data): - arr = data[:5] - with pytest.raises(ValueError): - arr[:1] = arr[:2] - - def test_setitem_slice_array(self, data): - arr = data[:5].copy() - arr[:5] = data[-5:] - tm.assert_extension_array_equal(arr, data[-5:]) + pass class TestGroupby(BasePeriodTests, base.BaseGroupbyTests): From 39a48075896db12fc23c3a327e944812b3eee67b Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Sun, 2 Dec 2018 10:03:52 +0100 Subject: [PATCH 13/14] remove unused package from import --- pandas/tests/extension/test_period.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/tests/extension/test_period.py b/pandas/tests/extension/test_period.py index 03d43d3f40a6b..2e629ccb2981e 100644 --- a/pandas/tests/extension/test_period.py +++ b/pandas/tests/extension/test_period.py @@ -8,7 +8,6 @@ import pandas as pd from pandas.core.arrays import PeriodArray from pandas.tests.extension import base -import pandas.util.testing as tm @pytest.fixture @@ -148,7 +147,6 @@ class TestReshaping(BasePeriodTests, base.BaseReshapingTests): class TestSetitem(BasePeriodTests, base.BaseSetitemTests): - pass From 6c9702eefd1c6eb75adfe987c0b9741fe3163fc9 Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Sun, 2 Dec 2018 19:23:01 +0100 Subject: [PATCH 14/14] add whatsnew description --- doc/source/whatsnew/v0.24.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index c74bcb505b6be..5976ecb6bffd3 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1349,6 +1349,7 @@ Indexing - Bug where setting a timedelta column by ``Index`` causes it to be casted to double, and therefore lose precision (:issue:`23511`) - Bug in :func:`Index.union` and :func:`Index.intersection` where name of the ``Index`` of the result was not computed correctly for certain cases (:issue:`9943`, :issue:`9862`) - Bug in :class:`Index` slicing with boolean :class:`Index` may raise ``TypeError`` (:issue:`22533`) +- Bug in ``PeriodArray.__setitem__`` when accepting slice and list-like value (:issue:`23978`) Missing ^^^^^^^