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)
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
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
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
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)
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]
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]
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
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'
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]
def test_get(sers): ser1 = bpd.Series(data=[1, 2, 3, 4]) assert ser1.get(key=2) == 3