Skip to content

Commit 079758e

Browse files
committed
BUG: Index.to_series() is not copying the index
1 parent 28df913 commit 079758e

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

pandas/indexes/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ 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(), index=self.copy(), name=self.name)
948948

949949
def _to_embed(self, keep_tz=False):
950950
"""

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.copy(),
900+
name=self.name)
899901

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

0 commit comments

Comments
 (0)