-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
return series | ||
|
||
@cache_readonly | ||
def objSeries(self): | ||
objSeries = tm.makeObjectSeries() | ||
objSeries.name = 'objects' | ||
return objSeries | ||
|
||
@cache_readonly | ||
def empty(self): | ||
return pd.Series([], index=[]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 bycache_readonly
) in between unit tests. same with the DataFrame tests