Skip to content

Commit ebc1b68

Browse files
committed
Updated type annotations, whatsnew doc, and specific tests added and passed
1 parent d308111 commit ebc1b68

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ Other enhancements
232232
- Support reading Stata 102-format (Stata 1) dta files (:issue:`58978`)
233233
- Support reading Stata 110-format (Stata 7) dta files (:issue:`47176`)
234234
- Switched wheel upload to **PyPI Trusted Publishing** (OIDC) for release-tag pushes in ``wheels.yml``. (:issue:`61718`)
235-
-
235+
- :func:`qcut` now accepts ``right`` as optional arguments, as in :meth:`cut` (:issue:`62938`)
236236

237237
.. ---------------------------------------------------------------------------
238238
.. _whatsnew_300.notable_bug_fixes:

pandas/core/reshape/tile.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,18 +317,18 @@ def qcut(
317317
Used as labels for the resulting bins. Must be of the same length as
318318
the resulting bins. If False, return only integer indicators of the
319319
bins. If True, raises an error.
320+
right : bool, default True
321+
Indicates whether `bins` includes the rightmost edge or not. If
322+
``right == True`` (the default), then the `bins` ``[1, 2, 3, 4]``
323+
indicate (1,2], (2,3], (3,4]. This argument is ignored when
324+
`bins` is an IntervalIndex.
320325
retbins : bool, optional
321326
Whether to return the (bins, labels) or not. Can be useful if bins
322327
is given as a scalar.
323328
precision : int, optional
324329
The precision at which to store and display the bins labels.
325330
duplicates : {default 'raise', 'drop'}, optional
326331
If bin edges are not unique, raise ValueError or drop non-uniques.
327-
right : bool, default True
328-
Indicates whether `bins` includes the rightmost edge or not. If
329-
``right == True`` (the default), then the `bins` ``[1, 2, 3, 4]``
330-
indicate (1,2], (2,3], (3,4]. This argument is ignored when
331-
`bins` is an IntervalIndex.
332332
333333
Returns
334334
-------

pandas/tests/reshape/test_qcut.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,40 @@ def test_qcut():
4444
tm.assert_categorical_equal(labels, ex_levels)
4545

4646

47+
def test_qcut_right():
48+
arr = np.random.default_rng(2).standard_normal(1000)
49+
50+
# We store the bins as Index that have been
51+
# rounded to comparisons are a bit tricky.
52+
labels, _ = qcut(arr, 4, retbins=True, right=True)
53+
ex_bins = np.quantile(arr, [0, 0.25, 0.5, 0.75, 1.0])
54+
55+
result = labels.categories.left.values
56+
assert np.allclose(result, ex_bins[:-1], atol=1e-2)
57+
58+
result = labels.categories.right.values
59+
assert np.allclose(result, ex_bins[1:], atol=1e-2)
60+
61+
ex_levels = cut(arr, ex_bins, include_lowest=True, right=True)
62+
tm.assert_categorical_equal(labels, ex_levels)
63+
64+
65+
def test_qcut_no_right():
66+
arr = np.random.default_rng(2).standard_normal(1000)
67+
68+
labels, _ = qcut(arr, 4, retbins=True, right=False)
69+
ex_bins = np.quantile(arr, [0, 0.25, 0.5, 0.75, 1.0])
70+
71+
lefts = labels.categories.left.values
72+
assert np.allclose(lefts, ex_bins[:-1], atol=1e-2)
73+
74+
rights = labels.categories.right.values
75+
assert np.allclose(rights, ex_bins[1:], atol=1e-2)
76+
77+
ex_levels = cut(arr, ex_bins, include_lowest=True, right=False)
78+
tm.assert_categorical_equal(labels, ex_levels)
79+
80+
4781
def test_qcut_bounds():
4882
arr = np.random.default_rng(2).standard_normal(1000)
4983

0 commit comments

Comments
 (0)