Skip to content

Commit 5713fcd

Browse files
committed
added docstring to new methods. Merged all datetime tests into one test, updated whatsnew
1 parent 92bdabc commit 5713fcd

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

doc/source/whatsnew/v0.20.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Other enhancements
5050

5151
- ``pd.read_excel`` now preserves sheet order when using ``sheetname=None`` (:issue:`9930`)
5252

53+
ENH: support cut/qcut for datetime/timedelta (GH14714)
5354

5455
.. _whatsnew_0200.api_breaking:
5556

pandas/tools/tests/test_tile.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ def test_single_bin(self):
286286

287287
def test_datetime_cut(self):
288288
# GH 14714
289+
# testing for time data to be present as series
289290
data = to_datetime(Series(['2013-01-01', '2013-01-02', '2013-01-03']))
290291
result, bins = cut(data, 3, retbins=True)
291292
expected = Series(['(2012-12-31 23:57:07.200000, 2013-01-01 16:00:00]',
@@ -294,37 +295,23 @@ def test_datetime_cut(self):
294295
).astype("category", ordered=True)
295296
tm.assert_series_equal(result, expected)
296297

297-
def test_datetime_list_cut(self):
298-
# GH 14714
298+
# testing for time data to be present as list
299299
data = [np.datetime64('2013-01-01'), np.datetime64('2013-01-02'),
300300
np.datetime64('2013-01-03')]
301301
result, bins = cut(data, 3, retbins=True)
302-
expected = Series(['(2012-12-31 23:57:07.200000, 2013-01-01 16:00:00]',
303-
'(2013-01-01 16:00:00, 2013-01-02 08:00:00]',
304-
'(2013-01-02 08:00:00, 2013-01-03 00:00:00]'],
305-
).astype("category", ordered=True)
306302
tm.assert_series_equal(Series(result), expected)
307303

308-
def test_datetime_ndarray_cut(self):
309-
# GH 14714
304+
# testing for time data to be present as ndarray
305+
310306
data = np.array([np.datetime64('2013-01-01'),
311307
np.datetime64('2013-01-02'),
312308
np.datetime64('2013-01-03')])
313309
result, bins = cut(data, 3, retbins=True)
314-
expected = Series(['(2012-12-31 23:57:07.200000, 2013-01-01 16:00:00]',
315-
'(2013-01-01 16:00:00, 2013-01-02 08:00:00]',
316-
'(2013-01-02 08:00:00, 2013-01-03 00:00:00]'],
317-
).astype("category", ordered=True)
318310
tm.assert_series_equal(Series(result), expected)
319311

320-
def test_datetime_index_cut(self):
321-
# GH 14714
312+
# testing for time data to be present as datetime index
322313
data = DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03'])
323314
result, bins = cut(data, 3, retbins=True)
324-
expected = Series(['(2012-12-31 23:57:07.200000, 2013-01-01 16:00:00]',
325-
'(2013-01-01 16:00:00, 2013-01-02 08:00:00]',
326-
'(2013-01-02 08:00:00, 2013-01-03 00:00:00]'],
327-
).astype("category", ordered=True)
328315
tm.assert_series_equal(Series(result), expected)
329316

330317

pandas/tools/tile.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,11 @@ def _trim_zeros(x):
311311

312312

313313
def _coerce_to_type(x):
314+
'''
315+
if the passed data is of datetime/timedelta type,
316+
this method converts it to integer so that cut method can
317+
handle it
318+
'''
314319
dtype = None
315320
original = x
316321
if is_timedelta64_dtype(x):
@@ -324,6 +329,11 @@ def _coerce_to_type(x):
324329

325330

326331
def _preprocess_for_cut(x):
332+
'''
333+
handles preprocessing for cut where we convert passed
334+
input to array, strip the index information and store it
335+
seperately
336+
'''
327337
x_is_series = isinstance(x, Series)
328338
series_index = None
329339

@@ -339,6 +349,11 @@ def _preprocess_for_cut(x):
339349

340350

341351
def _postprocess_for_cut(fac, bins, retbins, x_is_series, series_index, name):
352+
'''
353+
handles post processing for the cut method where
354+
we combine the index information if the originally passed
355+
datatype was a series
356+
'''
342357
if x_is_series:
343358
fac = Series(fac, index=series_index, name=name)
344359

0 commit comments

Comments
 (0)