Skip to content

REF: reorganize pandas/tests/test_series.py #12130

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

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 2 additions & 2 deletions pandas/sparse/tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from pandas.sparse.tests.test_array import assert_sp_array_equal

import pandas.tests.test_panel as test_panel
import pandas.tests.test_series as test_series
from pandas.tests.series.test_misc_api import SharedWithSparse

dec = np.testing.dec

Expand Down Expand Up @@ -116,7 +116,7 @@ def assert_sp_panel_equal(left, right, exact_indices=True):
assert (item in left)


class TestSparseSeries(tm.TestCase, test_series.CheckNameIntegration):
class TestSparseSeries(tm.TestCase, SharedWithSparse):
_multiprocess_can_split_ = True

def setUp(self):
Expand Down
Empty file added pandas/tests/series/__init__.py
Empty file.
30 changes: 30 additions & 0 deletions pandas/tests/series/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from pandas.util.decorators import cache_readonly
import pandas.util.testing as tm
import pandas as pd

_ts = tm.makeTimeSeries()


class TestData(object):

@cache_readonly
def ts(self):
ts = _ts.copy()
ts.name = 'ts'
return ts

@cache_readonly
def series(self):
series = tm.makeStringSeries()
series.name = 'series'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not directly related, and maybe I am missing something, but these have to be strictly read only, enforced by the user. IOW. If in a single test class I modify one of these (say set an element of the Series), won't that persist? In the prior impl,, I believe these were recreated each time in setUp. So no possibility of that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, good point; to be on the safe side we should clear() the _cache variable (set by cache_readonly) in between unit tests. same with the DataFrame tests

return series

@cache_readonly
def objSeries(self):
objSeries = tm.makeObjectSeries()
objSeries.name = 'objects'
return objSeries

@cache_readonly
def empty(self):
return pd.Series([], index=[])
148 changes: 148 additions & 0 deletions pandas/tests/series/test_alter_axes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# coding=utf-8
# pylint: disable-msg=E1101,W0612

import numpy as np
import pandas as pd

from pandas import Index, Series
from pandas.core.index import MultiIndex, RangeIndex

from pandas.compat import lrange, range, zip
from pandas.util.testing import assert_series_equal, assert_frame_equal
import pandas.util.testing as tm

from .common import TestData


class TestSeriesAlterAxes(TestData, tm.TestCase):

_multiprocess_can_split_ = True

def test_setindex(self):
# wrong type
series = self.series.copy()
self.assertRaises(TypeError, setattr, series, 'index', None)

# wrong length
series = self.series.copy()
self.assertRaises(Exception, setattr, series, 'index',
np.arange(len(series) - 1))

# works
series = self.series.copy()
series.index = np.arange(len(series))
tm.assertIsInstance(series.index, Index)

def test_rename(self):
renamer = lambda x: x.strftime('%Y%m%d')
renamed = self.ts.rename(renamer)
self.assertEqual(renamed.index[0], renamer(self.ts.index[0]))

# dict
rename_dict = dict(zip(self.ts.index, renamed.index))
renamed2 = self.ts.rename(rename_dict)
assert_series_equal(renamed, renamed2)

# partial dict
s = Series(np.arange(4), index=['a', 'b', 'c', 'd'], dtype='int64')
renamed = s.rename({'b': 'foo', 'd': 'bar'})
self.assert_numpy_array_equal(renamed.index, ['a', 'foo', 'c', 'bar'])

# index with name
renamer = Series(np.arange(4),
index=Index(['a', 'b', 'c', 'd'], name='name'),
dtype='int64')
renamed = renamer.rename({})
self.assertEqual(renamed.index.name, renamer.index.name)

def test_rename_inplace(self):
renamer = lambda x: x.strftime('%Y%m%d')
expected = renamer(self.ts.index[0])

self.ts.rename(renamer, inplace=True)
self.assertEqual(self.ts.index[0], expected)

def test_set_index_makes_timeseries(self):
idx = tm.makeDateIndex(10)

s = Series(lrange(10))
s.index = idx

with tm.assert_produces_warning(FutureWarning):
self.assertTrue(s.is_time_series)
self.assertTrue(s.index.is_all_dates)

def test_reset_index(self):
df = tm.makeDataFrame()[:5]
ser = df.stack()
ser.index.names = ['hash', 'category']

ser.name = 'value'
df = ser.reset_index()
self.assertIn('value', df)

df = ser.reset_index(name='value2')
self.assertIn('value2', df)

# check inplace
s = ser.reset_index(drop=True)
s2 = ser
s2.reset_index(drop=True, inplace=True)
assert_series_equal(s, s2)

# level
index = MultiIndex(levels=[['bar'], ['one', 'two', 'three'], [0, 1]],
labels=[[0, 0, 0, 0, 0, 0], [0, 1, 2, 0, 1, 2],
[0, 1, 0, 1, 0, 1]])
s = Series(np.random.randn(6), index=index)
rs = s.reset_index(level=1)
self.assertEqual(len(rs.columns), 2)

rs = s.reset_index(level=[0, 2], drop=True)
self.assertTrue(rs.index.equals(Index(index.get_level_values(1))))
tm.assertIsInstance(rs, Series)

def test_reset_index_range(self):
# GH 12071
s = pd.Series(range(2), name='A', dtype='int64')
series_result = s.reset_index()
tm.assertIsInstance(series_result.index, RangeIndex)
series_expected = pd.DataFrame([[0, 0], [1, 1]],
columns=['index', 'A'],
index=RangeIndex(stop=2))
assert_frame_equal(series_result, series_expected)

def test_reorder_levels(self):
index = MultiIndex(levels=[['bar'], ['one', 'two', 'three'], [0, 1]],
labels=[[0, 0, 0, 0, 0, 0], [0, 1, 2, 0, 1, 2],
[0, 1, 0, 1, 0, 1]],
names=['L0', 'L1', 'L2'])
s = Series(np.arange(6), index=index)

# no change, position
result = s.reorder_levels([0, 1, 2])
assert_series_equal(s, result)

# no change, labels
result = s.reorder_levels(['L0', 'L1', 'L2'])
assert_series_equal(s, result)

# rotate, position
result = s.reorder_levels([1, 2, 0])
e_idx = MultiIndex(levels=[['one', 'two', 'three'], [0, 1], ['bar']],
labels=[[0, 1, 2, 0, 1, 2], [0, 1, 0, 1, 0, 1],
[0, 0, 0, 0, 0, 0]],
names=['L1', 'L2', 'L0'])
expected = Series(np.arange(6), index=e_idx)
assert_series_equal(result, expected)

result = s.reorder_levels([0, 0, 0])
e_idx = MultiIndex(levels=[['bar'], ['bar'], ['bar']],
labels=[[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]],
names=['L0', 'L0', 'L0'])
expected = Series(range(6), index=e_idx)
assert_series_equal(result, expected)

result = s.reorder_levels(['L0', 'L0', 'L0'])
assert_series_equal(result, expected)
Loading