Esempio n. 1
0
def test_str_dir():
    # check that Series.str is discoverable with dir()

    # given
    s = bpd.Series(data=['this', 'is', 'a', 'test'])

    # then
    assert 'isupper' in dir(s.str)
    assert 'wrap' in dir(s.str)
    assert '__lt__' not in dir(s.str)
Esempio n. 2
0
def test_negation():
    # given
    s = bpd.Series(data=[1,2,3])

    # when
    t = -s

    # then
    assert t.iloc[0] == -1
    assert t.iloc[1] == -2
    assert t.iloc[2] == -3
Esempio n. 3
0
def test_reverse_subtract():
    # given
    s = bpd.Series(data=[1,2,3])

    # when
    t = 1 - s

    # then
    assert t.iloc[0] == 0
    assert t.iloc[1] == -1
    assert t.iloc[2] == -2
Esempio n. 4
0
def test_reverse_add():
    # given
    s = bpd.Series(data=[1,2,3])

    # when
    t = 1 + s

    # then
    assert t.iloc[0] == 2
    assert t.iloc[1] == 3
    assert t.iloc[2] == 4
Esempio n. 5
0
def test_where():

    # replace with an array
    s = bpd.Series(data=[1, 5, 3, 5, 6])
    t = bpd.Series(data=[0, 5, 2, 5, 4])
    cond = s == 5
    other = np.array([0, 1, 2, 3, 4])
    assert_series_equal(s, t, method='where', cond=cond, other=other)

    # replace with a broadcasted float
    s = bpd.Series(data=[1, 5, 3, 5, 6])
    t = bpd.Series(data=[10, 5, 10, 5, 6])
    cond = s >= 5
    other = 10
    assert_series_equal(s, t, method='where', cond=cond, other=other)

    # throw an error if other is not supplied
    s = bpd.Series(data=[1, 5, 3, 5, 6])
    cond = s == 5
    assert pytest.raises(TypeError, s.where, cond=cond)
Esempio n. 6
0
def test_unary_not():
    # check that ~x works on boolean series

    # given
    s = bpd.Series(data=[True, True, False])

    # when
    result = ~s

    # then
    assert not result.iloc[0]
    assert not result.iloc[1]
    assert result.iloc[2]
Esempio n. 7
0
def test_bitwise_xor():
    # check that bitwise or between two Series works

    # given
    s = bpd.Series(data=[1,2,3,4])

    # when
    result = ((s >= 2) ^ (s <= 2))

    # then
    assert result.iloc[0]
    assert not result.iloc[1]
    assert result.iloc[2]
    assert result.iloc[3]
Esempio n. 8
0
def sers():
    inputs = []
    # ser1 input: Sorted Series
    inputs.append({'data': [1, 2, 3, 4, 5]})
    # ser2 input: String Series
    inputs.append({'data': ['a', 'b', 'c', 'd']})
    # ser3 input: Unsorted Series
    inputs.append({'data': [7, 1, 3, 4, 2]})

    dct = {}
    for key in range(len(inputs)):
        dct['ser{}'.format(key + 1)] = (bpd.Series(**inputs[key]),
                                        pd.Series(**inputs[key]))
    return dct
Esempio n. 9
0
def test_str():
    # check that we can use Series.str methods

    # given
    s = bpd.Series(data=['this', 'is', 'a', 'test'])

    # when
    result = s.str.upper()

    # then
    assert isinstance(result, bpd.Series)
    assert result.iloc[0] == 'THIS'
    assert result.iloc[1] == 'IS'
    assert result.iloc[2] == 'A'
    assert result.iloc[3] == 'TEST'
Esempio n. 10
0
def test_indexing():
    # Check that boolean indexing works as expected.
    s = bpd.Series(data=[1, 5, 3, 5, 6])
    n = len(s)
    s_is_5 = s == 5
    # Simple indexing cases, Series, and array.
    for this_ind in (s_is_5, np.array(s_is_5)):
        indexed = s[this_ind]
        assert len(indexed) == np.count_nonzero(this_ind)
        assert list(indexed.index) == list(np.arange(n)[this_ind])
    # Sort Series index, and get the same output (because it depends on the
    # index).
    sorted_indexer = s_is_5.sort_values()
    indexed = s[sorted_indexer]
    assert len(indexed) == 2
    assert list(indexed.index) == [1, 3]
    # Any other type of indexing generates an error
    for indexer in (2, slice(1, 3)):
        with pytest.raises(IndexError):
            s[indexer]
Esempio n. 11
0
def test_get(sers):
    
    ser1 = bpd.Series(data=[1, 2, 3, 4])
    assert ser1.get(key=2) == 3