-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Return DatetimeIndex or TimedeltaIndex bins for q/cut when input is datelike #20956
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 8 commits
2bc5d24
17ebfbe
ccf4b96
326258d
86f113d
b4b652b
c70bf40
125c425
4b3788b
1a67044
c3e2c7e
876d9ee
4ef7309
5f53579
17e96c4
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 |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
from pandas._libs.lib import infer_dtype | ||
from pandas import (to_timedelta, to_datetime, | ||
Categorical, Timestamp, Timedelta, | ||
Series, Interval, IntervalIndex) | ||
Series, Index, Interval, IntervalIndex) | ||
|
||
import numpy as np | ||
|
||
|
@@ -364,6 +364,8 @@ def _bins_to_cuts(x, bins, right=True, labels=None, | |
result = result.astype(np.float64) | ||
np.putmask(result, na_mask, np.nan) | ||
|
||
bins = _convert_bin_to_datelike_type(bins, dtype) | ||
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. instead of this just call 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 there a way to make this logic flow better, e.g. 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. Well they both serve different purposes;
Plus, I like the explicit naming of these two processes. Just my 2c. 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. ok that's fine then. |
||
|
||
return result, bins | ||
|
||
|
||
|
@@ -428,6 +430,29 @@ def _convert_bin_to_numeric_type(bins, dtype): | |
return bins | ||
|
||
|
||
def _convert_bin_to_datelike_type(bins, dtype): | ||
""" | ||
Convert bins to a DatetimeIndex or TimedeltaIndex if the orginal dtype is | ||
datelike | ||
|
||
Parameters | ||
---------- | ||
bins : list-like of bins | ||
dtype : dtype of data | ||
|
||
Returns | ||
------- | ||
bins : Array-like of bins, DatetimeIndex or TimedeltaIndex if dtype is | ||
datelike | ||
""" | ||
# Can be simplified once GH 20964 is fixed. | ||
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. would like to fix #20964 first |
||
if is_datetime64tz_dtype(dtype): | ||
bins = to_datetime(bins, utc=True).tz_convert(dtype.tz) | ||
elif is_datetime64_dtype(dtype) or is_timedelta64_dtype(dtype): | ||
bins = Index(bins, dtype=dtype) | ||
return bins | ||
|
||
|
||
def _format_labels(bins, precision, right=True, | ||
include_lowest=False, dtype=None): | ||
""" based on the dtype, return our labels """ | ||
|
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.24