<s> import numpy as np <EOL> import pandas as pd <EOL> from pandas import Index , Series , DataFrame , date_range <EOL> from pandas . tseries . index import Timestamp <EOL> from pandas . compat import range <EOL> from pandas import compat <EOL> import pandas . formats . printing as printing <EOL> from pandas . util . testing import ( assert_series_equal , <EOL> ensure_clean ) <EOL> import pandas . util . testing as tm <EOL> from . common import TestData <EOL> class SharedWithSparse ( object ) : <EOL> def test_scalarop_preserve_name ( self ) : <EOL> result = self . ts * <NUM_LIT:2> <EOL> self . assertEqual ( result . name , self . ts . name ) <EOL> def test_copy_name ( self ) : <EOL> result = self . ts . copy ( ) <EOL> self . assertEqual ( result . name , self . ts . name ) <EOL> def test_copy_index_name_checking ( self ) : <EOL> self . ts . index . name = None <EOL> self . assertIsNone ( self . ts . index . name ) <EOL> self . assertIs ( self . ts , self . ts ) <EOL> cp = self . ts . copy ( ) <EOL> cp . index . name = '<STR_LIT:foo>' <EOL> printing . pprint_thing ( self . ts . index . name ) <EOL> self . assertIsNone ( self . ts . index . name ) <EOL> def test_append_preserve_name ( self ) : <EOL> result = self . ts [ : <NUM_LIT:5> ] . append ( self . ts [ <NUM_LIT:5> : ] ) <EOL> self . assertEqual ( result . name , self . ts . name ) <EOL> def test_binop_maybe_preserve_name ( self ) : <EOL> result = self . ts * self . ts <EOL> self . assertEqual ( result . name , self . ts . name ) <EOL> result = self . ts . mul ( self . ts ) <EOL> self . assertEqual ( result . name , self . ts . name ) <EOL> result = self . ts * self . ts [ : - <NUM_LIT:2> ] <EOL> self . assertEqual ( result . name , self . ts . name ) <EOL> cp = self . ts . copy ( ) <EOL> cp . name = '<STR_LIT>' <EOL> result = self . ts + cp <EOL> self . assertIsNone ( result . name ) <EOL> result = self . ts . add ( cp ) <EOL> self . assertIsNone ( result . name ) <EOL> ops = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ops = ops + [ '<STR_LIT:r>' + op for op in ops ] <EOL> for op in ops : <EOL> s = self . ts . copy ( ) <EOL> result = getattr ( s , op ) ( s ) <EOL> self . assertEqual ( result . name , self . ts . name ) <EOL> cp = self . ts . copy ( ) <EOL> cp . name = '<STR_LIT>' <EOL> result = getattr ( s , op ) ( cp ) <EOL> self . assertIsNone ( result . name ) <EOL> def test_combine_first_name ( self ) : <EOL> result = self . ts . combine_first ( self . ts [ : <NUM_LIT:5> ] ) <EOL> self . assertEqual ( result . name , self . ts . name ) <EOL> def test_getitem_preserve_name ( self ) : <EOL> result = self . ts [ self . ts > <NUM_LIT:0> ] <EOL> self . assertEqual ( result . name , self . ts . name ) <EOL> result = self . ts [ [ <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:4> ] ] <EOL> self . assertEqual ( result . name , self . ts . name ) <EOL> result = self . ts [ <NUM_LIT:5> : <NUM_LIT:10> ] <EOL> self . assertEqual ( result . name , self . ts . name ) <EOL> def test_pickle ( self ) : <EOL> unp_series = self . _pickle_roundtrip ( self . series ) <EOL> unp_ts = self . _pickle_roundtrip ( self . ts ) <EOL> assert_series_equal ( unp_series , self . series ) <EOL> assert_series_equal ( unp_ts , self . ts ) <EOL> def _pickle_roundtrip ( self , obj ) : <EOL> with ensure_clean ( ) as path : <EOL> obj . to_pickle ( path ) <EOL> unpickled = pd . read_pickle ( path ) <EOL> return unpickled <EOL> def test_argsort_preserve_name ( self ) : <EOL> result = self . ts . argsort ( ) <EOL> self . assertEqual ( result . name , self . ts . name ) <EOL> def test_sort_index_name ( self ) : <EOL> result = self . ts . sort_index ( ascending = False ) <EOL> self . assertEqual ( result . name , self . ts . name ) <EOL> def test_to_sparse_pass_name ( self ) : <EOL> result = self . ts . to_sparse ( ) <EOL> self . assertEqual ( result . name , self . ts . name ) <EOL> class TestSeriesMisc ( TestData , SharedWithSparse , tm . TestCase ) : <EOL> _multiprocess_can_split_ = True <EOL> def test_tab_completion ( self ) : <EOL> s = Series ( list ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( '<STR_LIT:str>' in dir ( s ) ) <EOL> self . assertTrue ( '<STR_LIT>' not in dir ( s ) ) <EOL> self . assertTrue ( '<STR_LIT>' not in dir ( s ) ) <EOL> s = Series ( date_range ( '<STR_LIT>' , periods = <NUM_LIT:5> ) ) <EOL> self . assertTrue ( '<STR_LIT>' in dir ( s ) ) <EOL> self . assertTrue ( '<STR_LIT:str>' not in dir ( s ) ) <EOL> self . assertTrue ( '<STR_LIT>' not in dir ( s ) ) <EOL> s = Series ( list ( '<STR_LIT>' ) , dtype = "<STR_LIT>" ) <EOL> self . assertTrue ( '<STR_LIT>' in dir ( s ) ) <EOL> self . assertTrue ( '<STR_LIT:str>' in dir ( s ) ) <EOL> self . assertTrue ( '<STR_LIT>' not in dir ( s ) ) <EOL> s = Series ( date_range ( '<STR_LIT>' , periods = <NUM_LIT:5> ) ) . astype ( "<STR_LIT>" ) <EOL> self . assertTrue ( '<STR_LIT>' in dir ( s ) ) <EOL> self . assertTrue ( '<STR_LIT:str>' not in dir ( s ) ) <EOL> self . assertTrue ( '<STR_LIT>' in dir ( s ) ) <EOL> def test_not_hashable ( self ) : <EOL> s_empty = Series ( ) <EOL> s = Series ( [ <NUM_LIT:1> ] ) <EOL> self . assertRaises ( TypeError , hash , s_empty ) <EOL> self . assertRaises ( TypeError , hash , s ) <EOL> def test_contains ( self ) : <EOL> tm . assert_contains_all ( self . ts . index , self . ts ) <EOL> def test_iter ( self ) : <EOL> for i , val in enumerate ( self . series ) : <EOL> self . assertEqual ( val , self . series [ i ] ) <EOL> for i , val in enumerate ( self . ts ) : <EOL> self . assertEqual ( val , self . ts [ i ] ) <EOL> def test_iter_box ( self ) : <EOL> vals = [ pd . Timestamp ( '<STR_LIT>' ) , pd . Timestamp ( '<STR_LIT>' ) ] <EOL> s = pd . Series ( vals ) <EOL> self . assertEqual ( s . dtype , '<STR_LIT>' ) <EOL> for res , exp in zip ( s , vals ) : <EOL> self . assertIsInstance ( res , pd . Timestamp ) <EOL> self . assertEqual ( res , exp ) <EOL> self . assertIsNone ( res . tz ) <EOL> vals = [ pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) , <EOL> pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) ] <EOL> s = pd . Series ( vals ) <EOL> self . assertEqual ( s . dtype , '<STR_LIT>' ) <EOL> for res , exp in zip ( s , vals ) : <EOL> self . assertIsInstance ( res , pd . Timestamp ) <EOL> self . assertEqual ( res , exp ) <EOL> self . assertEqual ( res . tz , exp . tz ) <EOL> vals = [ pd . Timedelta ( '<STR_LIT>' ) , pd . Timedelta ( '<STR_LIT>' ) ] <EOL> s = pd . Series ( vals ) <EOL> self . assertEqual ( s . dtype , '<STR_LIT>' ) <EOL> for res , exp in zip ( s , vals ) : <EOL> self . assertIsInstance ( res , pd . Timedelta ) <EOL> self . assertEqual ( res , exp ) <EOL> vals = [ pd . Period ( '<STR_LIT>' , freq = '<STR_LIT:M>' ) , <EOL> pd . Period ( '<STR_LIT>' , freq = '<STR_LIT:M>' ) ] <EOL> s = pd . Series ( vals ) <EOL> self . assertEqual ( s . dtype , '<STR_LIT:object>' ) <EOL> for res , exp in zip ( s , vals ) : <EOL> self . assertIsInstance ( res , pd . Period ) <EOL> self . assertEqual ( res , exp ) <EOL> self . assertEqual ( res . freq , '<STR_LIT:M>' ) <EOL> def test_keys ( self ) : <EOL> getkeys = self . ts . keys <EOL> self . assertIs ( getkeys ( ) , self . ts . index ) <EOL> def test_values ( self ) : <EOL> self . assert_numpy_array_equal ( self . ts , self . ts . values ) <EOL> def test_iteritems ( self ) : <EOL> for idx , val in compat . iteritems ( self . series ) : <EOL> self . assertEqual ( val , self . series [ idx ] ) <EOL> for idx , val in compat . iteritems ( self . ts ) : <EOL> self . assertEqual ( val , self . ts [ idx ] ) <EOL> self . assertFalse ( hasattr ( self . series . iteritems ( ) , '<STR_LIT>' ) ) <EOL> def test_raise_on_info ( self ) : <EOL> s = Series ( np . random . randn ( <NUM_LIT:10> ) ) <EOL> with tm . assertRaises ( AttributeError ) : <EOL> s . info ( ) <EOL> def test_copy ( self ) : <EOL> for deep in [ None , False , True ] : <EOL> s = Series ( np . arange ( <NUM_LIT:10> ) , dtype = '<STR_LIT>' ) <EOL> if deep is None : <EOL> s2 = s . copy ( ) <EOL> else : <EOL> s2 = s . copy ( deep = deep ) <EOL> s2 [ : : <NUM_LIT:2> ] = np . NaN <EOL> if deep is None or deep is True : <EOL> self . assertTrue ( np . isnan ( s2 [ <NUM_LIT:0> ] ) ) <EOL> self . assertFalse ( np . isnan ( s [ <NUM_LIT:0> ] ) ) <EOL> else : <EOL> self . assertTrue ( np . isnan ( s2 [ <NUM_LIT:0> ] ) ) <EOL> self . assertTrue ( np . isnan ( s [ <NUM_LIT:0> ] ) ) <EOL> expected = Series ( [ Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) ] ) <EOL> expected2 = Series ( [ Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) ] ) <EOL> for deep in [ None , False , True ] : <EOL> s = Series ( [ Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) ] ) <EOL> if deep is None : <EOL> s2 = s . copy ( ) <EOL> else : <EOL> s2 = s . copy ( deep = deep ) <EOL> s2 [ <NUM_LIT:0> ] = pd . Timestamp ( '<STR_LIT>' , tz = '<STR_LIT>' ) <EOL> if deep is None or deep is True : <EOL> assert_series_equal ( s , expected ) <EOL> assert_series_equal ( s2 , expected2 ) <EOL> else : <EOL> assert_series_equal ( s , expected2 ) <EOL> assert_series_equal ( s2 , expected2 ) <EOL> def test_axis_alias ( self ) : <EOL> s = Series ( [ <NUM_LIT:1> , <NUM_LIT:2> , np . nan ] ) <EOL> assert_series_equal ( s . dropna ( axis = '<STR_LIT>' ) , s . dropna ( axis = '<STR_LIT:index>' ) ) <EOL> self . assertEqual ( s . dropna ( ) . sum ( '<STR_LIT>' ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( s . _get_axis_number ( '<STR_LIT>' ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( s . _get_axis_name ( '<STR_LIT>' ) , '<STR_LIT:index>' ) <EOL> def test_numpy_unique ( self ) : <EOL> np . unique ( self . ts ) <EOL> def test_ndarray_compat ( self ) : <EOL> tsdf = DataFrame ( np . random . randn ( <NUM_LIT:1000> , <NUM_LIT:3> ) , columns = [ '<STR_LIT:A>' , '<STR_LIT:B>' , '<STR_LIT:C>' ] , <EOL> index = date_range ( '<STR_LIT>' , periods = <NUM_LIT:1000> ) ) <EOL> def f ( x ) : <EOL> return x [ x . argmax ( ) ] <EOL> result = tsdf . apply ( f ) <EOL> expected = tsdf . max ( ) <EOL> assert_series_equal ( result , expected ) <EOL> s = Series ( [ <NUM_LIT:1> ] ) <EOL> result = s . item ( ) <EOL> self . assertEqual ( result , <NUM_LIT:1> ) <EOL> self . assertEqual ( s . item ( ) , s . iloc [ <NUM_LIT:0> ] ) <EOL> s = Series ( np . random . randn ( <NUM_LIT:10> ) ) <EOL> result = np . ones_like ( s ) <EOL> expected = Series ( <NUM_LIT:1> , index = range ( <NUM_LIT:10> ) , dtype = '<STR_LIT>' ) <EOL> s = Series ( np . random . randn ( <NUM_LIT:10> ) ) <EOL> tm . assert_almost_equal ( s . ravel ( order = '<STR_LIT:F>' ) , s . values . ravel ( order = '<STR_LIT:F>' ) ) <EOL> s = Series ( [ <NUM_LIT:0> , <NUM_LIT:1.> , - <NUM_LIT:1> ] , index = list ( '<STR_LIT:abc>' ) ) <EOL> result = np . compress ( s > <NUM_LIT:0> , s ) <EOL> assert_series_equal ( result , Series ( [ <NUM_LIT:1.> ] , index = [ '<STR_LIT:b>' ] ) ) <EOL> result = np . compress ( s < - <NUM_LIT:1> , s ) <EOL> exp = Series ( [ ] , dtype = '<STR_LIT>' , index = Index ( [ ] , dtype = '<STR_LIT:object>' ) ) <EOL> assert_series_equal ( result , exp ) <EOL> s = Series ( [ <NUM_LIT:0> , <NUM_LIT:1.> , - <NUM_LIT:1> ] , index = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> result = np . compress ( s > <NUM_LIT:0> , s ) <EOL> assert_series_equal ( result , Series ( [ <NUM_LIT:1.> ] , index = [ <NUM_LIT> ] ) ) <EOL> result = np . compress ( s < - <NUM_LIT:1> , s ) <EOL> exp = Series ( [ ] , dtype = '<STR_LIT>' , index = Index ( [ ] , dtype = '<STR_LIT>' ) ) <EOL> assert_series_equal ( result , exp ) <EOL> def test_str_attribute ( self ) : <EOL> methods = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> s = Series ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> for method in methods : <EOL> expected = Series ( [ getattr ( str , method ) ( x ) for x in s . values ] ) <EOL> assert_series_equal ( getattr ( Series . str , method ) ( s . str ) , expected ) <EOL> s = Series ( range ( <NUM_LIT:5> ) ) <EOL> with self . assertRaisesRegexp ( AttributeError , '<STR_LIT>' ) : <EOL> s . str . repeat ( <NUM_LIT:2> ) </s>
