17
17
import pandas .util .testing as tm
18
18
from pandas .conftest import _get_cython_table_params
19
19
20
- from .common import TestData
21
20
21
+ class TestSeriesApply ():
22
22
23
- class TestSeriesApply (TestData ):
24
-
25
- def test_apply (self ):
23
+ def test_apply (self , datetime_series ):
26
24
with np .errstate (all = 'ignore' ):
27
- tm .assert_series_equal (self .ts .apply (np .sqrt ), np .sqrt (self .ts ))
25
+ tm .assert_series_equal (datetime_series .apply (np .sqrt ),
26
+ np .sqrt (datetime_series ))
28
27
29
28
# element-wise apply
30
29
import math
31
- tm .assert_series_equal (self .ts .apply (math .exp ), np .exp (self .ts ))
30
+ tm .assert_series_equal (datetime_series .apply (math .exp ),
31
+ np .exp (datetime_series ))
32
32
33
33
# empty series
34
34
s = Series (dtype = object , name = 'foo' , index = pd .Index ([], name = 'bar' ))
@@ -66,11 +66,11 @@ def test_apply_dont_convert_dtype(self):
66
66
result = s .apply (f , convert_dtype = False )
67
67
assert result .dtype == object
68
68
69
- def test_with_string_args (self ):
69
+ def test_with_string_args (self , datetime_series ):
70
70
71
71
for arg in ['sum' , 'mean' , 'min' , 'max' , 'std' ]:
72
- result = self . ts .apply (arg )
73
- expected = getattr (self . ts , arg )()
72
+ result = datetime_series .apply (arg )
73
+ expected = getattr (datetime_series , arg )()
74
74
assert result == expected
75
75
76
76
def test_apply_args (self ):
@@ -165,45 +165,45 @@ def test_apply_dict_depr(self):
165
165
tsdf .A .agg ({'foo' : ['sum' , 'mean' ]})
166
166
167
167
168
- class TestSeriesAggregate (TestData ):
168
+ class TestSeriesAggregate ():
169
169
170
- def test_transform (self ):
170
+ def test_transform (self , string_series ):
171
171
# transforming functions
172
172
173
173
with np .errstate (all = 'ignore' ):
174
174
175
- f_sqrt = np .sqrt (self . series )
176
- f_abs = np .abs (self . series )
175
+ f_sqrt = np .sqrt (string_series )
176
+ f_abs = np .abs (string_series )
177
177
178
178
# ufunc
179
- result = self . series .transform (np .sqrt )
179
+ result = string_series .transform (np .sqrt )
180
180
expected = f_sqrt .copy ()
181
181
assert_series_equal (result , expected )
182
182
183
- result = self . series .apply (np .sqrt )
183
+ result = string_series .apply (np .sqrt )
184
184
assert_series_equal (result , expected )
185
185
186
186
# list-like
187
- result = self . series .transform ([np .sqrt ])
187
+ result = string_series .transform ([np .sqrt ])
188
188
expected = f_sqrt .to_frame ().copy ()
189
189
expected .columns = ['sqrt' ]
190
190
assert_frame_equal (result , expected )
191
191
192
- result = self . series .transform ([np .sqrt ])
192
+ result = string_series .transform ([np .sqrt ])
193
193
assert_frame_equal (result , expected )
194
194
195
- result = self . series .transform (['sqrt' ])
195
+ result = string_series .transform (['sqrt' ])
196
196
assert_frame_equal (result , expected )
197
197
198
198
# multiple items in list
199
199
# these are in the order as if we are applying both functions per
200
200
# series and then concatting
201
201
expected = pd .concat ([f_sqrt , f_abs ], axis = 1 )
202
202
expected .columns = ['sqrt' , 'absolute' ]
203
- result = self . series .apply ([np .sqrt , np .abs ])
203
+ result = string_series .apply ([np .sqrt , np .abs ])
204
204
assert_frame_equal (result , expected )
205
205
206
- result = self . series .transform (['sqrt' , 'abs' ])
206
+ result = string_series .transform (['sqrt' , 'abs' ])
207
207
expected .columns = ['sqrt' , 'abs' ]
208
208
assert_frame_equal (result , expected )
209
209
@@ -212,28 +212,28 @@ def test_transform(self):
212
212
expected .columns = ['foo' , 'bar' ]
213
213
expected = expected .unstack ().rename ('series' )
214
214
215
- result = self . series .apply ({'foo' : np .sqrt , 'bar' : np .abs })
215
+ result = string_series .apply ({'foo' : np .sqrt , 'bar' : np .abs })
216
216
assert_series_equal (result .reindex_like (expected ), expected )
217
217
218
- def test_transform_and_agg_error (self ):
218
+ def test_transform_and_agg_error (self , string_series ):
219
219
# we are trying to transform with an aggregator
220
220
def f ():
221
- self . series .transform (['min' , 'max' ])
221
+ string_series .transform (['min' , 'max' ])
222
222
pytest .raises (ValueError , f )
223
223
224
224
def f ():
225
225
with np .errstate (all = 'ignore' ):
226
- self . series .agg (['sqrt' , 'max' ])
226
+ string_series .agg (['sqrt' , 'max' ])
227
227
pytest .raises (ValueError , f )
228
228
229
229
def f ():
230
230
with np .errstate (all = 'ignore' ):
231
- self . series .transform (['sqrt' , 'max' ])
231
+ string_series .transform (['sqrt' , 'max' ])
232
232
pytest .raises (ValueError , f )
233
233
234
234
def f ():
235
235
with np .errstate (all = 'ignore' ):
236
- self . series .agg ({'foo' : np .sqrt , 'bar' : 'sum' })
236
+ string_series .agg ({'foo' : np .sqrt , 'bar' : 'sum' })
237
237
pytest .raises (ValueError , f )
238
238
239
239
def test_demo (self ):
@@ -272,33 +272,34 @@ def test_multiple_aggregators_with_dict_api(self):
272
272
'min' , 'sum' ]).unstack ().rename ('series' )
273
273
tm .assert_series_equal (result .reindex_like (expected ), expected )
274
274
275
- def test_agg_apply_evaluate_lambdas_the_same (self ):
275
+ def test_agg_apply_evaluate_lambdas_the_same (self , string_series ):
276
276
# test that we are evaluating row-by-row first
277
277
# before vectorized evaluation
278
- result = self . series .apply (lambda x : str (x ))
279
- expected = self . series .agg (lambda x : str (x ))
278
+ result = string_series .apply (lambda x : str (x ))
279
+ expected = string_series .agg (lambda x : str (x ))
280
280
tm .assert_series_equal (result , expected )
281
281
282
- result = self . series .apply (str )
283
- expected = self . series .agg (str )
282
+ result = string_series .apply (str )
283
+ expected = string_series .agg (str )
284
284
tm .assert_series_equal (result , expected )
285
285
286
- def test_with_nested_series (self ):
286
+ def test_with_nested_series (self , datetime_series ):
287
287
# GH 2316
288
288
# .agg with a reducer and a transform, what to do
289
- result = self . ts .apply (lambda x : Series (
289
+ result = datetime_series .apply (lambda x : Series (
290
290
[x , x ** 2 ], index = ['x' , 'x^2' ]))
291
- expected = DataFrame ({'x' : self .ts , 'x^2' : self .ts ** 2 })
291
+ expected = DataFrame ({'x' : datetime_series ,
292
+ 'x^2' : datetime_series ** 2 })
292
293
tm .assert_frame_equal (result , expected )
293
294
294
- result = self . ts .agg (lambda x : Series (
295
+ result = datetime_series .agg (lambda x : Series (
295
296
[x , x ** 2 ], index = ['x' , 'x^2' ]))
296
297
tm .assert_frame_equal (result , expected )
297
298
298
- def test_replicate_describe (self ):
299
+ def test_replicate_describe (self , string_series ):
299
300
# this also tests a result set that is all scalars
300
- expected = self . series .describe ()
301
- result = self . series .apply (OrderedDict (
301
+ expected = string_series .describe ()
302
+ result = string_series .apply (OrderedDict (
302
303
[('count' , 'count' ),
303
304
('mean' , 'mean' ),
304
305
('std' , 'std' ),
@@ -309,13 +310,13 @@ def test_replicate_describe(self):
309
310
('max' , 'max' )]))
310
311
assert_series_equal (result , expected )
311
312
312
- def test_reduce (self ):
313
+ def test_reduce (self , string_series ):
313
314
# reductions with named functions
314
- result = self . series .agg (['sum' , 'mean' ])
315
- expected = Series ([self . series .sum (),
316
- self . series .mean ()],
315
+ result = string_series .agg (['sum' , 'mean' ])
316
+ expected = Series ([string_series .sum (),
317
+ string_series .mean ()],
317
318
['sum' , 'mean' ],
318
- name = self . series .name )
319
+ name = string_series .name )
319
320
assert_series_equal (result , expected )
320
321
321
322
def test_non_callable_aggregates (self ):
@@ -414,9 +415,9 @@ def test_agg_cython_table_raises(self, series, func, expected):
414
415
series .agg (func )
415
416
416
417
417
- class TestSeriesMap (TestData ):
418
+ class TestSeriesMap ():
418
419
419
- def test_map (self ):
420
+ def test_map (self , datetime_series ):
420
421
index , data = tm .getMixedTypeDict ()
421
422
422
423
source = Series (data ['B' ], index = data ['C' ])
@@ -434,8 +435,8 @@ def test_map(self):
434
435
assert v == source [target [k ]]
435
436
436
437
# function
437
- result = self . ts .map (lambda x : x * 2 )
438
- tm .assert_series_equal (result , self . ts * 2 )
438
+ result = datetime_series .map (lambda x : x * 2 )
439
+ tm .assert_series_equal (result , datetime_series * 2 )
439
440
440
441
# GH 10324
441
442
a = Series ([1 , 2 , 3 , 4 ])
@@ -500,10 +501,10 @@ def test_map_type_inference(self):
500
501
s2 = s .map (lambda x : np .where (x == 0 , 0 , 1 ))
501
502
assert issubclass (s2 .dtype .type , np .integer )
502
503
503
- def test_map_decimal (self ):
504
+ def test_map_decimal (self , string_series ):
504
505
from decimal import Decimal
505
506
506
- result = self . series .map (lambda x : Decimal (str (x )))
507
+ result = string_series .map (lambda x : Decimal (str (x )))
507
508
assert result .dtype == np .object_
508
509
assert isinstance (result [0 ], Decimal )
509
510
0 commit comments