|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +from pandas import ( |
| 5 | + DataFrame, |
| 6 | + Index, |
| 7 | + Series, |
| 8 | +) |
| 9 | +import pandas._testing as tm |
| 10 | + |
| 11 | + |
| 12 | +class TestDataFrameCount: |
| 13 | + def test_count_multiindex(self, multiindex_dataframe_random_data): |
| 14 | + frame = multiindex_dataframe_random_data |
| 15 | + |
| 16 | + frame = frame.copy() |
| 17 | + frame.index.names = ["a", "b"] |
| 18 | + |
| 19 | + with tm.assert_produces_warning(FutureWarning): |
| 20 | + result = frame.count(level="b") |
| 21 | + with tm.assert_produces_warning(FutureWarning): |
| 22 | + expected = frame.count(level=1) |
| 23 | + tm.assert_frame_equal(result, expected, check_names=False) |
| 24 | + |
| 25 | + with tm.assert_produces_warning(FutureWarning): |
| 26 | + result = frame.count(level="a") |
| 27 | + with tm.assert_produces_warning(FutureWarning): |
| 28 | + expected = frame.count(level=0) |
| 29 | + tm.assert_frame_equal(result, expected, check_names=False) |
| 30 | + |
| 31 | + msg = "Level x not found" |
| 32 | + with pytest.raises(KeyError, match=msg): |
| 33 | + with tm.assert_produces_warning(FutureWarning): |
| 34 | + frame.count(level="x") |
| 35 | + |
| 36 | + def test_count_level_corner(self, multiindex_dataframe_random_data): |
| 37 | + frame = multiindex_dataframe_random_data |
| 38 | + |
| 39 | + ser = frame["A"][:0] |
| 40 | + with tm.assert_produces_warning(FutureWarning): |
| 41 | + result = ser.count(level=0) |
| 42 | + expected = Series(0, index=ser.index.levels[0], name="A") |
| 43 | + tm.assert_series_equal(result, expected) |
| 44 | + |
| 45 | + df = frame[:0] |
| 46 | + with tm.assert_produces_warning(FutureWarning): |
| 47 | + result = df.count(level=0) |
| 48 | + expected = ( |
| 49 | + DataFrame( |
| 50 | + index=ser.index.levels[0].set_names(["first"]), columns=df.columns |
| 51 | + ) |
| 52 | + .fillna(0) |
| 53 | + .astype(np.int64) |
| 54 | + ) |
| 55 | + tm.assert_frame_equal(result, expected) |
| 56 | + |
| 57 | + def test_count_index_with_nan(self): |
| 58 | + # https://github.com/pandas-dev/pandas/issues/21824 |
| 59 | + df = DataFrame( |
| 60 | + { |
| 61 | + "Person": ["John", "Myla", None, "John", "Myla"], |
| 62 | + "Age": [24.0, 5, 21.0, 33, 26], |
| 63 | + "Single": [False, True, True, True, False], |
| 64 | + } |
| 65 | + ) |
| 66 | + |
| 67 | + # count on row labels |
| 68 | + with tm.assert_produces_warning(FutureWarning): |
| 69 | + res = df.set_index(["Person", "Single"]).count(level="Person") |
| 70 | + expected = DataFrame( |
| 71 | + index=Index(["John", "Myla"], name="Person"), |
| 72 | + columns=Index(["Age"]), |
| 73 | + data=[2, 2], |
| 74 | + ) |
| 75 | + tm.assert_frame_equal(res, expected) |
| 76 | + |
| 77 | + # count on column labels |
| 78 | + with tm.assert_produces_warning(FutureWarning): |
| 79 | + res = df.set_index(["Person", "Single"]).T.count(level="Person", axis=1) |
| 80 | + expected = DataFrame( |
| 81 | + columns=Index(["John", "Myla"], name="Person"), |
| 82 | + index=Index(["Age"]), |
| 83 | + data=[[2, 2]], |
| 84 | + ) |
| 85 | + tm.assert_frame_equal(res, expected) |
| 86 | + |
| 87 | + def test_count_level( |
| 88 | + self, |
| 89 | + multiindex_year_month_day_dataframe_random_data, |
| 90 | + multiindex_dataframe_random_data, |
| 91 | + ): |
| 92 | + ymd = multiindex_year_month_day_dataframe_random_data |
| 93 | + frame = multiindex_dataframe_random_data |
| 94 | + |
| 95 | + def _check_counts(frame, axis=0): |
| 96 | + index = frame._get_axis(axis) |
| 97 | + for i in range(index.nlevels): |
| 98 | + with tm.assert_produces_warning(FutureWarning): |
| 99 | + result = frame.count(axis=axis, level=i) |
| 100 | + expected = frame.groupby(axis=axis, level=i).count() |
| 101 | + expected = expected.reindex_like(result).astype("i8") |
| 102 | + tm.assert_frame_equal(result, expected) |
| 103 | + |
| 104 | + frame.iloc[1, [1, 2]] = np.nan |
| 105 | + frame.iloc[7, [0, 1]] = np.nan |
| 106 | + ymd.iloc[1, [1, 2]] = np.nan |
| 107 | + ymd.iloc[7, [0, 1]] = np.nan |
| 108 | + |
| 109 | + _check_counts(frame) |
| 110 | + _check_counts(ymd) |
| 111 | + _check_counts(frame.T, axis=1) |
| 112 | + _check_counts(ymd.T, axis=1) |
| 113 | + |
| 114 | + # can't call with level on regular DataFrame |
| 115 | + df = tm.makeTimeDataFrame() |
| 116 | + with pytest.raises(TypeError, match="hierarchical"): |
| 117 | + with tm.assert_produces_warning(FutureWarning): |
| 118 | + df.count(level=0) |
| 119 | + |
| 120 | + frame["D"] = "foo" |
| 121 | + with tm.assert_produces_warning(FutureWarning): |
| 122 | + result = frame.count(level=0, numeric_only=True) |
| 123 | + tm.assert_index_equal(result.columns, Index(list("ABC"), name="exp")) |
0 commit comments