|
2 | 2 | datetime,
|
3 | 3 | timedelta,
|
4 | 4 | )
|
| 5 | +from decimal import Decimal |
5 | 6 |
|
6 | 7 | import numpy as np
|
7 | 8 | import pytest
|
@@ -1070,27 +1071,89 @@ def test_timedelta64_analytics(self):
|
1070 | 1071 | (Series(["foo", "foo", "bar", "bar", None, np.nan, "baz"]), TypeError),
|
1071 | 1072 | ],
|
1072 | 1073 | )
|
1073 |
| - def test_assert_idxminmax_raises(self, test_input, error_type): |
| 1074 | + def test_assert_idxminmax_empty_raises(self, test_input, error_type): |
1074 | 1075 | """
|
1075 | 1076 | Cases where ``Series.argmax`` and related should raise an exception
|
1076 | 1077 | """
|
1077 |
| - msg = ( |
1078 |
| - "reduction operation 'argmin' not allowed for this dtype|" |
1079 |
| - "attempt to get argmin of an empty sequence" |
1080 |
| - ) |
1081 |
| - with pytest.raises(error_type, match=msg): |
| 1078 | + test_input = Series([], dtype="float64") |
| 1079 | + msg = "attempt to get argmin of an empty sequence" |
| 1080 | + with pytest.raises(ValueError, match=msg): |
1082 | 1081 | test_input.idxmin()
|
1083 |
| - with pytest.raises(error_type, match=msg): |
| 1082 | + with pytest.raises(ValueError, match=msg): |
1084 | 1083 | test_input.idxmin(skipna=False)
|
1085 |
| - msg = ( |
1086 |
| - "reduction operation 'argmax' not allowed for this dtype|" |
1087 |
| - "attempt to get argmax of an empty sequence" |
1088 |
| - ) |
1089 |
| - with pytest.raises(error_type, match=msg): |
| 1084 | + msg = "attempt to get argmax of an empty sequence" |
| 1085 | + with pytest.raises(ValueError, match=msg): |
1090 | 1086 | test_input.idxmax()
|
1091 |
| - with pytest.raises(error_type, match=msg): |
| 1087 | + with pytest.raises(ValueError, match=msg): |
1092 | 1088 | test_input.idxmax(skipna=False)
|
1093 | 1089 |
|
| 1090 | + def test_idxminmax_object_dtype(self): |
| 1091 | + # pre-2.1 object-dtype was disallowed for argmin/max |
| 1092 | + ser = Series(["foo", "bar", "baz"]) |
| 1093 | + assert ser.idxmax() == 0 |
| 1094 | + assert ser.idxmax(skipna=False) == 0 |
| 1095 | + assert ser.idxmin() == 1 |
| 1096 | + assert ser.idxmin(skipna=False) == 1 |
| 1097 | + |
| 1098 | + ser2 = Series([(1,), (2,)]) |
| 1099 | + assert ser2.idxmax() == 1 |
| 1100 | + assert ser2.idxmax(skipna=False) == 1 |
| 1101 | + assert ser2.idxmin() == 0 |
| 1102 | + assert ser2.idxmin(skipna=False) == 0 |
| 1103 | + |
| 1104 | + # attempting to compare np.nan with string raises |
| 1105 | + ser3 = Series(["foo", "foo", "bar", "bar", None, np.nan, "baz"]) |
| 1106 | + msg = "'>' not supported between instances of 'float' and 'str'" |
| 1107 | + with pytest.raises(TypeError, match=msg): |
| 1108 | + ser3.idxmax() |
| 1109 | + with pytest.raises(TypeError, match=msg): |
| 1110 | + ser3.idxmax(skipna=False) |
| 1111 | + msg = "'<' not supported between instances of 'float' and 'str'" |
| 1112 | + with pytest.raises(TypeError, match=msg): |
| 1113 | + ser3.idxmin() |
| 1114 | + with pytest.raises(TypeError, match=msg): |
| 1115 | + ser3.idxmin(skipna=False) |
| 1116 | + |
| 1117 | + def test_idxminmax_object_frame(self): |
| 1118 | + # GH#4279 |
| 1119 | + df = DataFrame([["zimm", 2.5], ["biff", 1.0], ["bid", 12.0]]) |
| 1120 | + res = df.idxmax() |
| 1121 | + exp = Series([0, 2]) |
| 1122 | + tm.assert_series_equal(res, exp) |
| 1123 | + |
| 1124 | + def test_idxminmax_object_tuples(self): |
| 1125 | + # GH#43697 |
| 1126 | + ser = Series([(1, 3), (2, 2), (3, 1)]) |
| 1127 | + assert ser.idxmax() == 2 |
| 1128 | + assert ser.idxmin() == 0 |
| 1129 | + assert ser.idxmax(skipna=False) == 2 |
| 1130 | + assert ser.idxmin(skipna=False) == 0 |
| 1131 | + |
| 1132 | + def test_idxminmax_object_decimals(self): |
| 1133 | + # GH#40685 |
| 1134 | + df = DataFrame( |
| 1135 | + { |
| 1136 | + "idx": [0, 1], |
| 1137 | + "x": [Decimal("8.68"), Decimal("42.23")], |
| 1138 | + "y": [Decimal("7.11"), Decimal("79.61")], |
| 1139 | + } |
| 1140 | + ) |
| 1141 | + res = df.idxmax() |
| 1142 | + exp = Series({"idx": 1, "x": 1, "y": 1}) |
| 1143 | + tm.assert_series_equal(res, exp) |
| 1144 | + |
| 1145 | + res2 = df.idxmin() |
| 1146 | + exp2 = exp - 1 |
| 1147 | + tm.assert_series_equal(res2, exp2) |
| 1148 | + |
| 1149 | + def test_argminmax_object_ints(self): |
| 1150 | + # GH#18021 |
| 1151 | + ser = Series([0, 1], dtype="object") |
| 1152 | + assert ser.argmax() == 1 |
| 1153 | + assert ser.argmin() == 0 |
| 1154 | + assert ser.argmax(skipna=False) == 1 |
| 1155 | + assert ser.argmin(skipna=False) == 0 |
| 1156 | + |
1094 | 1157 | def test_idxminmax_with_inf(self):
|
1095 | 1158 | # For numeric data with NA and Inf (GH #13595)
|
1096 | 1159 | s = Series([0, -np.inf, np.inf, np.nan])
|
|
0 commit comments