|
6 | 6 | import numpy as np
|
7 | 7 | import pytest
|
8 | 8 |
|
9 |
| -from pandas.compat import is_platform_windows |
| 9 | +from pandas.compat import ( |
| 10 | + IS64, |
| 11 | + is_platform_windows, |
| 12 | +) |
10 | 13 | import pandas.util._test_decorators as td
|
11 | 14 |
|
12 | 15 | import pandas as pd
|
|
29 | 32 | nanops,
|
30 | 33 | )
|
31 | 34 |
|
| 35 | +is_windows_or_is32 = is_platform_windows() or not IS64 |
| 36 | + |
32 | 37 |
|
33 | 38 | def assert_stat_op_calc(
|
34 | 39 | opname,
|
@@ -917,7 +922,7 @@ def test_mean_extensionarray_numeric_only_true(self):
|
917 | 922 | arr = np.random.randint(1000, size=(10, 5))
|
918 | 923 | df = DataFrame(arr, dtype="Int64")
|
919 | 924 | result = df.mean(numeric_only=True)
|
920 |
| - expected = DataFrame(arr).mean() |
| 925 | + expected = DataFrame(arr).mean().astype("Float64") |
921 | 926 | tm.assert_series_equal(result, expected)
|
922 | 927 |
|
923 | 928 | def test_stats_mixed_type(self, float_string_frame):
|
@@ -1626,6 +1631,101 @@ def test_min_max_categorical_dtype_non_ordered_nuisance_column(self, method):
|
1626 | 1631 | getattr(np, method)(df, axis=0)
|
1627 | 1632 |
|
1628 | 1633 |
|
| 1634 | +class TestEmptyDataFrameReductions: |
| 1635 | + @pytest.mark.parametrize( |
| 1636 | + "opname, dtype, exp_value, exp_dtype", |
| 1637 | + [ |
| 1638 | + ("sum", np.int8, 0, np.int64), |
| 1639 | + ("prod", np.int8, 1, np.int_), |
| 1640 | + ("sum", np.int64, 0, np.int64), |
| 1641 | + ("prod", np.int64, 1, np.int64), |
| 1642 | + ("sum", np.uint8, 0, np.int64), |
| 1643 | + ("prod", np.uint8, 1, np.uint), |
| 1644 | + ("sum", np.uint64, 0, np.int64), |
| 1645 | + ("prod", np.uint64, 1, np.uint64), |
| 1646 | + ("sum", np.float32, 0, np.float32), |
| 1647 | + ("prod", np.float32, 1, np.float32), |
| 1648 | + ("sum", np.float64, 0, np.float64), |
| 1649 | + ], |
| 1650 | + ) |
| 1651 | + def test_df_empty_min_count_0(self, opname, dtype, exp_value, exp_dtype): |
| 1652 | + df = DataFrame({0: [], 1: []}, dtype=dtype) |
| 1653 | + result = getattr(df, opname)(min_count=0) |
| 1654 | + |
| 1655 | + expected = Series([exp_value, exp_value], dtype=exp_dtype) |
| 1656 | + tm.assert_series_equal(result, expected) |
| 1657 | + |
| 1658 | + @pytest.mark.parametrize( |
| 1659 | + "opname, dtype, exp_dtype", |
| 1660 | + [ |
| 1661 | + ("sum", np.int8, np.float64), |
| 1662 | + ("prod", np.int8, np.float64), |
| 1663 | + ("sum", np.int64, np.float64), |
| 1664 | + ("prod", np.int64, np.float64), |
| 1665 | + ("sum", np.uint8, np.float64), |
| 1666 | + ("prod", np.uint8, np.float64), |
| 1667 | + ("sum", np.uint64, np.float64), |
| 1668 | + ("prod", np.uint64, np.float64), |
| 1669 | + ("sum", np.float32, np.float32), |
| 1670 | + ("prod", np.float32, np.float32), |
| 1671 | + ("sum", np.float64, np.float64), |
| 1672 | + ], |
| 1673 | + ) |
| 1674 | + def test_df_empty_min_count_1(self, opname, dtype, exp_dtype): |
| 1675 | + df = DataFrame({0: [], 1: []}, dtype=dtype) |
| 1676 | + result = getattr(df, opname)(min_count=1) |
| 1677 | + |
| 1678 | + expected = Series([np.nan, np.nan], dtype=exp_dtype) |
| 1679 | + tm.assert_series_equal(result, expected) |
| 1680 | + |
| 1681 | + @pytest.mark.parametrize( |
| 1682 | + "opname, dtype, exp_value, exp_dtype", |
| 1683 | + [ |
| 1684 | + ("sum", "Int8", 0, ("Int32" if is_windows_or_is32 else "Int64")), |
| 1685 | + ("prod", "Int8", 1, ("Int32" if is_windows_or_is32 else "Int64")), |
| 1686 | + ("prod", "Int8", 1, ("Int32" if is_windows_or_is32 else "Int64")), |
| 1687 | + ("sum", "Int64", 0, "Int64"), |
| 1688 | + ("prod", "Int64", 1, "Int64"), |
| 1689 | + ("sum", "UInt8", 0, ("UInt32" if is_windows_or_is32 else "UInt64")), |
| 1690 | + ("prod", "UInt8", 1, ("UInt32" if is_windows_or_is32 else "UInt64")), |
| 1691 | + ("sum", "UInt64", 0, "UInt64"), |
| 1692 | + ("prod", "UInt64", 1, "UInt64"), |
| 1693 | + ("sum", "Float32", 0, "Float32"), |
| 1694 | + ("prod", "Float32", 1, "Float32"), |
| 1695 | + ("sum", "Float64", 0, "Float64"), |
| 1696 | + ], |
| 1697 | + ) |
| 1698 | + def test_df_empty_nullable_min_count_0(self, opname, dtype, exp_value, exp_dtype): |
| 1699 | + df = DataFrame({0: [], 1: []}, dtype=dtype) |
| 1700 | + result = getattr(df, opname)(min_count=0) |
| 1701 | + |
| 1702 | + expected = Series([exp_value, exp_value], dtype=exp_dtype) |
| 1703 | + tm.assert_series_equal(result, expected) |
| 1704 | + |
| 1705 | + @pytest.mark.parametrize( |
| 1706 | + "opname, dtype, exp_dtype", |
| 1707 | + [ |
| 1708 | + ("sum", "Int8", "Int64"), |
| 1709 | + ("prod", "Int8", "Int64"), |
| 1710 | + ("sum", "Int64", "Int64"), |
| 1711 | + ("prod", "Int64", "Int64"), |
| 1712 | + ("sum", "UInt8", "UInt64"), |
| 1713 | + ("prod", "UInt8", "UInt64"), |
| 1714 | + ("sum", "UInt64", "UInt64"), |
| 1715 | + ("prod", "UInt64", "UInt64"), |
| 1716 | + ("sum", "Float32", "Float32"), |
| 1717 | + ("prod", "Float32", "Float32"), |
| 1718 | + ("sum", "Float64", "Float64"), |
| 1719 | + ], |
| 1720 | + ) |
| 1721 | + def test_df_empty_nullable_min_count_1(self, opname, dtype, exp_dtype): |
| 1722 | + df = DataFrame({0: [], 1: []}, dtype=dtype) |
| 1723 | + result = getattr(df, opname)(min_count=1) |
| 1724 | + |
| 1725 | + expected = Series([pd.NA, pd.NA], dtype=exp_dtype) |
| 1726 | + tm.assert_series_equal(result, expected) |
| 1727 | + |
| 1728 | + |
1629 | 1729 | def test_sum_timedelta64_skipna_false(using_array_manager, request):
|
1630 | 1730 | # GH#17235
|
1631 | 1731 | if using_array_manager:
|
|
0 commit comments