Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST: clean up series/frame api tests inheritance a bit #15949

Merged
merged 2 commits into from
Apr 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,7 @@ Conversion
- Bug in ``DataFrame.fillna()`` with tz-aware datetimes (:issue:`15855`)
- 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`)
- Bug in the return type of ``pd.unique`` on a ``Categorical``, which was returning an ndarray and not a ``Categorical`` (:issue:`15903`)
- Bug in ``Index.to_series()`` where the index was not copied (and so mutating later would change the original), (:issue:`15949`)

Indexing
^^^^^^^^
Expand Down
4 changes: 3 additions & 1 deletion pandas/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,9 @@ def to_series(self, **kwargs):
"""

from pandas import Series
return Series(self._to_embed(), index=self, name=self.name)
return Series(self._to_embed(),
index=self._shallow_copy(),
name=self.name)

def _to_embed(self, keep_tz=False):
"""
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_query_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ def test_date_index_query_with_NaT_duplicates(self):
df = DataFrame(d)
df.loc[np.random.rand(n) > 0.5, 'dates1'] = pd.NaT
df.set_index('dates1', inplace=True, drop=True)
res = df.query('index < 20130101 < dates3', engine=engine,
res = df.query('dates1 < 20130101 < dates3', engine=engine,
parser=parser)
expec = df[(df.index.to_series() < '20130101') &
('20130101' < df.dates3)]
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ def test_pickle_compat_construction(self):
# need an object to create with
self.assertRaises(TypeError, self._holder)

def test_to_series(self):
# assert that we are creating a copy of the index

idx = self.create_index()
s = idx.to_series()
assert s.values is not idx.values
assert s.index is not idx
assert s.name == idx.name

def test_shift(self):

# GH8083 test the base class for shift
Expand Down
File renamed without changes.
42 changes: 17 additions & 25 deletions pandas/tests/series/test_quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@
class TestSeriesQuantile(TestData, tm.TestCase):

def test_quantile(self):
from numpy import percentile

q = self.ts.quantile(0.1)
self.assertEqual(q, percentile(self.ts.valid(), 10))
self.assertEqual(q, np.percentile(self.ts.valid(), 10))

q = self.ts.quantile(0.9)
self.assertEqual(q, percentile(self.ts.valid(), 90))
self.assertEqual(q, np.percentile(self.ts.valid(), 90))

# object dtype
q = Series(self.ts, dtype=object).quantile(0.9)
self.assertEqual(q, percentile(self.ts.valid(), 90))
self.assertEqual(q, np.percentile(self.ts.valid(), 90))

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

def test_quantile_multi(self):
from numpy import percentile

qs = [.1, .9]
result = self.ts.quantile(qs)
expected = pd.Series([percentile(self.ts.valid(), 10),
percentile(self.ts.valid(), 90)],
expected = pd.Series([np.percentile(self.ts.valid(), 10),
np.percentile(self.ts.valid(), 90)],
index=qs, name=self.ts.name)
tm.assert_series_equal(result, expected)

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

@pytest.mark.skipif(_np_version_under1p9,
reason="Numpy version is under 1.9")
def test_quantile_interpolation(self):
# GH #10174
if _np_version_under1p9:
pytest.skip("Numpy version is under 1.9")

from numpy import percentile

# interpolation = linear (default case)
q = self.ts.quantile(0.1, interpolation='linear')
self.assertEqual(q, percentile(self.ts.valid(), 10))
self.assertEqual(q, np.percentile(self.ts.valid(), 10))
q1 = self.ts.quantile(0.1)
self.assertEqual(q1, percentile(self.ts.valid(), 10))
self.assertEqual(q1, np.percentile(self.ts.valid(), 10))

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

@pytest.mark.skipif(_np_version_under1p9,
reason="Numpy version is under 1.9")
def test_quantile_interpolation_dtype(self):
# GH #10174
if _np_version_under1p9:
pytest.skip("Numpy version is under 1.9")

from numpy import percentile

# interpolation = linear (default case)
q = pd.Series([1, 3, 4]).quantile(0.5, interpolation='lower')
self.assertEqual(q, percentile(np.array([1, 3, 4]), 50))
self.assertEqual(q, np.percentile(np.array([1, 3, 4]), 50))
self.assertTrue(is_integer(q))

q = pd.Series([1, 3, 4]).quantile(0.5, interpolation='higher')
self.assertEqual(q, percentile(np.array([1, 3, 4]), 50))
self.assertEqual(q, np.percentile(np.array([1, 3, 4]), 50))
self.assertTrue(is_integer(q))

@pytest.mark.skipif(not _np_version_under1p9,
reason="Numpy version is greater 1.9")
def test_quantile_interpolation_np_lt_1p9(self):
# GH #10174
if not _np_version_under1p9:
pytest.skip("Numpy version is greater than 1.9")

from numpy import percentile

# interpolation = linear (default case)
q = self.ts.quantile(0.1, interpolation='linear')
self.assertEqual(q, percentile(self.ts.valid(), 10))
self.assertEqual(q, np.percentile(self.ts.valid(), 10))
q1 = self.ts.quantile(0.1)
self.assertEqual(q1, percentile(self.ts.valid(), 10))
self.assertEqual(q1, np.percentile(self.ts.valid(), 10))

# interpolation other than linear
expErrMsg = "Interpolation methods other than "
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/sparse/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from pandas.sparse.libsparse import BlockIndex, IntIndex
from pandas.sparse.api import SparseSeries, SparseDataFrame, SparseArray
from pandas.tests.frame.test_misc_api import SharedWithSparse
from pandas.tests.frame.test_api import SharedWithSparse

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

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/sparse/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from pandas.sparse.libsparse import BlockIndex, IntIndex
from pandas.sparse.api import SparseSeries
from pandas.tests.series.test_misc_api import SharedWithSparse
from pandas.tests.series.test_api import SharedWithSparse


def _test_data1():
Expand Down
4 changes: 3 additions & 1 deletion pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,9 @@ def to_series(self, keep_tz=False):
Series
"""
from pandas import Series
return Series(self._to_embed(keep_tz), index=self, name=self.name)
return Series(self._to_embed(keep_tz),
index=self._shallow_copy(),
name=self.name)

def _to_embed(self, keep_tz=False):
"""
Expand Down