From 72b7ee5b65a182e76d8bf72a8b9b4d241ac7823a Mon Sep 17 00:00:00 2001 From: makbigc Date: Sun, 29 Jul 2018 23:30:05 +0800 Subject: [PATCH 1/6] Fix Bug #21907 --- doc/source/whatsnew/v0.24.0.rst | 1 + pandas/core/series.py | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 98941b6d353bb..5677a38bc7fce 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1303,6 +1303,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 when :class:`Series` was created from :class:`DatetimeIndex`, the series shares the same underneath data with that index (:issue:`21907`) Missing ^^^^^^^ diff --git a/pandas/core/series.py b/pandas/core/series.py index 892b24f6ee552..f4db88f47cd3e 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -182,7 +182,11 @@ def __init__(self, data=None, index=None, dtype=None, name=None, data = data.astype(dtype) else: # need to copy to avoid aliasing issues - data = data._values.copy() + # GH21907 + if isinstance(data, DatetimeIndex): + data = data.copy(deep=True) + else: + data = data._values.copy() copy = False elif isinstance(data, np.ndarray): From 260afd8fc12d8237a5808f0dbf37b42f243cd837 Mon Sep 17 00:00:00 2001 From: Mak Sze Chun Date: Tue, 7 Aug 2018 22:06:45 +0800 Subject: [PATCH 2/6] Update test_constructors.py --- pandas/tests/series/test_constructors.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index ce0cf0d5c089e..34f815fdd5085 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -970,6 +970,23 @@ def test_constructor_set(self): values = frozenset(values) pytest.raises(TypeError, Series, values) + @pytest.mark.parametrize( + "src", + [pd.date_range('2016-01-01', periods=10, tz='US/Pacific'), + pd.timedelta_range('1 day', periods=10), + pd.period_range('2012Q1', periods=3, freq='Q'), + pd.Index(list('abcdefghij')), + pd.Int64Index(np.arange(10)), + pd.RangeIndex(0, 10)]) + def test_fromIndex(self, src): + # GH21907 + # When constructing a series from an index, + # the series does not share data with that index + expected = src.copy(deep=True) + prod = Series(src) + prod[::3] = NaT + tm.assert_index_equal(src, expected) + # https://github.com/pandas-dev/pandas/issues/22698 @pytest.mark.filterwarnings("ignore:elementwise comparison:FutureWarning") def test_fromDict(self): From ca4ff5df925b5db18cfbfdfa60b2e5d19c6caa71 Mon Sep 17 00:00:00 2001 From: Mak Sze Chun Date: Tue, 7 Aug 2018 22:08:31 +0800 Subject: [PATCH 3/6] Update v0.24.0.txt --- 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 5677a38bc7fce..6ab10406c2b2a 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1304,6 +1304,7 @@ Indexing - 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 when :class:`Series` was created from :class:`DatetimeIndex`, the series shares the same underneath data with that index (:issue:`21907`) +- Bug when :class:`Series` was created from :class:`DatetimeIndex`, the series shares the same data with that index (:issue:`21907`) Missing ^^^^^^^ From f0397efa3f7ca300c0d790c093c6a4221e0a134f Mon Sep 17 00:00:00 2001 From: makbigc Date: Sun, 11 Nov 2018 22:36:08 +0800 Subject: [PATCH 4/6] Add assertion that expected[0] is not NaT --- pandas/tests/series/test_constructors.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 34f815fdd5085..8106fa8125103 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -985,6 +985,7 @@ def test_fromIndex(self, src): expected = src.copy(deep=True) prod = Series(src) prod[::3] = NaT + assert expected[0] is not NaT tm.assert_index_equal(src, expected) # https://github.com/pandas-dev/pandas/issues/22698 From 388a21a9f652e506c6f505ab99202e94c55c76c4 Mon Sep 17 00:00:00 2001 From: makbigc Date: Sun, 18 Nov 2018 21:41:47 +0800 Subject: [PATCH 5/6] Change data to data._values in checking condition --- pandas/core/series.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index f4db88f47cd3e..9ecb4b52bbb46 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -181,9 +181,8 @@ def __init__(self, data=None, index=None, dtype=None, name=None, # astype copies data = data.astype(dtype) else: - # need to copy to avoid aliasing issues # GH21907 - if isinstance(data, DatetimeIndex): + if isinstance(data._values, DatetimeIndex): data = data.copy(deep=True) else: data = data._values.copy() From aba2368cb71f8f2b95012cbe48c55d6eeaab9f66 Mon Sep 17 00:00:00 2001 From: Mak Sze Chun Date: Mon, 19 Nov 2018 20:41:42 +0800 Subject: [PATCH 6/6] remove duplicate line in v0.24.0.rst --- doc/source/whatsnew/v0.24.0.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 6ab10406c2b2a..b614d760c7b7d 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1303,7 +1303,6 @@ 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 when :class:`Series` was created from :class:`DatetimeIndex`, the series shares the same underneath data with that index (:issue:`21907`) - Bug when :class:`Series` was created from :class:`DatetimeIndex`, the series shares the same data with that index (:issue:`21907`) Missing