|
4 | 4 | import numpy as np
|
5 | 5 | from pandas.compat import zip
|
6 | 6 |
|
7 |
| -from pandas import (Series, isna, to_datetime, DatetimeIndex, |
| 7 | +from pandas import (DataFrame, Series, isna, to_datetime, DatetimeIndex, Index, |
8 | 8 | Timestamp, Interval, IntervalIndex, Categorical,
|
9 | 9 | cut, qcut, date_range, NaT, TimedeltaIndex)
|
10 | 10 | from pandas.tseries.offsets import Nano, Day
|
@@ -104,6 +104,12 @@ def test_cut_corner(self):
|
104 | 104 |
|
105 | 105 | pytest.raises(ValueError, cut, [1, 2, 3], 0.5)
|
106 | 106 |
|
| 107 | + @pytest.mark.parametrize('arg', [2, np.eye(2), DataFrame(np.eye(2))]) |
| 108 | + @pytest.mark.parametrize('cut_func', [cut, qcut]) |
| 109 | + def test_cut_not_1d_arg(self, arg, cut_func): |
| 110 | + with pytest.raises(ValueError): |
| 111 | + cut_func(arg, 2) |
| 112 | + |
107 | 113 | def test_cut_out_of_range_more(self):
|
108 | 114 | # #1511
|
109 | 115 | s = Series([0, -1, 0, 1, -3], name='x')
|
@@ -251,18 +257,6 @@ def test_qcut_nas(self):
|
251 | 257 | result = qcut(arr, 4)
|
252 | 258 | assert isna(result[:20]).all()
|
253 | 259 |
|
254 |
| - @pytest.mark.parametrize('s', [ |
255 |
| - Series(DatetimeIndex(['20180101', NaT, '20180103'])), |
256 |
| - Series(TimedeltaIndex(['0 days', NaT, '2 days']))], |
257 |
| - ids=lambda x: str(x.dtype)) |
258 |
| - def test_qcut_nat(self, s): |
259 |
| - # GH 19768 |
260 |
| - intervals = IntervalIndex.from_tuples( |
261 |
| - [(s[0] - Nano(), s[2] - Day()), np.nan, (s[2] - Day(), s[2])]) |
262 |
| - expected = Series(Categorical(intervals, ordered=True)) |
263 |
| - result = qcut(s, 2) |
264 |
| - tm.assert_series_equal(result, expected) |
265 |
| - |
266 | 260 | def test_qcut_index(self):
|
267 | 261 | result = qcut([0, 2], 2)
|
268 | 262 | intervals = [Interval(-0.001, 1), Interval(1, 2)]
|
@@ -452,6 +446,37 @@ def test_single_bin(self):
|
452 | 446 | result = cut(s, 1, labels=False)
|
453 | 447 | tm.assert_series_equal(result, expected)
|
454 | 448 |
|
| 449 | + @pytest.mark.parametrize( |
| 450 | + "array_1_writeable, array_2_writeable", |
| 451 | + [(True, True), (True, False), (False, False)]) |
| 452 | + def test_cut_read_only(self, array_1_writeable, array_2_writeable): |
| 453 | + # issue 18773 |
| 454 | + array_1 = np.arange(0, 100, 10) |
| 455 | + array_1.flags.writeable = array_1_writeable |
| 456 | + |
| 457 | + array_2 = np.arange(0, 100, 10) |
| 458 | + array_2.flags.writeable = array_2_writeable |
| 459 | + |
| 460 | + hundred_elements = np.arange(100) |
| 461 | + |
| 462 | + tm.assert_categorical_equal(cut(hundred_elements, array_1), |
| 463 | + cut(hundred_elements, array_2)) |
| 464 | + |
| 465 | + |
| 466 | +class TestDatelike(object): |
| 467 | + |
| 468 | + @pytest.mark.parametrize('s', [ |
| 469 | + Series(DatetimeIndex(['20180101', NaT, '20180103'])), |
| 470 | + Series(TimedeltaIndex(['0 days', NaT, '2 days']))], |
| 471 | + ids=lambda x: str(x.dtype)) |
| 472 | + def test_qcut_nat(self, s): |
| 473 | + # GH 19768 |
| 474 | + intervals = IntervalIndex.from_tuples( |
| 475 | + [(s[0] - Nano(), s[2] - Day()), np.nan, (s[2] - Day(), s[2])]) |
| 476 | + expected = Series(Categorical(intervals, ordered=True)) |
| 477 | + result = qcut(s, 2) |
| 478 | + tm.assert_series_equal(result, expected) |
| 479 | + |
455 | 480 | def test_datetime_cut(self):
|
456 | 481 | # GH 14714
|
457 | 482 | # testing for time data to be present as series
|
@@ -488,6 +513,47 @@ def test_datetime_cut(self):
|
488 | 513 | result, bins = cut(data, 3, retbins=True)
|
489 | 514 | tm.assert_series_equal(Series(result), expected)
|
490 | 515 |
|
| 516 | + @pytest.mark.parametrize('bins', [ |
| 517 | + 3, [Timestamp('2013-01-01 04:57:07.200000'), |
| 518 | + Timestamp('2013-01-01 21:00:00'), |
| 519 | + Timestamp('2013-01-02 13:00:00'), |
| 520 | + Timestamp('2013-01-03 05:00:00')]]) |
| 521 | + @pytest.mark.parametrize('box', [list, np.array, Index, Series]) |
| 522 | + def test_datetimetz_cut(self, bins, box): |
| 523 | + # GH 19872 |
| 524 | + tz = 'US/Eastern' |
| 525 | + s = Series(date_range('20130101', periods=3, tz=tz)) |
| 526 | + if not isinstance(bins, int): |
| 527 | + bins = box(bins) |
| 528 | + result = cut(s, bins) |
| 529 | + expected = ( |
| 530 | + Series(IntervalIndex([ |
| 531 | + Interval(Timestamp('2012-12-31 23:57:07.200000', tz=tz), |
| 532 | + Timestamp('2013-01-01 16:00:00', tz=tz)), |
| 533 | + Interval(Timestamp('2013-01-01 16:00:00', tz=tz), |
| 534 | + Timestamp('2013-01-02 08:00:00', tz=tz)), |
| 535 | + Interval(Timestamp('2013-01-02 08:00:00', tz=tz), |
| 536 | + Timestamp('2013-01-03 00:00:00', tz=tz))])) |
| 537 | + .astype(CDT(ordered=True))) |
| 538 | + tm.assert_series_equal(result, expected) |
| 539 | + |
| 540 | + @pytest.mark.parametrize('bins', [3, np.linspace(0, 1, 4)]) |
| 541 | + def test_datetimetz_qcut(self, bins): |
| 542 | + # GH 19872 |
| 543 | + tz = 'US/Eastern' |
| 544 | + s = Series(date_range('20130101', periods=3, tz=tz)) |
| 545 | + result = qcut(s, bins) |
| 546 | + expected = ( |
| 547 | + Series(IntervalIndex([ |
| 548 | + Interval(Timestamp('2012-12-31 23:59:59.999999999', tz=tz), |
| 549 | + Timestamp('2013-01-01 16:00:00', tz=tz)), |
| 550 | + Interval(Timestamp('2013-01-01 16:00:00', tz=tz), |
| 551 | + Timestamp('2013-01-02 08:00:00', tz=tz)), |
| 552 | + Interval(Timestamp('2013-01-02 08:00:00', tz=tz), |
| 553 | + Timestamp('2013-01-03 00:00:00', tz=tz))])) |
| 554 | + .astype(CDT(ordered=True))) |
| 555 | + tm.assert_series_equal(result, expected) |
| 556 | + |
491 | 557 | def test_datetime_bin(self):
|
492 | 558 | data = [np.datetime64('2012-12-13'), np.datetime64('2012-12-15')]
|
493 | 559 | bin_data = ['2012-12-12', '2012-12-14', '2012-12-16']
|
@@ -523,19 +589,3 @@ def f():
|
523 | 589 | mask = result.isna()
|
524 | 590 | tm.assert_numpy_array_equal(
|
525 | 591 | mask, np.array([False, True, True, True, True]))
|
526 |
| - |
527 |
| - @pytest.mark.parametrize( |
528 |
| - "array_1_writeable, array_2_writeable", |
529 |
| - [(True, True), (True, False), (False, False)]) |
530 |
| - def test_cut_read_only(self, array_1_writeable, array_2_writeable): |
531 |
| - # issue 18773 |
532 |
| - array_1 = np.arange(0, 100, 10) |
533 |
| - array_1.flags.writeable = array_1_writeable |
534 |
| - |
535 |
| - array_2 = np.arange(0, 100, 10) |
536 |
| - array_2.flags.writeable = array_2_writeable |
537 |
| - |
538 |
| - hundred_elements = np.arange(100) |
539 |
| - |
540 |
| - tm.assert_categorical_equal(cut(hundred_elements, array_1), |
541 |
| - cut(hundred_elements, array_2)) |
|
0 commit comments