Skip to content

allowing datetime and timedelta datatype in pd cut bins #14798

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion pandas/tools/tests/test_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pandas.core.algorithms import quantile
from pandas.tools.tile import cut, qcut
import pandas.tools.tile as tmod
from pandas import to_datetime, DatetimeIndex
from pandas import to_datetime, DatetimeIndex, Timestamp


class TestCut(tm.TestCase):
Expand Down Expand Up @@ -313,6 +313,26 @@ def test_datetime_cut(self):
result, bins = cut(data, 3, retbins=True)
tm.assert_series_equal(Series(result), expected)

def test_datetime_bin(self):
data = [np.datetime64('2012-12-13'), np.datetime64('2012-12-15')]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the issue number as a comment

Copy link
Contributor Author

@aileronajay aileronajay Dec 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback i dont think there is an open issue for this change, @jorisvandenbossche had proposed this change when i was making changes to cut to allow datetime and timedelta data types

bin_data = ['2012-12-12', '2012-12-14', '2012-12-16']
expected = Series(['(2012-12-12 00:00:00, 2012-12-14 00:00:00]',
'(2012-12-14 00:00:00, 2012-12-16 00:00:00]'],
).astype("category", ordered=True)

for conv in [Timestamp, Timestamp, np.datetime64]:
bins = [conv(v) for v in bin_data]
result = cut(data, bins=bins)
tm.assert_series_equal(Series(result), expected)

bin_pydatetime = [Timestamp(v).to_pydatetime() for v in bin_data]
result = cut(data, bins=bin_pydatetime)
tm.assert_series_equal(Series(result), expected)

bins = to_datetime(bin_data)
result = cut(data, bins=bin_pydatetime)
tm.assert_series_equal(Series(result), expected)


def curpath():
pth, _ = os.path.split(os.path.abspath(__file__))
Expand Down
15 changes: 15 additions & 0 deletions pandas/tools/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pandas.compat import zip
from pandas import to_timedelta, to_datetime
from pandas.types.common import is_datetime64_dtype, is_timedelta64_dtype
from pandas.lib import infer_dtype

import numpy as np

Expand Down Expand Up @@ -116,6 +117,7 @@ def cut(x, bins, right=True, labels=None, retbins=False, precision=3,

else:
bins = np.asarray(bins)
bins = _convert_bin_to_numeric_type(bins)
if (np.diff(bins) < 0).any():
raise ValueError('bins must increase monotonically.')

Expand Down Expand Up @@ -327,6 +329,19 @@ def _coerce_to_type(x):
return x, dtype


def _convert_bin_to_numeric_type(x):
"""
if the passed bin is of datetime/timedelta type,
this method converts it to integer
"""
dtype = infer_dtype(x)
if dtype == 'timedelta' or dtype == 'timedelta64':
x = to_timedelta(x).view(np.int64)
elif dtype == 'datetime' or dtype == 'datetime64':
x = to_datetime(x).view(np.int64)
return x


def _preprocess_for_cut(x):
"""
handles preprocessing for cut where we convert passed
Expand Down