Skip to content

Commit

Permalink
add additional test
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke committed Mar 3, 2018
1 parent e691329 commit e2772f7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pandas/core/reshape/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def _convert_bin_to_numeric_type(bins, dtype):
Parameters
----------
bins : list-liek of bins
bins : list-like of bins
dtype : dtype of data
Raises
Expand Down
84 changes: 49 additions & 35 deletions pandas/tests/reshape/test_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,10 @@ def test_cut_corner(self):
pytest.raises(ValueError, cut, [1, 2, 3], 0.5)

@pytest.mark.parametrize('arg', [2, np.eye(2), DataFrame(np.eye(2))])
@pytest.mark.parametrize('bins', [2, np.array(2), np.arange(1, 2), [1, 2],
Series([1, 2]), Index([1, 2])])
@pytest.mark.parametrize('cut_func', [cut, qcut])
def test_cut_not_1d_arg(self, arg, bins, cut_func):
def test_cut_not_1d_arg(self, arg, cut_func):
with pytest.raises(ValueError):
cut_func(arg, bins)
cut_func(arg, 2)

def test_cut_out_of_range_more(self):
# #1511
Expand Down Expand Up @@ -259,18 +257,6 @@ def test_qcut_nas(self):
result = qcut(arr, 4)
assert isna(result[:20]).all()

@pytest.mark.parametrize('s', [
Series(DatetimeIndex(['20180101', NaT, '20180103'])),
Series(TimedeltaIndex(['0 days', NaT, '2 days']))],
ids=lambda x: str(x.dtype))
def test_qcut_nat(self, s):
# GH 19768
intervals = IntervalIndex.from_tuples(
[(s[0] - Nano(), s[2] - Day()), np.nan, (s[2] - Day(), s[2])])
expected = Series(Categorical(intervals, ordered=True))
result = qcut(s, 2)
tm.assert_series_equal(result, expected)

def test_qcut_index(self):
result = qcut([0, 2], 2)
intervals = [Interval(-0.001, 1), Interval(1, 2)]
Expand Down Expand Up @@ -460,6 +446,37 @@ def test_single_bin(self):
result = cut(s, 1, labels=False)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
"array_1_writeable, array_2_writeable",
[(True, True), (True, False), (False, False)])
def test_cut_read_only(self, array_1_writeable, array_2_writeable):
# issue 18773
array_1 = np.arange(0, 100, 10)
array_1.flags.writeable = array_1_writeable

array_2 = np.arange(0, 100, 10)
array_2.flags.writeable = array_2_writeable

hundred_elements = np.arange(100)

tm.assert_categorical_equal(cut(hundred_elements, array_1),
cut(hundred_elements, array_2))


class TestDatelike(object):

@pytest.mark.parametrize('s', [
Series(DatetimeIndex(['20180101', NaT, '20180103'])),
Series(TimedeltaIndex(['0 days', NaT, '2 days']))],
ids=lambda x: str(x.dtype))
def test_qcut_nat(self, s):
# GH 19768
intervals = IntervalIndex.from_tuples(
[(s[0] - Nano(), s[2] - Day()), np.nan, (s[2] - Day(), s[2])])
expected = Series(Categorical(intervals, ordered=True))
result = qcut(s, 2)
tm.assert_series_equal(result, expected)

def test_datetime_cut(self):
# GH 14714
# testing for time data to be present as series
Expand Down Expand Up @@ -496,11 +513,19 @@ def test_datetime_cut(self):
result, bins = cut(data, 3, retbins=True)
tm.assert_series_equal(Series(result), expected)

def test_datetimetz_cut(self):
@pytest.mark.parametrize('bins', [
3, [Timestamp('2013-01-01 04:57:07.200000').value,
Timestamp('2013-01-01 21:00:00').value,
Timestamp('2013-01-02 13:00:00').value,
Timestamp('2013-01-03 05:00:00').value]])
@pytest.mark.parametrize('const', [list, np.array, Index, Series])
def test_datetimetz_cut(self, bins, const):
# GH 19872
tz = 'US/Eastern'
s = Series(date_range('20130101', periods=3, tz=tz))
result = cut(s, 3)
if not isinstance(bins, int):
bins = const(bins)
result = cut(s, bins)
expected = (
Series(IntervalIndex([
Interval(Timestamp('2012-12-31 23:57:07.200000', tz=tz),
Expand All @@ -512,7 +537,12 @@ def test_datetimetz_cut(self):
.astype(CDT(ordered=True)))
tm.assert_series_equal(result, expected)

result = qcut(s, 3)
@pytest.mark.parametrize('bins', [3, np.linspace(0, 1, 4)])
def test_datetimetz_qcut(self, bins):
# GH 19872
tz = 'US/Eastern'
s = Series(date_range('20130101', periods=3, tz=tz))
result = qcut(s, bins)
expected = (
Series(IntervalIndex([
Interval(Timestamp('2012-12-31 23:59:59.999999999', tz=tz),
Expand Down Expand Up @@ -559,19 +589,3 @@ def f():
mask = result.isna()
tm.assert_numpy_array_equal(
mask, np.array([False, True, True, True, True]))

@pytest.mark.parametrize(
"array_1_writeable, array_2_writeable",
[(True, True), (True, False), (False, False)])
def test_cut_read_only(self, array_1_writeable, array_2_writeable):
# issue 18773
array_1 = np.arange(0, 100, 10)
array_1.flags.writeable = array_1_writeable

array_2 = np.arange(0, 100, 10)
array_2.flags.writeable = array_2_writeable

hundred_elements = np.arange(100)

tm.assert_categorical_equal(cut(hundred_elements, array_1),
cut(hundred_elements, array_2))

0 comments on commit e2772f7

Please sign in to comment.