Skip to content

Commit 6f51d03

Browse files
mroeschkeCharlie-XIAOjbrockmendellukemanleylithomas1
authored
TST: Use more pytest fixtures (#53750)
* CI: Build pandas even if doctests fail * BUG: groupby sum turning `inf+inf` and `(-inf)+(-inf)` into `nan` (#53623) * DEPR: method, limit in NDFrame.replace (#53492) * DEPR: method, limit in NDFrame.replace * update test, docs * suppress doctest warning * doctests * PERF: Series.str.get_dummies for ArrowDtype(pa.string()) (#53655) * PERF: Series.str.get_dummies for ArrowDtype(pa.string()) * whatsnew * typing * TYP: core.missing (#53625) * CI: Attempt to fix wheel builds (#53670) * DOC: Fixing EX01 - Added examples (#53647) * SeriesGroupBy.fillna example added * Added examples * Corrected failing test for timedelta.total_seconds * Corrected fillna example * CI/TST: Mark test_to_read_gcs as single_cpu (#53677) * BUG/CoW: is_range_indexer can't handle very large arrays (#53672) * BUG: is_range_indexer can't handle very large arrays * fix test on 32-bit * TST: Use more pytest fixtures --------- Co-authored-by: Yao Xiao <[email protected]> Co-authored-by: jbrockmendel <[email protected]> Co-authored-by: Luke Manley <[email protected]> Co-authored-by: Thomas Li <[email protected]> Co-authored-by: Dea María Léon <[email protected]>
1 parent 78fac35 commit 6f51d03

13 files changed

+280
-267
lines changed

pandas/tests/resample/test_resample_api.py

+29-24
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,43 @@
1616
import pandas._testing as tm
1717
from pandas.core.indexes.datetimes import date_range
1818

19-
dti = date_range(start=datetime(2005, 1, 1), end=datetime(2005, 1, 10), freq="Min")
2019

21-
test_series = Series(np.random.rand(len(dti)), dti)
22-
_test_frame = DataFrame({"A": test_series, "B": test_series, "C": np.arange(len(dti))})
20+
@pytest.fixture
21+
def dti():
22+
return date_range(start=datetime(2005, 1, 1), end=datetime(2005, 1, 10), freq="Min")
23+
24+
25+
@pytest.fixture
26+
def _test_series(dti):
27+
return Series(np.random.rand(len(dti)), dti)
2328

2429

2530
@pytest.fixture
26-
def test_frame():
27-
return _test_frame.copy()
31+
def test_frame(dti, _test_series):
32+
return DataFrame({"A": _test_series, "B": _test_series, "C": np.arange(len(dti))})
2833

2934

30-
def test_str():
31-
r = test_series.resample("H")
35+
def test_str(_test_series):
36+
r = _test_series.resample("H")
3237
assert (
3338
"DatetimeIndexResampler [freq=<Hour>, axis=0, closed=left, "
3439
"label=left, convention=start, origin=start_day]" in str(r)
3540
)
3641

37-
r = test_series.resample("H", origin="2000-01-01")
42+
r = _test_series.resample("H", origin="2000-01-01")
3843
assert (
3944
"DatetimeIndexResampler [freq=<Hour>, axis=0, closed=left, "
4045
"label=left, convention=start, origin=2000-01-01 00:00:00]" in str(r)
4146
)
4247

4348

44-
def test_api():
45-
r = test_series.resample("H")
49+
def test_api(_test_series):
50+
r = _test_series.resample("H")
4651
result = r.mean()
4752
assert isinstance(result, Series)
4853
assert len(result) == 217
4954

50-
r = test_series.to_frame().resample("H")
55+
r = _test_series.to_frame().resample("H")
5156
result = r.mean()
5257
assert isinstance(result, DataFrame)
5358
assert len(result) == 217
@@ -116,11 +121,11 @@ def test_resample_group_keys():
116121
tm.assert_frame_equal(result, expected)
117122

118123

119-
def test_pipe(test_frame):
124+
def test_pipe(test_frame, _test_series):
120125
# GH17905
121126

122127
# series
123-
r = test_series.resample("H")
128+
r = _test_series.resample("H")
124129
expected = r.max() - r.mean()
125130
result = r.pipe(lambda x: x.max() - x.mean())
126131
tm.assert_series_equal(result, expected)
@@ -261,9 +266,9 @@ def test_combined_up_downsampling_of_irregular():
261266
tm.assert_series_equal(result, expected)
262267

263268

264-
def test_transform_series():
265-
r = test_series.resample("20min")
266-
expected = test_series.groupby(pd.Grouper(freq="20min")).transform("mean")
269+
def test_transform_series(_test_series):
270+
r = _test_series.resample("20min")
271+
expected = _test_series.groupby(pd.Grouper(freq="20min")).transform("mean")
267272
result = r.transform("mean")
268273
tm.assert_series_equal(result, expected)
269274

@@ -319,17 +324,17 @@ def test_fillna():
319324
],
320325
ids=["resample", "groupby"],
321326
)
322-
def test_apply_without_aggregation(func):
327+
def test_apply_without_aggregation(func, _test_series):
323328
# both resample and groupby should work w/o aggregation
324-
t = func(test_series)
329+
t = func(_test_series)
325330
result = t.apply(lambda x: x)
326-
tm.assert_series_equal(result, test_series)
331+
tm.assert_series_equal(result, _test_series)
327332

328333

329-
def test_apply_without_aggregation2():
330-
grouped = test_series.to_frame(name="foo").resample("20min", group_keys=False)
334+
def test_apply_without_aggregation2(_test_series):
335+
grouped = _test_series.to_frame(name="foo").resample("20min", group_keys=False)
331336
result = grouped["foo"].apply(lambda x: x)
332-
tm.assert_series_equal(result, test_series.rename("foo"))
337+
tm.assert_series_equal(result, _test_series.rename("foo"))
333338

334339

335340
def test_agg_consistency():
@@ -1010,13 +1015,13 @@ def test_df_axis_param_depr():
10101015
df.resample("M", axis=0)
10111016

10121017

1013-
def test_series_axis_param_depr():
1018+
def test_series_axis_param_depr(_test_series):
10141019
warning_msg = (
10151020
"The 'axis' keyword in Series.resample is "
10161021
"deprecated and will be removed in a future version."
10171022
)
10181023
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
1019-
test_series.resample("H", axis=0)
1024+
_test_series.resample("H", axis=0)
10201025

10211026

10221027
def test_resample_empty():

pandas/tests/resample/test_resampler_grouper.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
import pandas._testing as tm
1818
from pandas.core.indexes.datetimes import date_range
1919

20-
test_frame = DataFrame(
21-
{"A": [1] * 20 + [2] * 12 + [3] * 8, "B": np.arange(40)},
22-
index=date_range("1/1/2000", freq="s", periods=40),
23-
)
20+
21+
@pytest.fixture
22+
def test_frame():
23+
return DataFrame(
24+
{"A": [1] * 20 + [2] * 12 + [3] * 8, "B": np.arange(40)},
25+
index=date_range("1/1/2000", freq="s", periods=40),
26+
)
2427

2528

2629
@async_mark()
@@ -85,7 +88,7 @@ def f_1(x):
8588
tm.assert_frame_equal(result, expected)
8689

8790

88-
def test_getitem():
91+
def test_getitem(test_frame):
8992
g = test_frame.groupby("A")
9093

9194
expected = g.B.apply(lambda x: x.resample("2s").mean())
@@ -217,7 +220,7 @@ def test_nearest():
217220
"ohlc",
218221
],
219222
)
220-
def test_methods(f):
223+
def test_methods(f, test_frame):
221224
g = test_frame.groupby("A")
222225
r = g.resample("2s")
223226

@@ -226,7 +229,7 @@ def test_methods(f):
226229
tm.assert_equal(result, expected)
227230

228231

229-
def test_methods_nunique():
232+
def test_methods_nunique(test_frame):
230233
# series only
231234
g = test_frame.groupby("A")
232235
r = g.resample("2s")
@@ -236,15 +239,15 @@ def test_methods_nunique():
236239

237240

238241
@pytest.mark.parametrize("f", ["std", "var"])
239-
def test_methods_std_var(f):
242+
def test_methods_std_var(f, test_frame):
240243
g = test_frame.groupby("A")
241244
r = g.resample("2s")
242245
result = getattr(r, f)(ddof=1)
243246
expected = g.apply(lambda x: getattr(x.resample("2s"), f)(ddof=1))
244247
tm.assert_frame_equal(result, expected)
245248

246249

247-
def test_apply():
250+
def test_apply(test_frame):
248251
g = test_frame.groupby("A")
249252
r = g.resample("2s")
250253

@@ -342,7 +345,7 @@ def test_resample_groupby_with_label():
342345
tm.assert_frame_equal(result, expected)
343346

344347

345-
def test_consistency_with_window():
348+
def test_consistency_with_window(test_frame):
346349
# consistent return values with window
347350
df = test_frame
348351
expected = Index([1, 2, 3], name="A")

pandas/tests/resample/test_time_grouper.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
from pandas.core.groupby.grouper import Grouper
1515
from pandas.core.indexes.datetimes import date_range
1616

17-
test_series = Series(np.random.randn(1000), index=date_range("1/1/2000", periods=1000))
1817

18+
@pytest.fixture
19+
def test_series():
20+
return Series(np.random.randn(1000), index=date_range("1/1/2000", periods=1000))
1921

20-
def test_apply():
22+
23+
def test_apply(test_series):
2124
grouper = Grouper(freq="A", label="right", closed="right")
2225

2326
grouped = test_series.groupby(grouper)
@@ -33,7 +36,7 @@ def f(x):
3336
tm.assert_series_equal(applied, expected)
3437

3538

36-
def test_count():
39+
def test_count(test_series):
3740
test_series[::3] = np.nan
3841

3942
expected = test_series.groupby(lambda x: x.year).count()
@@ -48,7 +51,7 @@ def test_count():
4851
tm.assert_series_equal(result, expected)
4952

5053

51-
def test_numpy_reduction():
54+
def test_numpy_reduction(test_series):
5255
result = test_series.resample("A", closed="right").prod()
5356

5457
expected = test_series.groupby(lambda x: x.year).agg(np.prod)

pandas/tests/reshape/concat/test_append_common.py

+40-38
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,53 @@
1010
)
1111
import pandas._testing as tm
1212

13-
dt_data = [
14-
pd.Timestamp("2011-01-01"),
15-
pd.Timestamp("2011-01-02"),
16-
pd.Timestamp("2011-01-03"),
17-
]
18-
tz_data = [
19-
pd.Timestamp("2011-01-01", tz="US/Eastern"),
20-
pd.Timestamp("2011-01-02", tz="US/Eastern"),
21-
pd.Timestamp("2011-01-03", tz="US/Eastern"),
22-
]
23-
td_data = [
24-
pd.Timedelta("1 days"),
25-
pd.Timedelta("2 days"),
26-
pd.Timedelta("3 days"),
27-
]
28-
period_data = [
29-
pd.Period("2011-01", freq="M"),
30-
pd.Period("2011-02", freq="M"),
31-
pd.Period("2011-03", freq="M"),
32-
]
33-
data_dict = {
34-
"bool": [True, False, True],
35-
"int64": [1, 2, 3],
36-
"float64": [1.1, np.nan, 3.3],
37-
"category": Categorical(["X", "Y", "Z"]),
38-
"object": ["a", "b", "c"],
39-
"datetime64[ns]": dt_data,
40-
"datetime64[ns, US/Eastern]": tz_data,
41-
"timedelta64[ns]": td_data,
42-
"period[M]": period_data,
43-
}
13+
14+
@pytest.fixture(
15+
params=list(
16+
{
17+
"bool": [True, False, True],
18+
"int64": [1, 2, 3],
19+
"float64": [1.1, np.nan, 3.3],
20+
"category": Categorical(["X", "Y", "Z"]),
21+
"object": ["a", "b", "c"],
22+
"datetime64[ns]": [
23+
pd.Timestamp("2011-01-01"),
24+
pd.Timestamp("2011-01-02"),
25+
pd.Timestamp("2011-01-03"),
26+
],
27+
"datetime64[ns, US/Eastern]": [
28+
pd.Timestamp("2011-01-01", tz="US/Eastern"),
29+
pd.Timestamp("2011-01-02", tz="US/Eastern"),
30+
pd.Timestamp("2011-01-03", tz="US/Eastern"),
31+
],
32+
"timedelta64[ns]": [
33+
pd.Timedelta("1 days"),
34+
pd.Timedelta("2 days"),
35+
pd.Timedelta("3 days"),
36+
],
37+
"period[M]": [
38+
pd.Period("2011-01", freq="M"),
39+
pd.Period("2011-02", freq="M"),
40+
pd.Period("2011-03", freq="M"),
41+
],
42+
}.items()
43+
)
44+
)
45+
def item(request):
46+
key, data = request.param
47+
return key, data
48+
49+
50+
@pytest.fixture
51+
def item2(item):
52+
return item
4453

4554

4655
class TestConcatAppendCommon:
4756
"""
4857
Test common dtype coercion rules between concat and append.
4958
"""
5059

51-
@pytest.fixture(params=sorted(data_dict.keys()))
52-
def item(self, request):
53-
key = request.param
54-
return key, data_dict[key]
55-
56-
item2 = item
57-
5860
def test_dtypes(self, item, index_or_series):
5961
# to confirm test case covers intended dtypes
6062
typ, vals = item

pandas/tests/series/methods/test_argsort.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010

1111

1212
class TestSeriesArgsort:
13-
def _check_accum_op(self, name, ser, check_dtype=True):
14-
func = getattr(np, name)
13+
def test_argsort_numpy(self, datetime_series):
14+
ser = datetime_series
15+
func = np.argsort
1516
tm.assert_numpy_array_equal(
16-
func(ser).values, func(np.array(ser)), check_dtype=check_dtype
17+
func(ser).values, func(np.array(ser)), check_dtype=False
1718
)
1819

1920
# with missing values
@@ -26,7 +27,6 @@ def _check_accum_op(self, name, ser, check_dtype=True):
2627
tm.assert_numpy_array_equal(result.values, expected, check_dtype=False)
2728

2829
def test_argsort(self, datetime_series):
29-
self._check_accum_op("argsort", datetime_series, check_dtype=False)
3030
argsorted = datetime_series.argsort()
3131
assert issubclass(argsorted.dtype.type, np.integer)
3232

0 commit comments

Comments
 (0)