From b286300cabf4b0e5d20f2dee49d2720e0ae49426 Mon Sep 17 00:00:00 2001 From: Nikhil-Narayanan Date: Sun, 26 Oct 2025 21:40:23 +0300 Subject: [PATCH 1/2] TST: ensuring bins in cut during tiling are predictably small --- pandas/tests/reshape/test_cut.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pandas/tests/reshape/test_cut.py b/pandas/tests/reshape/test_cut.py index 10335ff716c1f..84fe324af87f3 100644 --- a/pandas/tests/reshape/test_cut.py +++ b/pandas/tests/reshape/test_cut.py @@ -410,6 +410,31 @@ def test_single_bin(data, length): tm.assert_series_equal(result, expected) +def test_single_bin_edge_adjustment(): + # gh-58517 - test edge adjustment when all values are the same + # mutation on _nbins_to_bins + data = [0.1, 0.1, 0.1] + result, bins = cut(data, 3, retbins=True) + + # mutation would create large bin sizes + bin_range = bins[-1] - bins[0] + assert bin_range < 0.001 + + # Test negative values + data_neg = [-0.1, -0.1, -0.1] + result_neg, bins_neg = cut(data_neg, 3, retbins=True) + + bin_range_neg = bins_neg[-1] - bins_neg[0] + assert bin_range_neg < 0.001 + + # Test very small values + data_tiny = [0.01, 0.01, 0.01] + result_tiny, bins_tiny = cut(data_tiny, 3, retbins=True) + + bin_range_tiny = bins_tiny[-1] - bins_tiny[0] + assert bin_range_tiny < 0.0001 + + @pytest.mark.parametrize( "array_1_writeable,array_2_writeable", [(True, True), (True, False), (False, False)] ) From 0599336c1f5b816bec2049481f5dc9b4895ea628 Mon Sep 17 00:00:00 2001 From: Nikhil-Narayanan Date: Wed, 29 Oct 2025 10:17:52 +0300 Subject: [PATCH 2/2] TST: parametrize single_bin_edge_adjustment re mroeschke's review --- pandas/tests/reshape/test_cut.py | 33 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/pandas/tests/reshape/test_cut.py b/pandas/tests/reshape/test_cut.py index 84fe324af87f3..5b535ad0061ba 100644 --- a/pandas/tests/reshape/test_cut.py +++ b/pandas/tests/reshape/test_cut.py @@ -410,29 +410,20 @@ def test_single_bin(data, length): tm.assert_series_equal(result, expected) -def test_single_bin_edge_adjustment(): - # gh-58517 - test edge adjustment when all values are the same - # mutation on _nbins_to_bins - data = [0.1, 0.1, 0.1] - result, bins = cut(data, 3, retbins=True) +@pytest.mark.parametrize( + "values,threshold", + [ + ([0.1, 0.1, 0.1], 0.001), # small positive values + ([-0.1, -0.1, -0.1], 0.001), # negative values + ([0.01, 0.01, 0.01], 0.0001), # very small values + ], +) +def test_single_bin_edge_adjustment(values, threshold): + # gh-58517 - edge adjustment mutation when all values are same + result, bins = cut(values, 3, retbins=True) - # mutation would create large bin sizes bin_range = bins[-1] - bins[0] - assert bin_range < 0.001 - - # Test negative values - data_neg = [-0.1, -0.1, -0.1] - result_neg, bins_neg = cut(data_neg, 3, retbins=True) - - bin_range_neg = bins_neg[-1] - bins_neg[0] - assert bin_range_neg < 0.001 - - # Test very small values - data_tiny = [0.01, 0.01, 0.01] - result_tiny, bins_tiny = cut(data_tiny, 3, retbins=True) - - bin_range_tiny = bins_tiny[-1] - bins_tiny[0] - assert bin_range_tiny < 0.0001 + assert bin_range < threshold @pytest.mark.parametrize(