Skip to content

Commit 82bffa1

Browse files
committed
added method for time type bins in pd cut and modified tests
1 parent ac919cf commit 82bffa1

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

pandas/tools/tests/test_tile.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pandas.core.algorithms import quantile
1313
from pandas.tools.tile import cut, qcut
1414
import pandas.tools.tile as tmod
15-
from pandas import to_datetime, DatetimeIndex
15+
from pandas import to_datetime, DatetimeIndex, Timestamp
1616

1717

1818
class TestCut(tm.TestCase):
@@ -315,14 +315,22 @@ def test_datetime_cut(self):
315315

316316
def test_datetime_bin(self):
317317
data = [np.datetime64('2012-12-13'), np.datetime64('2012-12-15')]
318-
bins = [np.datetime64('2012-12-12'), np.datetime64('2012-12-14'),
319-
np.datetime64('2012-12-16')]
320-
result = cut(data, bins=bins)
321-
318+
bin_data = ['2012-12-12', '2012-12-14', '2012-12-16']
322319
expected = Series(['(2012-12-12 00:00:00, 2012-12-14 00:00:00]',
323-
'(2012-12-14 00:00:00, 2012-12-16 00:00:00]'],
320+
'(2012-12-14 00:00:00, 2012-12-16 00:00:00]'],
324321
).astype("category", ordered=True)
325322

323+
for conv in [Timestamp, Timestamp, np.datetime64]:
324+
bins = [conv(v) for v in bin_data]
325+
result = cut(data, bins=bins)
326+
tm.assert_series_equal(Series(result), expected)
327+
328+
bin_pydatetime = [Timestamp(v).to_pydatetime() for v in bin_data]
329+
result = cut(data, bins=bin_pydatetime)
330+
tm.assert_series_equal(Series(result), expected)
331+
332+
bins = to_datetime(bin_data)
333+
result = cut(data, bins=bin_pydatetime)
326334
tm.assert_series_equal(Series(result), expected)
327335

328336

pandas/tools/tile.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pandas.compat import zip
1414
from pandas import to_timedelta, to_datetime
1515
from pandas.types.common import is_datetime64_dtype, is_timedelta64_dtype
16+
from pandas.lib import infer_dtype
1617

1718
import numpy as np
1819

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

117118
else:
118119
bins = np.asarray(bins)
119-
bins, bin_dtype = _coerce_to_type(bins)
120+
bins = _convert_bin_to_numeric_type(bins)
120121
if (np.diff(bins) < 0).any():
121122
raise ValueError('bins must increase monotonically.')
122123

@@ -328,6 +329,19 @@ def _coerce_to_type(x):
328329
return x, dtype
329330

330331

332+
def _convert_bin_to_numeric_type(x):
333+
"""
334+
if the passed bin is of datetime/timedelta type,
335+
this method converts it to integer
336+
"""
337+
dtype = infer_dtype(x)
338+
if dtype == 'timedelta' or dtype == 'timedelta64':
339+
x = to_timedelta(x).view(np.int64)
340+
elif dtype == 'datetime' or dtype == 'datetime64':
341+
x = to_datetime(x).view(np.int64)
342+
return x
343+
344+
331345
def _preprocess_for_cut(x):
332346
"""
333347
handles preprocessing for cut where we convert passed

0 commit comments

Comments
 (0)