diff --git a/pandas/tests/series/test_analytics.py b/pandas/tests/series/test_analytics.py index 6ba55ce3c74b9..1a469d3e3d88b 100644 --- a/pandas/tests/series/test_analytics.py +++ b/pandas/tests/series/test_analytics.py @@ -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"]) @@ -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() diff --git a/pandas/tests/series/test_reductions.py b/pandas/tests/series/test_reductions.py index be9330a14f9c9..28d29c69f6526 100644 --- a/pandas/tests/series/test_reductions.py +++ b/pandas/tests/series/test_reductions.py @@ -1,3 +1,6 @@ +import numpy as np +import pytest + import pandas as pd from pandas import Series @@ -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) diff --git a/pandas/tests/test_lib.py b/pandas/tests/test_lib.py index b6f59807eaa15..c9c34916be32b 100644 --- a/pandas/tests/test_lib.py +++ b/pandas/tests/test_lib.py @@ -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 @@ -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) diff --git a/pandas/tests/test_sorting.py b/pandas/tests/test_sorting.py index deb7434694d01..1c9fd46ae451f 100644 --- a/pandas/tests/test_sorting.py +++ b/pandas/tests/test_sorting.py @@ -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"