-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: numpy histogram bin edges in cut (GH 14627) #23567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4be1ffa
f483d6a
f3323f4
d7b2e3e
79b7145
1d9e475
e6b36e7
43d61f9
3bc1f87
e6b2df3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ | |
is_scalar, is_timedelta64_dtype) | ||
from pandas.core.dtypes.missing import isna | ||
|
||
from pandas.compat import string_types | ||
|
||
from pandas import ( | ||
Categorical, Index, Interval, IntervalIndex, Series, Timedelta, Timestamp, | ||
to_datetime, to_timedelta) | ||
|
@@ -35,7 +37,7 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3, | |
---------- | ||
x : array-like | ||
The input array to be binned. Must be 1-dimensional. | ||
bins : int, sequence of scalars, or pandas.IntervalIndex | ||
bins : int, str, sequence of scalars, or pandas.IntervalIndex | ||
The criteria to bin by. | ||
|
||
* int : Defines the number of equal-width bins in the range of `x`. The | ||
|
@@ -44,6 +46,10 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3, | |
* sequence of scalars : Defines the bin edges allowing for non-uniform | ||
width. No extension of the range of `x` is done. | ||
* IntervalIndex : Defines the exact bins to be used. | ||
* str : Bin calculaton dispatched to :func:`np.histogram_bin_edges`. See that | ||
documentation for details. | ||
|
||
.. versionadded:: 0.24.0 | ||
|
||
right : bool, default True | ||
Indicates whether `bins` includes the rightmost edge or not. If | ||
|
@@ -83,11 +89,11 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3, | |
|
||
* False : returns an ndarray of integers. | ||
|
||
bins : numpy.ndarray or IntervalIndex. | ||
bins : numpy.ndarray or IntervalIndex | ||
The computed or specified bins. Only returned when `retbins=True`. | ||
For scalar or sequence `bins`, this is an ndarray with the computed | ||
bins. If set `duplicates=drop`, `bins` will drop non-unique bin. For | ||
an IntervalIndex `bins`, this is equal to `bins`. | ||
For scalar, str, or sequence `bins`, this is an ndarray with the | ||
computed bins. If set `duplicates=drop`, `bins` will drop non-unique | ||
bin. For an IntervalIndex `bins`, this is equal to `bins`. | ||
|
||
See Also | ||
-------- | ||
|
@@ -98,6 +104,8 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3, | |
Series : One-dimensional array with axis labels (including time series). | ||
pandas.IntervalIndex : Immutable Index implementing an ordered, | ||
sliceable set. | ||
numpy.histogram_bin_edges : Bin calculation dispatched to this method when | ||
`bins` is a string. | ||
|
||
Notes | ||
----- | ||
|
@@ -181,14 +189,45 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3, | |
>>> pd.cut([0, 0.5, 1.5, 2.5, 4.5], bins) | ||
[NaN, (0, 1], NaN, (2, 3], (4, 5]] | ||
Categories (3, interval[int64]): [(0, 1] < (2, 3] < (4, 5]] | ||
|
||
Passng a string for `bins` dispatches the bin calculation to numpy's | ||
gfyoung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`histogram_bin_edges`. (Starting in version 0.24.) | ||
|
||
>>> pd.cut(array([0.1, 0.1, 0.2, 0.5, 0.5, 0.9, 1.0]), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. leave a blank line before this line |
||
... bins="auto") # doctest: +ELLIPSIS` | ||
[(0.0991, 0.325], (0.0991, 0.325], (0.0991, 0.325], (0.325, 0.55], | ||
(0.325, 0.55], (0.775, 1.0], (0.775, 1.0]] | ||
Categories (4, interval[float64]): [(0.0991, 0.325] < (0.325, 0.55] < | ||
(0.55, 0.775] < (0.775, 1.0]] | ||
""" | ||
# NOTE: this binning code is changed a bit from histogram for var(x) == 0 | ||
|
||
# for handling the cut for datetime and timedelta objects | ||
x_is_series, series_index, name, x = _preprocess_for_cut(x) | ||
x, dtype = _coerce_to_type(x) | ||
|
||
if not np.iterable(bins): | ||
if isinstance(bins, string_types): | ||
# GH 14627 | ||
# NOTE: when the minimum numpy requirement is | ||
# increased to 1.15, the try-except statement | ||
# can be removed. | ||
try: | ||
bins = np.histogram_bin_edges(x, bins) | ||
except AttributeError: | ||
cnt, bins = np.histogram(x, bins) | ||
|
||
mn, mx = bins[0], bins[-1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this equivalent to doing |
||
adj = (mx - mn) | ||
if adj: | ||
adj *= 0.001 # 0.1% of the range | ||
else: | ||
adj = 0.001 | ||
if right: | ||
bins[0] -= adj | ||
else: | ||
bins[-1] += adj | ||
|
||
elif not np.iterable(bins): | ||
if is_scalar(bins) and bins < 1: | ||
raise ValueError("`bins` should be a positive integer.") | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to 0.25 at this point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsure how to get my fork to update here. I recently changed my GH id and I'm unsure if that's getting in the way. Any ideas?