-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Allow drop bins when using the cut function #20947
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c9ec7de
Allow drop bins when using the cut function
FANGOD dbf8cc2
added parameters
FANGOD 6b8babf
added examples to the cut function
FANGOD 1ec414b
Modify according to PEP8
FANGOD 2b0e07b
Allow drop bins when using the cut function
FANGOD 2ac70fe
add test_tile.py
FANGOD 251d007
Update test_tile.py
FANGOD 9b7eec3
add a versionadded tag and update doc-string
FANGOD 8b2650d
add a whatsnew entry for 'duplicates' option
FANGOD 8ff6fd5
Update some
FANGOD 9376d01
Update some
FANGOD 6b054ba
Update some
FANGOD b0b63e2
Merge branch 'master' into PR_TOOL_MERGE_PR_20947
jreback 185758e
lint
jreback 987856f
add test
jreback 46c9bca
Merge branch 'master' into PR_TOOL_MERGE_PR_20947
jreback File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ | |
|
||
|
||
def cut(x, bins, right=True, labels=None, retbins=False, precision=3, | ||
include_lowest=False): | ||
include_lowest=False, duplicates='raise'): | ||
""" | ||
Bin values into discrete intervals. | ||
|
||
|
@@ -65,6 +65,8 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3, | |
The precision at which to store and display the bins labels. | ||
include_lowest : bool, default False | ||
Whether the first interval should be left-inclusive or not. | ||
duplicates : {default 'raise', 'drop'}, optional | ||
If bin edges are not unique, raise ValueError or drop non-uniques. | ||
|
||
Returns | ||
------- | ||
|
@@ -144,6 +146,32 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3, | |
dtype: category | ||
Categories (3, interval[float64]): [(1.992, 4.667] < (4.667, ... | ||
|
||
Passing a Series as an input returns a Series with mapping value. | ||
It is used to map numerically to intervals based on bins. | ||
|
||
>>> s = pd.Series(np.array([2, 4, 6, 8, 10]), | ||
... index=['a', 'b', 'c', 'd', 'e']) | ||
>>> pd.cut(s, [0, 2, 4, 6, 8, 10], labels=False, retbins=True, right=False) | ||
... # doctest: +ELLIPSIS | ||
(a 0.0 | ||
b 1.0 | ||
c 2.0 | ||
d 3.0 | ||
e 4.0 | ||
dtype: float64, array([0, 2, 4, 6, 8])) | ||
|
||
``duplicates=drop`` drop non-uniques | ||
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. make this a sentence |
||
|
||
>>> pd.cut(s, [0, 2, 4, 6, 10, 10], labels=False, retbins=True, | ||
... right=False, duplicates='drop') | ||
... # doctest: +ELLIPSIS | ||
(a 0.0 | ||
b 1.0 | ||
c 2.0 | ||
d 3.0 | ||
e 3.0 | ||
dtype: float64, array([0, 2, 4, 6, 8])) | ||
|
||
Passing an IntervalIndex for `bins` results in those categories exactly. | ||
Notice that values not covered by the IntervalIndex are set to NaN. 0 | ||
is to the left of the first bin (which is closed on the right), and 1.5 | ||
|
@@ -199,7 +227,8 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3, | |
fac, bins = _bins_to_cuts(x, bins, right=right, labels=labels, | ||
precision=precision, | ||
include_lowest=include_lowest, | ||
dtype=dtype) | ||
dtype=dtype, | ||
duplicates=duplicates) | ||
|
||
return _postprocess_for_cut(fac, bins, retbins, x_is_series, | ||
series_index, name) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
add a versionadded tag
add a Raises part of the doc-string