Skip to content

Commit 2312ad6

Browse files
committed
BUG: Index.to_series() is not copying the index
1 parent f35209e commit 2312ad6

File tree

5 files changed

+17
-3
lines changed

5 files changed

+17
-3
lines changed

doc/source/whatsnew/v0.20.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,7 @@ Conversion
11481148
- Bug in ``DataFrame`` construction with nulls and datetimes in a list-like (:issue:`15869`)
11491149
- Bug in ``DataFrame.fillna()`` with tz-aware datetimes (:issue:`15855`)
11501150
- Bug in ``is_string_dtype``, ``is_timedelta64_ns_dtype``, and ``is_string_like_dtype`` in which an error was raised when ``None`` was passed in (:issue:`15941`)
1151+
- Bug in ``Index.to_series()`` where the index was not copied (and so mutating later would change the original), (:issue:`15949`)
11511152

11521153
Indexing
11531154
^^^^^^^^

pandas/indexes/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,9 @@ def to_series(self, **kwargs):
944944
"""
945945

946946
from pandas import Series
947-
return Series(self._to_embed(), index=self, name=self.name)
947+
return Series(self._to_embed(),
948+
index=self._shallow_copy(),
949+
name=self.name)
948950

949951
def _to_embed(self, keep_tz=False):
950952
"""

pandas/tests/frame/test_query_eval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ def test_date_index_query_with_NaT_duplicates(self):
484484
df = DataFrame(d)
485485
df.loc[np.random.rand(n) > 0.5, 'dates1'] = pd.NaT
486486
df.set_index('dates1', inplace=True, drop=True)
487-
res = df.query('index < 20130101 < dates3', engine=engine,
487+
res = df.query('dates1 < 20130101 < dates3', engine=engine,
488488
parser=parser)
489489
expec = df[(df.index.to_series() < '20130101') &
490490
('20130101' < df.dates3)]

pandas/tests/indexes/common.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ def test_pickle_compat_construction(self):
3838
# need an object to create with
3939
self.assertRaises(TypeError, self._holder)
4040

41+
def test_to_series(self):
42+
# assert that we are creating a copy of the index
43+
44+
idx = self.create_index()
45+
s = idx.to_series()
46+
assert s.values is not idx.values
47+
assert s.index is not idx
48+
assert s.name == idx.name
49+
4150
def test_shift(self):
4251

4352
# GH8083 test the base class for shift

pandas/tseries/index.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,9 @@ def to_series(self, keep_tz=False):
895895
Series
896896
"""
897897
from pandas import Series
898-
return Series(self._to_embed(keep_tz), index=self, name=self.name)
898+
return Series(self._to_embed(keep_tz),
899+
index=self._shallow_copy(),
900+
name=self.name)
899901

900902
def _to_embed(self, keep_tz=False):
901903
"""

0 commit comments

Comments
 (0)