Skip to content

Commit 27de8e6

Browse files
Anjali2019jreback
authored andcommitted
TST: Fixturize series/test_apply.py (#22769)
1 parent e5a99c6 commit 27de8e6

File tree

1 file changed

+50
-49
lines changed

1 file changed

+50
-49
lines changed

pandas/tests/series/test_apply.py

+50-49
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
import pandas.util.testing as tm
1818
from pandas.conftest import _get_cython_table_params
1919

20-
from .common import TestData
2120

21+
class TestSeriesApply():
2222

23-
class TestSeriesApply(TestData):
24-
25-
def test_apply(self):
23+
def test_apply(self, datetime_series):
2624
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))
2827

2928
# element-wise apply
3029
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))
3232

3333
# empty series
3434
s = Series(dtype=object, name='foo', index=pd.Index([], name='bar'))
@@ -66,11 +66,11 @@ def test_apply_dont_convert_dtype(self):
6666
result = s.apply(f, convert_dtype=False)
6767
assert result.dtype == object
6868

69-
def test_with_string_args(self):
69+
def test_with_string_args(self, datetime_series):
7070

7171
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)()
7474
assert result == expected
7575

7676
def test_apply_args(self):
@@ -165,45 +165,45 @@ def test_apply_dict_depr(self):
165165
tsdf.A.agg({'foo': ['sum', 'mean']})
166166

167167

168-
class TestSeriesAggregate(TestData):
168+
class TestSeriesAggregate():
169169

170-
def test_transform(self):
170+
def test_transform(self, string_series):
171171
# transforming functions
172172

173173
with np.errstate(all='ignore'):
174174

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)
177177

178178
# ufunc
179-
result = self.series.transform(np.sqrt)
179+
result = string_series.transform(np.sqrt)
180180
expected = f_sqrt.copy()
181181
assert_series_equal(result, expected)
182182

183-
result = self.series.apply(np.sqrt)
183+
result = string_series.apply(np.sqrt)
184184
assert_series_equal(result, expected)
185185

186186
# list-like
187-
result = self.series.transform([np.sqrt])
187+
result = string_series.transform([np.sqrt])
188188
expected = f_sqrt.to_frame().copy()
189189
expected.columns = ['sqrt']
190190
assert_frame_equal(result, expected)
191191

192-
result = self.series.transform([np.sqrt])
192+
result = string_series.transform([np.sqrt])
193193
assert_frame_equal(result, expected)
194194

195-
result = self.series.transform(['sqrt'])
195+
result = string_series.transform(['sqrt'])
196196
assert_frame_equal(result, expected)
197197

198198
# multiple items in list
199199
# these are in the order as if we are applying both functions per
200200
# series and then concatting
201201
expected = pd.concat([f_sqrt, f_abs], axis=1)
202202
expected.columns = ['sqrt', 'absolute']
203-
result = self.series.apply([np.sqrt, np.abs])
203+
result = string_series.apply([np.sqrt, np.abs])
204204
assert_frame_equal(result, expected)
205205

206-
result = self.series.transform(['sqrt', 'abs'])
206+
result = string_series.transform(['sqrt', 'abs'])
207207
expected.columns = ['sqrt', 'abs']
208208
assert_frame_equal(result, expected)
209209

@@ -212,28 +212,28 @@ def test_transform(self):
212212
expected.columns = ['foo', 'bar']
213213
expected = expected.unstack().rename('series')
214214

215-
result = self.series.apply({'foo': np.sqrt, 'bar': np.abs})
215+
result = string_series.apply({'foo': np.sqrt, 'bar': np.abs})
216216
assert_series_equal(result.reindex_like(expected), expected)
217217

218-
def test_transform_and_agg_error(self):
218+
def test_transform_and_agg_error(self, string_series):
219219
# we are trying to transform with an aggregator
220220
def f():
221-
self.series.transform(['min', 'max'])
221+
string_series.transform(['min', 'max'])
222222
pytest.raises(ValueError, f)
223223

224224
def f():
225225
with np.errstate(all='ignore'):
226-
self.series.agg(['sqrt', 'max'])
226+
string_series.agg(['sqrt', 'max'])
227227
pytest.raises(ValueError, f)
228228

229229
def f():
230230
with np.errstate(all='ignore'):
231-
self.series.transform(['sqrt', 'max'])
231+
string_series.transform(['sqrt', 'max'])
232232
pytest.raises(ValueError, f)
233233

234234
def f():
235235
with np.errstate(all='ignore'):
236-
self.series.agg({'foo': np.sqrt, 'bar': 'sum'})
236+
string_series.agg({'foo': np.sqrt, 'bar': 'sum'})
237237
pytest.raises(ValueError, f)
238238

239239
def test_demo(self):
@@ -272,33 +272,34 @@ def test_multiple_aggregators_with_dict_api(self):
272272
'min', 'sum']).unstack().rename('series')
273273
tm.assert_series_equal(result.reindex_like(expected), expected)
274274

275-
def test_agg_apply_evaluate_lambdas_the_same(self):
275+
def test_agg_apply_evaluate_lambdas_the_same(self, string_series):
276276
# test that we are evaluating row-by-row first
277277
# 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))
280280
tm.assert_series_equal(result, expected)
281281

282-
result = self.series.apply(str)
283-
expected = self.series.agg(str)
282+
result = string_series.apply(str)
283+
expected = string_series.agg(str)
284284
tm.assert_series_equal(result, expected)
285285

286-
def test_with_nested_series(self):
286+
def test_with_nested_series(self, datetime_series):
287287
# GH 2316
288288
# .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(
290290
[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})
292293
tm.assert_frame_equal(result, expected)
293294

294-
result = self.ts.agg(lambda x: Series(
295+
result = datetime_series.agg(lambda x: Series(
295296
[x, x ** 2], index=['x', 'x^2']))
296297
tm.assert_frame_equal(result, expected)
297298

298-
def test_replicate_describe(self):
299+
def test_replicate_describe(self, string_series):
299300
# 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(
302303
[('count', 'count'),
303304
('mean', 'mean'),
304305
('std', 'std'),
@@ -309,13 +310,13 @@ def test_replicate_describe(self):
309310
('max', 'max')]))
310311
assert_series_equal(result, expected)
311312

312-
def test_reduce(self):
313+
def test_reduce(self, string_series):
313314
# 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()],
317318
['sum', 'mean'],
318-
name=self.series.name)
319+
name=string_series.name)
319320
assert_series_equal(result, expected)
320321

321322
def test_non_callable_aggregates(self):
@@ -414,9 +415,9 @@ def test_agg_cython_table_raises(self, series, func, expected):
414415
series.agg(func)
415416

416417

417-
class TestSeriesMap(TestData):
418+
class TestSeriesMap():
418419

419-
def test_map(self):
420+
def test_map(self, datetime_series):
420421
index, data = tm.getMixedTypeDict()
421422

422423
source = Series(data['B'], index=data['C'])
@@ -434,8 +435,8 @@ def test_map(self):
434435
assert v == source[target[k]]
435436

436437
# 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)
439440

440441
# GH 10324
441442
a = Series([1, 2, 3, 4])
@@ -500,10 +501,10 @@ def test_map_type_inference(self):
500501
s2 = s.map(lambda x: np.where(x == 0, 0, 1))
501502
assert issubclass(s2.dtype.type, np.integer)
502503

503-
def test_map_decimal(self):
504+
def test_map_decimal(self, string_series):
504505
from decimal import Decimal
505506

506-
result = self.series.map(lambda x: Decimal(str(x)))
507+
result = string_series.map(lambda x: Decimal(str(x)))
507508
assert result.dtype == np.object_
508509
assert isinstance(result[0], Decimal)
509510

0 commit comments

Comments
 (0)