Skip to content

Commit 6650f3e

Browse files
committed
add support for strings
1 parent 236d89b commit 6650f3e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

pandas/tests/reshape/test_cut.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,37 @@ def test_bins_not_monotonic():
114114
cut(data, [0.1, 1.5, 1, 10])
115115

116116

117+
@pytest.mark.parametrize(
118+
"data",
119+
[
120+
[0.2, 1.4, 2.5, 6.2, 9.7, 2.1],
121+
np.array([0.2, 1.4, 2.5, 6.2, 9.7, 2.1, 2.575]),
122+
np.array([10, 15, 13, 12, 23, 25, 28, 59, 60]),
123+
range(5),
124+
],
125+
)
126+
@pytest.mark.parametrize(
127+
"bin_str", ["auto", "fd", "doane", "scott", "stone", "rice", "sturges", "sqrt"]
128+
)
129+
def test_bins_from_string(data, bin_str):
130+
# we make sure calling cut(df, str) is equivalent
131+
# to calling cut(df, bins=np.histogram_bin_edges(df,str))
132+
expected = cut(data, bins=np.histogram_bin_edges(data, bins=bin_str))
133+
result = cut(data, bin_str)
134+
tm.assert_categorical_equal(result, expected, check_dtype=False)
135+
136+
137+
def test_bins_from_invalid_string():
138+
# we make sure calling cut(df, str) with invalid string
139+
# throws an error
140+
bin_str = "INVALID_STR"
141+
msg = f"{bin_str!r} is not a valid estimator for `bins`"
142+
data = [0.2, 1.4, 2.5, 6.2, 9.7, 2.1]
143+
144+
with pytest.raises(ValueError, match=msg):
145+
cut(data, bin_str)
146+
147+
117148
@pytest.mark.parametrize(
118149
"x, bins, expected",
119150
[

0 commit comments

Comments
 (0)