15
15
import pandas .core .datetools as datetools
16
16
from pandas .util import py3compat
17
17
from pandas .util .testing import assert_series_equal , assert_almost_equal
18
- import pandas .util .testing as common
18
+ import pandas .util .testing as tm
19
19
20
20
#-------------------------------------------------------------------------------
21
21
# Series test cases
@@ -138,13 +138,13 @@ def test_to_sparse_pass_name(self):
138
138
class TestSeries (unittest .TestCase , CheckNameIntegration ):
139
139
140
140
def setUp (self ):
141
- self .ts = common .makeTimeSeries ()
141
+ self .ts = tm .makeTimeSeries ()
142
142
self .ts .name = 'ts'
143
143
144
- self .series = common .makeStringSeries ()
144
+ self .series = tm .makeStringSeries ()
145
145
self .series .name = 'series'
146
146
147
- self .objSeries = common .makeObjectSeries ()
147
+ self .objSeries = tm .makeObjectSeries ()
148
148
self .objSeries .name = 'objects'
149
149
150
150
self .empty = Series ([], index = [])
@@ -157,7 +157,7 @@ def test_constructor(self):
157
157
derived = Series (self .ts )
158
158
self .assert_ (isinstance (derived , TimeSeries ))
159
159
160
- self .assert_ (common .equalContents (derived .index , self .ts .index ))
160
+ self .assert_ (tm .equalContents (derived .index , self .ts .index ))
161
161
# Ensure new index is not created
162
162
self .assertEquals (id (self .ts .index ), id (derived .index ))
163
163
@@ -181,7 +181,7 @@ def test_constructor_default_index(self):
181
181
assert_almost_equal (s .index , np .arange (3 ))
182
182
183
183
def test_constructor_corner (self ):
184
- df = common .makeTimeDataFrame ()
184
+ df = tm .makeTimeDataFrame ()
185
185
objs = [df , df ]
186
186
s = Series (objs , index = [0 , 1 ])
187
187
self .assert_ (isinstance (s , Series ))
@@ -204,7 +204,7 @@ def test_fromDict(self):
204
204
data = {'a' : 0 , 'b' : 1 , 'c' : 2 , 'd' : 3 }
205
205
206
206
series = Series (data )
207
- self .assert_ (common .is_sorted (series .index ))
207
+ self .assert_ (tm .is_sorted (series .index ))
208
208
209
209
data = {'a' : 0 , 'b' : '1' , 'c' : '2' , 'd' : datetime .now ()}
210
210
series = Series (data )
@@ -251,7 +251,7 @@ def test_fromValue(self):
251
251
self .assertEqual (len (dates ), len (self .ts ))
252
252
253
253
def test_contains (self ):
254
- common .assert_contains_all (self .ts .index , self .ts )
254
+ tm .assert_contains_all (self .ts .index , self .ts )
255
255
256
256
def test_pickle (self ):
257
257
unp_series = self ._pickle_roundtrip (self .series )
@@ -365,7 +365,7 @@ def test_slice(self):
365
365
366
366
self .assertEqual (numSlice .index [1 ], self .series .index [11 ])
367
367
368
- self .assert_ (common .equalContents (numSliceEnd ,
368
+ self .assert_ (tm .equalContents (numSliceEnd ,
369
369
np .array (self .series )[- 10 :]))
370
370
371
371
# test return view
@@ -383,8 +383,8 @@ def test_setitem(self):
383
383
self .assert_ (not np .isnan (self .ts [2 ]))
384
384
385
385
# caught this bug when writing tests
386
- series = Series (common .makeIntIndex (20 ).astype (float ),
387
- index = common .makeIntIndex (20 ))
386
+ series = Series (tm .makeIntIndex (20 ).astype (float ),
387
+ index = tm .makeIntIndex (20 ))
388
388
389
389
series [::2 ] = 0
390
390
self .assert_ ((series [::2 ] == 0 ).all ())
@@ -469,7 +469,7 @@ def test_repr(self):
469
469
str (self .series .astype (int ))
470
470
str (self .objSeries )
471
471
472
- str (Series (common .randn (1000 ), index = np .arange (1000 )))
472
+ str (Series (tm .randn (1000 ), index = np .arange (1000 )))
473
473
474
474
# empty
475
475
str (self .empty )
@@ -647,7 +647,7 @@ def _check_op(other, op):
647
647
cython_or_numpy = op (series , other )
648
648
python = series .combine (other , op )
649
649
650
- common .assert_almost_equal (cython_or_numpy , python )
650
+ tm .assert_almost_equal (cython_or_numpy , python )
651
651
652
652
def check (other ):
653
653
_check_op (other , operator .add )
@@ -706,8 +706,6 @@ def test_operators_corner(self):
706
706
self .assert_ (np .array_equal (added [:- 5 ], expected ))
707
707
708
708
def test_operators_reverse_object (self ):
709
- from pandas .util .testing import rands
710
-
711
709
# GH 56
712
710
arr = Series (np .random .randn (10 ), index = np .arange (10 ),
713
711
dtype = object )
@@ -723,18 +721,26 @@ def _check_op(arr, op):
723
721
_check_op (arr , operator .truediv )
724
722
_check_op (arr , operator .floordiv )
725
723
724
+ def test_series_frame_radd_bug (self ):
725
+ from pandas .util .testing import rands
726
+
726
727
# GH 353
727
728
vals = Series ([rands (5 ) for _ in xrange (10 )])
728
729
result = 'foo_' + vals
729
730
expected = vals .map (lambda x : 'foo_' + x )
730
731
assert_series_equal (result , expected )
731
732
733
+ frame = DataFrame ({'vals' : vals })
734
+ result = 'foo_' + frame
735
+ expected = DataFrame ({'vals' : vals .map (lambda x : 'foo_' + x )})
736
+ tm .assert_frame_equal (result , expected )
737
+
732
738
def test_operators_frame (self ):
733
739
# rpow does not work with DataFrame
734
740
df = DataFrame ({'A' : self .ts })
735
741
736
- common .assert_almost_equal (self .ts + self .ts , (self .ts + df )['A' ])
737
- common .assert_almost_equal (self .ts ** self .ts , (self .ts ** df )['A' ])
742
+ tm .assert_almost_equal (self .ts + self .ts , (self .ts + df )['A' ])
743
+ tm .assert_almost_equal (self .ts ** self .ts , (self .ts ** df )['A' ])
738
744
739
745
def test_operators_combine (self ):
740
746
def _check_fill (meth , op , a , b , fill_value = 0 ):
@@ -782,8 +788,8 @@ def _check_fill(meth, op, a, b, fill_value=0):
782
788
_check_fill (op , equiv_op , a , b , fill_value = fv )
783
789
784
790
def test_combine_first (self ):
785
- values = common .makeIntIndex (20 ).values .astype (float )
786
- series = Series (values , index = common .makeIntIndex (20 ))
791
+ values = tm .makeIntIndex (20 ).values .astype (float )
792
+ series = Series (values , index = tm .makeIntIndex (20 ))
787
793
788
794
series_copy = series * 2
789
795
series_copy [::2 ] = np .NaN
@@ -801,14 +807,14 @@ def test_combine_first(self):
801
807
self .assert_ (np .array_equal (combined [1 ::2 ], series_copy [1 ::2 ]))
802
808
803
809
# mixed types
804
- index = common .makeStringIndex (20 )
805
- floats = Series (common .randn (20 ), index = index )
806
- strings = Series (common .makeStringIndex (10 ), index = index [::2 ])
810
+ index = tm .makeStringIndex (20 )
811
+ floats = Series (tm .randn (20 ), index = index )
812
+ strings = Series (tm .makeStringIndex (10 ), index = index [::2 ])
807
813
808
814
combined = strings .combine_first (floats )
809
815
810
- common .assert_dict_equal (strings , combined , compare_keys = False )
811
- common .assert_dict_equal (floats [1 ::2 ], combined , compare_keys = False )
816
+ tm .assert_dict_equal (strings , combined , compare_keys = False )
817
+ tm .assert_dict_equal (floats [1 ::2 ], combined , compare_keys = False )
812
818
813
819
# corner case
814
820
s = Series ([1. , 2 , 3 ], index = [0 , 1 , 2 ])
@@ -939,7 +945,7 @@ def test_valid(self):
939
945
result = ts .valid ()
940
946
self .assertEqual (len (result ), ts .count ())
941
947
942
- common .assert_dict_equal (result , ts , compare_keys = False )
948
+ tm .assert_dict_equal (result , ts , compare_keys = False )
943
949
944
950
def test_isnull (self ):
945
951
ser = Series ([0 ,5.4 ,3 ,nan ,- 0.001 ])
@@ -957,7 +963,7 @@ def test_shift(self):
957
963
shifted = self .ts .shift (1 )
958
964
unshifted = shifted .shift (- 1 )
959
965
960
- common .assert_dict_equal (unshifted .valid (), self .ts , compare_keys = False )
966
+ tm .assert_dict_equal (unshifted .valid (), self .ts , compare_keys = False )
961
967
962
968
offset = datetools .bday
963
969
shifted = self .ts .shift (1 , offset = offset )
@@ -1055,7 +1061,7 @@ def test_asof(self):
1055
1061
self .assert_ (np .isnan (self .ts .asof (d )))
1056
1062
1057
1063
def test_map (self ):
1058
- index , data = common .getMixedTypeDict ()
1064
+ index , data = tm .getMixedTypeDict ()
1059
1065
1060
1066
source = Series (data ['B' ], index = data ['C' ])
1061
1067
target = Series (data ['C' ][:4 ], index = data ['D' ][:4 ])
@@ -1262,8 +1268,8 @@ def test_preserveRefs(self):
1262
1268
def test_ne (self ):
1263
1269
ts = TimeSeries ([3 , 4 , 5 , 6 , 7 ], [3 , 4 , 5 , 6 , 7 ], dtype = float )
1264
1270
expected = [True , True , False , True , True ]
1265
- self .assert_ (common .equalContents (ts .index != 5 , expected ))
1266
- self .assert_ (common .equalContents (~ (ts .index == 5 ), expected ))
1271
+ self .assert_ (tm .equalContents (ts .index != 5 , expected ))
1272
+ self .assert_ (tm .equalContents (~ (ts .index == 5 ), expected ))
1267
1273
1268
1274
def test_pad_nan (self ):
1269
1275
x = TimeSeries ([np .nan , 1. , np .nan , 3. , np .nan ],
@@ -1320,7 +1326,7 @@ def test_isin(self):
1320
1326
# TimeSeries-specific
1321
1327
1322
1328
def test_fillna (self ):
1323
- ts = Series ([0. , 1. , 2. , 3. , 4. ], index = common .makeDateIndex (5 ))
1329
+ ts = Series ([0. , 1. , 2. , 3. , 4. ], index = tm .makeDateIndex (5 ))
1324
1330
1325
1331
self .assert_ (np .array_equal (ts , ts .fillna ()))
1326
1332
0 commit comments