Skip to content

REF: collect reduction tests #36901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 0 additions & 77 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@


class TestSeriesAnalytics:
def test_prod_numpy16_bug(self):
s = Series([1.0, 1.0, 1.0], index=range(3))
result = s.prod()

assert not isinstance(result, Series)

def test_matmul(self):
# matmul test is for GH #10259
a = Series(np.random.randn(4), index=["p", "q", "r", "s"])
Expand Down Expand Up @@ -125,74 +119,3 @@ def test_is_monotonic(self):
s = Series(list(reversed(s.tolist())))
assert s.is_monotonic is False
assert s.is_monotonic_decreasing is True

@pytest.mark.parametrize("func", [np.any, np.all])
@pytest.mark.parametrize("kwargs", [dict(keepdims=True), dict(out=object())])
def test_validate_any_all_out_keepdims_raises(self, kwargs, func):
s = pd.Series([1, 2])
param = list(kwargs)[0]
name = func.__name__

msg = (
f"the '{param}' parameter is not "
"supported in the pandas "
fr"implementation of {name}\(\)"
)
with pytest.raises(ValueError, match=msg):
func(s, **kwargs)

def test_validate_sum_initial(self):
s = pd.Series([1, 2])
msg = (
r"the 'initial' parameter is not "
r"supported in the pandas "
r"implementation of sum\(\)"
)
with pytest.raises(ValueError, match=msg):
np.sum(s, initial=10)

def test_validate_median_initial(self):
s = pd.Series([1, 2])
msg = (
r"the 'overwrite_input' parameter is not "
r"supported in the pandas "
r"implementation of median\(\)"
)
with pytest.raises(ValueError, match=msg):
# It seems like np.median doesn't dispatch, so we use the
# method instead of the ufunc.
s.median(overwrite_input=True)

def test_validate_stat_keepdims(self):
s = pd.Series([1, 2])
msg = (
r"the 'keepdims' parameter is not "
r"supported in the pandas "
r"implementation of sum\(\)"
)
with pytest.raises(ValueError, match=msg):
np.sum(s, keepdims=True)

def test_td64_summation_overflow(self):
# GH 9442
s = pd.Series(pd.date_range("20130101", periods=100000, freq="H"))
s[0] += pd.Timedelta("1s 1ms")

# mean
result = (s - s.min()).mean()
expected = pd.Timedelta((pd.TimedeltaIndex(s - s.min()).asi8 / len(s)).sum())

# the computation is converted to float so
# might be some loss of precision
assert np.allclose(result.value / 1000, expected.value / 1000)

# sum
msg = "overflow in timedelta operation"
with pytest.raises(ValueError, match=msg):
(s - s.min()).sum()

s1 = s[0:10000]
with pytest.raises(ValueError, match=msg):
(s1 - s1.min()).sum()
s2 = s[0:1000]
(s2 - s2.min()).sum()
86 changes: 86 additions & 0 deletions pandas/tests/series/test_reductions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import numpy as np
import pytest

import pandas as pd
from pandas import Series

Expand All @@ -9,3 +12,86 @@ def test_reductions_td64_with_nat():
assert ser.median() == exp
assert ser.min() == exp
assert ser.max() == exp


def test_td64_summation_overflow():
# GH#9442
ser = Series(pd.date_range("20130101", periods=100000, freq="H"))
ser[0] += pd.Timedelta("1s 1ms")

# mean
result = (ser - ser.min()).mean()
expected = pd.Timedelta((pd.TimedeltaIndex(ser - ser.min()).asi8 / len(ser)).sum())

# the computation is converted to float so
# might be some loss of precision
assert np.allclose(result.value / 1000, expected.value / 1000)

# sum
msg = "overflow in timedelta operation"
with pytest.raises(ValueError, match=msg):
(ser - ser.min()).sum()

s1 = ser[0:10000]
with pytest.raises(ValueError, match=msg):
(s1 - s1.min()).sum()
s2 = ser[0:1000]
(s2 - s2.min()).sum()


def test_prod_numpy16_bug():
ser = Series([1.0, 1.0, 1.0], index=range(3))
result = ser.prod()

assert not isinstance(result, Series)


@pytest.mark.parametrize("func", [np.any, np.all])
@pytest.mark.parametrize("kwargs", [dict(keepdims=True), dict(out=object())])
def test_validate_any_all_out_keepdims_raises(kwargs, func):
ser = Series([1, 2])
param = list(kwargs)[0]
name = func.__name__

msg = (
f"the '{param}' parameter is not "
"supported in the pandas "
fr"implementation of {name}\(\)"
)
with pytest.raises(ValueError, match=msg):
func(ser, **kwargs)


def test_validate_sum_initial():
ser = Series([1, 2])
msg = (
r"the 'initial' parameter is not "
r"supported in the pandas "
r"implementation of sum\(\)"
)
with pytest.raises(ValueError, match=msg):
np.sum(ser, initial=10)


def test_validate_median_initial():
ser = Series([1, 2])
msg = (
r"the 'overwrite_input' parameter is not "
r"supported in the pandas "
r"implementation of median\(\)"
)
with pytest.raises(ValueError, match=msg):
# It seems like np.median doesn't dispatch, so we use the
# method instead of the ufunc.
ser.median(overwrite_input=True)


def test_validate_stat_keepdims():
ser = Series([1, 2])
msg = (
r"the 'keepdims' parameter is not "
r"supported in the pandas "
r"implementation of sum\(\)"
)
with pytest.raises(ValueError, match=msg):
np.sum(ser, keepdims=True)
5 changes: 2 additions & 3 deletions pandas/tests/test_lib.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import numpy as np
import pytest

from pandas._libs import lib, writers as libwriters
from pandas._libs import Timestamp, lib, writers as libwriters

import pandas as pd
from pandas import Index
import pandas._testing as tm

Expand Down Expand Up @@ -41,7 +40,7 @@ def test_fast_unique_multiple_list_gen_sort(self):
tm.assert_numpy_array_equal(np.array(out), expected)

def test_fast_unique_multiple_unsortable_runtimewarning(self):
arr = [np.array(["foo", pd.Timestamp("2000")])]
arr = [np.array(["foo", Timestamp("2000")])]
with tm.assert_produces_warning(RuntimeWarning):
lib.fast_unique_multiple(arr, sort=None)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def verify_order(df):
"outer": np.ones(len(out), dtype="bool"),
}

for how in "left", "right", "outer", "inner":
for how in ["left", "right", "outer", "inner"]:
mask = jmask[how]
frame = align(out[mask].copy())
assert mask.all() ^ mask.any() or how == "outer"
Expand Down