Skip to content

Commit fbc1cf8

Browse files
committed
doc example and bug
1 parent 7577335 commit fbc1cf8

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

doc/source/reshaping.rst

+9-1
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,15 @@ Alternatively we can specify custom bin-edges:
517517

518518
.. ipython:: python
519519
520-
pd.cut(ages, bins=[0, 18, 35, 70])
520+
c = pd.cut(ages, bins=[0, 18, 35, 70])
521+
c
522+
523+
.. versionadded:: 0.20.0
524+
525+
If the ``bins`` keyword is an ``IntervalIndex``, then these will be
526+
used to bin the passed data.
527+
528+
pd.cut([25, 20, 50], bins=c.categories)
521529

522530

523531
.. _reshaping.dummies:

pandas/tests/tools/test_tile.py

+12
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ def test_bins_from_intervalindex(self):
7171
result = cut(range(6), bins=expected.categories)
7272
tm.assert_categorical_equal(result, expected)
7373

74+
# doc example
75+
# make sure we preserve the bins
76+
ages = np.array([10, 15, 13, 12, 23, 25, 28, 59, 60])
77+
c = cut(ages, bins=[0, 18, 35, 70])
78+
expected = IntervalIndex.from_tuples([(0, 18), (18, 35), (35, 70)])
79+
tm.assert_index_equal(c.categories, expected)
80+
81+
result = cut([25, 20, 50], bins=c.categories)
82+
tm.assert_index_equal(result.categories, expected)
83+
tm.assert_numpy_array_equal(result.codes,
84+
np.array([1, 1, 2], dtype='int8'))
85+
7486
def test_bins_not_monotonic(self):
7587
data = [.2, 1.4, 2.5, 6.2, 9.7, 2.1]
7688
self.assertRaises(ValueError, cut, data, [0.1, 1.5, 1, 10])

pandas/tools/tile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def _bins_to_cuts(x, bins, right=True, labels=None,
220220
# we have a fast-path here
221221
ids = bins.get_indexer(x)
222222
result = algos.take_nd(bins, ids)
223-
result = Categorical(result, ordered=True)
223+
result = Categorical(result, categories=bins, ordered=True)
224224
return result, bins
225225

226226
unique_bins = algos.unique(bins)

0 commit comments

Comments
 (0)