Skip to content

Commit

Permalink
TST: Use more pytest fixtures (pandas-dev#53750)
Browse files Browse the repository at this point in the history
* CI: Build pandas even if doctests fail

* BUG: groupby sum turning `inf+inf` and `(-inf)+(-inf)` into `nan` (pandas-dev#53623)

* DEPR: method, limit in NDFrame.replace (pandas-dev#53492)

* DEPR: method, limit in NDFrame.replace

* update test, docs

* suppress doctest warning

* doctests

* PERF: Series.str.get_dummies for ArrowDtype(pa.string()) (pandas-dev#53655)

* PERF: Series.str.get_dummies for ArrowDtype(pa.string())

* whatsnew

* typing

* TYP: core.missing (pandas-dev#53625)

* CI: Attempt to fix wheel builds (pandas-dev#53670)

* DOC: Fixing EX01 - Added examples (pandas-dev#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 (pandas-dev#53677)

* BUG/CoW: is_range_indexer can't handle very large arrays (pandas-dev#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]>
  • Loading branch information
6 people authored and root committed Jun 23, 2023
1 parent 6ede1d8 commit 5710d1a
Show file tree
Hide file tree
Showing 13 changed files with 280 additions and 267 deletions.
53 changes: 29 additions & 24 deletions pandas/tests/resample/test_resample_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,43 @@
import pandas._testing as tm
from pandas.core.indexes.datetimes import date_range

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

test_series = Series(np.random.rand(len(dti)), dti)
_test_frame = DataFrame({"A": test_series, "B": test_series, "C": np.arange(len(dti))})
@pytest.fixture
def dti():
return date_range(start=datetime(2005, 1, 1), end=datetime(2005, 1, 10), freq="Min")


@pytest.fixture
def _test_series(dti):
return Series(np.random.rand(len(dti)), dti)


@pytest.fixture
def test_frame():
return _test_frame.copy()
def test_frame(dti, _test_series):
return DataFrame({"A": _test_series, "B": _test_series, "C": np.arange(len(dti))})


def test_str():
r = test_series.resample("H")
def test_str(_test_series):
r = _test_series.resample("H")
assert (
"DatetimeIndexResampler [freq=<Hour>, axis=0, closed=left, "
"label=left, convention=start, origin=start_day]" in str(r)
)

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


def test_api():
r = test_series.resample("H")
def test_api(_test_series):
r = _test_series.resample("H")
result = r.mean()
assert isinstance(result, Series)
assert len(result) == 217

r = test_series.to_frame().resample("H")
r = _test_series.to_frame().resample("H")
result = r.mean()
assert isinstance(result, DataFrame)
assert len(result) == 217
Expand Down Expand Up @@ -116,11 +121,11 @@ def test_resample_group_keys():
tm.assert_frame_equal(result, expected)


def test_pipe(test_frame):
def test_pipe(test_frame, _test_series):
# GH17905

# series
r = test_series.resample("H")
r = _test_series.resample("H")
expected = r.max() - r.mean()
result = r.pipe(lambda x: x.max() - x.mean())
tm.assert_series_equal(result, expected)
Expand Down Expand Up @@ -261,9 +266,9 @@ def test_combined_up_downsampling_of_irregular():
tm.assert_series_equal(result, expected)


def test_transform_series():
r = test_series.resample("20min")
expected = test_series.groupby(pd.Grouper(freq="20min")).transform("mean")
def test_transform_series(_test_series):
r = _test_series.resample("20min")
expected = _test_series.groupby(pd.Grouper(freq="20min")).transform("mean")
result = r.transform("mean")
tm.assert_series_equal(result, expected)

Expand Down Expand Up @@ -319,17 +324,17 @@ def test_fillna():
],
ids=["resample", "groupby"],
)
def test_apply_without_aggregation(func):
def test_apply_without_aggregation(func, _test_series):
# both resample and groupby should work w/o aggregation
t = func(test_series)
t = func(_test_series)
result = t.apply(lambda x: x)
tm.assert_series_equal(result, test_series)
tm.assert_series_equal(result, _test_series)


def test_apply_without_aggregation2():
grouped = test_series.to_frame(name="foo").resample("20min", group_keys=False)
def test_apply_without_aggregation2(_test_series):
grouped = _test_series.to_frame(name="foo").resample("20min", group_keys=False)
result = grouped["foo"].apply(lambda x: x)
tm.assert_series_equal(result, test_series.rename("foo"))
tm.assert_series_equal(result, _test_series.rename("foo"))


def test_agg_consistency():
Expand Down Expand Up @@ -1010,13 +1015,13 @@ def test_df_axis_param_depr():
df.resample("M", axis=0)


def test_series_axis_param_depr():
def test_series_axis_param_depr(_test_series):
warning_msg = (
"The 'axis' keyword in Series.resample is "
"deprecated and will be removed in a future version."
)
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
test_series.resample("H", axis=0)
_test_series.resample("H", axis=0)


def test_resample_empty():
Expand Down
23 changes: 13 additions & 10 deletions pandas/tests/resample/test_resampler_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
import pandas._testing as tm
from pandas.core.indexes.datetimes import date_range

test_frame = DataFrame(
{"A": [1] * 20 + [2] * 12 + [3] * 8, "B": np.arange(40)},
index=date_range("1/1/2000", freq="s", periods=40),
)

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


@async_mark()
Expand Down Expand Up @@ -85,7 +88,7 @@ def f_1(x):
tm.assert_frame_equal(result, expected)


def test_getitem():
def test_getitem(test_frame):
g = test_frame.groupby("A")

expected = g.B.apply(lambda x: x.resample("2s").mean())
Expand Down Expand Up @@ -217,7 +220,7 @@ def test_nearest():
"ohlc",
],
)
def test_methods(f):
def test_methods(f, test_frame):
g = test_frame.groupby("A")
r = g.resample("2s")

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


def test_methods_nunique():
def test_methods_nunique(test_frame):
# series only
g = test_frame.groupby("A")
r = g.resample("2s")
Expand All @@ -236,15 +239,15 @@ def test_methods_nunique():


@pytest.mark.parametrize("f", ["std", "var"])
def test_methods_std_var(f):
def test_methods_std_var(f, test_frame):
g = test_frame.groupby("A")
r = g.resample("2s")
result = getattr(r, f)(ddof=1)
expected = g.apply(lambda x: getattr(x.resample("2s"), f)(ddof=1))
tm.assert_frame_equal(result, expected)


def test_apply():
def test_apply(test_frame):
g = test_frame.groupby("A")
r = g.resample("2s")

Expand Down Expand Up @@ -342,7 +345,7 @@ def test_resample_groupby_with_label():
tm.assert_frame_equal(result, expected)


def test_consistency_with_window():
def test_consistency_with_window(test_frame):
# consistent return values with window
df = test_frame
expected = Index([1, 2, 3], name="A")
Expand Down
11 changes: 7 additions & 4 deletions pandas/tests/resample/test_time_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
from pandas.core.groupby.grouper import Grouper
from pandas.core.indexes.datetimes import date_range

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

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

def test_apply():

def test_apply(test_series):
grouper = Grouper(freq="A", label="right", closed="right")

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


def test_count():
def test_count(test_series):
test_series[::3] = np.nan

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


def test_numpy_reduction():
def test_numpy_reduction(test_series):
result = test_series.resample("A", closed="right").prod()

expected = test_series.groupby(lambda x: x.year).agg(np.prod)
Expand Down
78 changes: 40 additions & 38 deletions pandas/tests/reshape/concat/test_append_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,53 @@
)
import pandas._testing as tm

dt_data = [
pd.Timestamp("2011-01-01"),
pd.Timestamp("2011-01-02"),
pd.Timestamp("2011-01-03"),
]
tz_data = [
pd.Timestamp("2011-01-01", tz="US/Eastern"),
pd.Timestamp("2011-01-02", tz="US/Eastern"),
pd.Timestamp("2011-01-03", tz="US/Eastern"),
]
td_data = [
pd.Timedelta("1 days"),
pd.Timedelta("2 days"),
pd.Timedelta("3 days"),
]
period_data = [
pd.Period("2011-01", freq="M"),
pd.Period("2011-02", freq="M"),
pd.Period("2011-03", freq="M"),
]
data_dict = {
"bool": [True, False, True],
"int64": [1, 2, 3],
"float64": [1.1, np.nan, 3.3],
"category": Categorical(["X", "Y", "Z"]),
"object": ["a", "b", "c"],
"datetime64[ns]": dt_data,
"datetime64[ns, US/Eastern]": tz_data,
"timedelta64[ns]": td_data,
"period[M]": period_data,
}

@pytest.fixture(
params=list(
{
"bool": [True, False, True],
"int64": [1, 2, 3],
"float64": [1.1, np.nan, 3.3],
"category": Categorical(["X", "Y", "Z"]),
"object": ["a", "b", "c"],
"datetime64[ns]": [
pd.Timestamp("2011-01-01"),
pd.Timestamp("2011-01-02"),
pd.Timestamp("2011-01-03"),
],
"datetime64[ns, US/Eastern]": [
pd.Timestamp("2011-01-01", tz="US/Eastern"),
pd.Timestamp("2011-01-02", tz="US/Eastern"),
pd.Timestamp("2011-01-03", tz="US/Eastern"),
],
"timedelta64[ns]": [
pd.Timedelta("1 days"),
pd.Timedelta("2 days"),
pd.Timedelta("3 days"),
],
"period[M]": [
pd.Period("2011-01", freq="M"),
pd.Period("2011-02", freq="M"),
pd.Period("2011-03", freq="M"),
],
}.items()
)
)
def item(request):
key, data = request.param
return key, data


@pytest.fixture
def item2(item):
return item


class TestConcatAppendCommon:
"""
Test common dtype coercion rules between concat and append.
"""

@pytest.fixture(params=sorted(data_dict.keys()))
def item(self, request):
key = request.param
return key, data_dict[key]

item2 = item

def test_dtypes(self, item, index_or_series):
# to confirm test case covers intended dtypes
typ, vals = item
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/series/methods/test_argsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@


class TestSeriesArgsort:
def _check_accum_op(self, name, ser, check_dtype=True):
func = getattr(np, name)
def test_argsort_numpy(self, datetime_series):
ser = datetime_series
func = np.argsort
tm.assert_numpy_array_equal(
func(ser).values, func(np.array(ser)), check_dtype=check_dtype
func(ser).values, func(np.array(ser)), check_dtype=False
)

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

def test_argsort(self, datetime_series):
self._check_accum_op("argsort", datetime_series, check_dtype=False)
argsorted = datetime_series.argsort()
assert issubclass(argsorted.dtype.type, np.integer)

Expand Down
Loading

0 comments on commit 5710d1a

Please sign in to comment.