Skip to content

Commit d984cfc

Browse files
authored
TST: clean up series/frame api tests inheritance a bit (#15949)
* TST: clean up series/frame api tests inheritance a bit * BUG: Index.to_series() is not copying the index
1 parent 9cb2c2d commit d984cfc

File tree

10 files changed

+36
-30
lines changed

10 files changed

+36
-30
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,7 @@ Conversion
12191219
- Bug in ``DataFrame.fillna()`` with tz-aware datetimes (:issue:`15855`)
12201220
- 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`)
12211221
- Bug in the return type of ``pd.unique`` on a ``Categorical``, which was returning an ndarray and not a ``Categorical`` (:issue:`15903`)
1222+
- Bug in ``Index.to_series()`` where the index was not copied (and so mutating later would change the original), (:issue:`15949`)
12221223

12231224
Indexing
12241225
^^^^^^^^

pandas/indexes/base.py

+3-1
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
"""
File renamed without changes.

pandas/tests/frame/test_query_eval.py

+1-1
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

+9
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/tests/series/test_quantile.py

+17-25
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@
1616
class TestSeriesQuantile(TestData, tm.TestCase):
1717

1818
def test_quantile(self):
19-
from numpy import percentile
2019

2120
q = self.ts.quantile(0.1)
22-
self.assertEqual(q, percentile(self.ts.valid(), 10))
21+
self.assertEqual(q, np.percentile(self.ts.valid(), 10))
2322

2423
q = self.ts.quantile(0.9)
25-
self.assertEqual(q, percentile(self.ts.valid(), 90))
24+
self.assertEqual(q, np.percentile(self.ts.valid(), 90))
2625

2726
# object dtype
2827
q = Series(self.ts, dtype=object).quantile(0.9)
29-
self.assertEqual(q, percentile(self.ts.valid(), 90))
28+
self.assertEqual(q, np.percentile(self.ts.valid(), 90))
3029

3130
# datetime64[ns] dtype
3231
dts = self.ts.index.to_series()
@@ -48,12 +47,11 @@ def test_quantile(self):
4847
self.ts.quantile(invalid)
4948

5049
def test_quantile_multi(self):
51-
from numpy import percentile
5250

5351
qs = [.1, .9]
5452
result = self.ts.quantile(qs)
55-
expected = pd.Series([percentile(self.ts.valid(), 10),
56-
percentile(self.ts.valid(), 90)],
53+
expected = pd.Series([np.percentile(self.ts.valid(), 10),
54+
np.percentile(self.ts.valid(), 90)],
5755
index=qs, name=self.ts.name)
5856
tm.assert_series_equal(result, expected)
5957

@@ -70,50 +68,44 @@ def test_quantile_multi(self):
7068
[], dtype=float))
7169
tm.assert_series_equal(result, expected)
7270

71+
@pytest.mark.skipif(_np_version_under1p9,
72+
reason="Numpy version is under 1.9")
7373
def test_quantile_interpolation(self):
7474
# GH #10174
75-
if _np_version_under1p9:
76-
pytest.skip("Numpy version is under 1.9")
77-
78-
from numpy import percentile
7975

8076
# interpolation = linear (default case)
8177
q = self.ts.quantile(0.1, interpolation='linear')
82-
self.assertEqual(q, percentile(self.ts.valid(), 10))
78+
self.assertEqual(q, np.percentile(self.ts.valid(), 10))
8379
q1 = self.ts.quantile(0.1)
84-
self.assertEqual(q1, percentile(self.ts.valid(), 10))
80+
self.assertEqual(q1, np.percentile(self.ts.valid(), 10))
8581

8682
# test with and without interpolation keyword
8783
self.assertEqual(q, q1)
8884

85+
@pytest.mark.skipif(_np_version_under1p9,
86+
reason="Numpy version is under 1.9")
8987
def test_quantile_interpolation_dtype(self):
9088
# GH #10174
91-
if _np_version_under1p9:
92-
pytest.skip("Numpy version is under 1.9")
93-
94-
from numpy import percentile
9589

9690
# interpolation = linear (default case)
9791
q = pd.Series([1, 3, 4]).quantile(0.5, interpolation='lower')
98-
self.assertEqual(q, percentile(np.array([1, 3, 4]), 50))
92+
self.assertEqual(q, np.percentile(np.array([1, 3, 4]), 50))
9993
self.assertTrue(is_integer(q))
10094

10195
q = pd.Series([1, 3, 4]).quantile(0.5, interpolation='higher')
102-
self.assertEqual(q, percentile(np.array([1, 3, 4]), 50))
96+
self.assertEqual(q, np.percentile(np.array([1, 3, 4]), 50))
10397
self.assertTrue(is_integer(q))
10498

99+
@pytest.mark.skipif(not _np_version_under1p9,
100+
reason="Numpy version is greater 1.9")
105101
def test_quantile_interpolation_np_lt_1p9(self):
106102
# GH #10174
107-
if not _np_version_under1p9:
108-
pytest.skip("Numpy version is greater than 1.9")
109-
110-
from numpy import percentile
111103

112104
# interpolation = linear (default case)
113105
q = self.ts.quantile(0.1, interpolation='linear')
114-
self.assertEqual(q, percentile(self.ts.valid(), 10))
106+
self.assertEqual(q, np.percentile(self.ts.valid(), 10))
115107
q1 = self.ts.quantile(0.1)
116-
self.assertEqual(q1, percentile(self.ts.valid(), 10))
108+
self.assertEqual(q1, np.percentile(self.ts.valid(), 10))
117109

118110
# interpolation other than linear
119111
expErrMsg = "Interpolation methods other than "

pandas/tests/sparse/test_frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from pandas.sparse.libsparse import BlockIndex, IntIndex
2424
from pandas.sparse.api import SparseSeries, SparseDataFrame, SparseArray
25-
from pandas.tests.frame.test_misc_api import SharedWithSparse
25+
from pandas.tests.frame.test_api import SharedWithSparse
2626

2727
from pandas.tests.sparse.common import spmatrix # noqa: F401
2828

pandas/tests/sparse/test_series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from pandas.sparse.libsparse import BlockIndex, IntIndex
2020
from pandas.sparse.api import SparseSeries
21-
from pandas.tests.series.test_misc_api import SharedWithSparse
21+
from pandas.tests.series.test_api import SharedWithSparse
2222

2323

2424
def _test_data1():

pandas/tseries/index.py

+3-1
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)