def test_categorical_compare_ordered(): cat1 = pd.Categorical(['a', 'a', 'b', 'c', 'a'], categories=['a', 'b', 'c'], ordered=True) pdsr1 = pd.Series(cat1) sr1 = Series.from_any(cat1) cat2 = pd.Categorical(['a', 'b', 'a', 'c', 'b'], categories=['a', 'b', 'c'], ordered=True) pdsr2 = pd.Series(cat2) sr2 = Series.from_any(cat2) # test equal out = sr1 == sr1 assert out.dtype == np.bool_ assert type(out[0]) == np.bool_ assert np.all(out) assert np.all(pdsr1 == pdsr1) # test inequal out = sr1 != sr1 assert not np.any(out) assert not np.any(pdsr1 != pdsr1) assert pdsr1.cat.ordered assert sr1.cat.ordered # test using ordered operators np.testing.assert_array_equal(pdsr1 < pdsr2, sr1 < sr2) np.testing.assert_array_equal(pdsr1 > pdsr2, sr1 > sr2)
def test_series_compare(cmpop): arr1 = np.random.random(100) arr2 = np.random.random(100) sr1 = Series.from_any(arr1) sr2 = Series.from_any(arr2) np.testing.assert_equal(cmpop(sr1, sr1).to_array(), cmpop(arr1, arr1)) np.testing.assert_equal(cmpop(sr2, sr2).to_array(), cmpop(arr2, arr2)) np.testing.assert_equal(cmpop(sr1, sr2).to_array(), cmpop(arr1, arr2))
def test_series_unique(): for size in [10**x for x in range(5)]: arr = np.random.randint(low=0, high=10, size=size) sr = Series.from_any(arr) assert set(arr) == set(sr.unique_k(k=10)) # test out of space arr = np.arange(10) sr = Series.from_any(arr) with pytest.raises(ValueError) as raises: sr.unique_k(k=7) raises.match('too many unique value')
def test_categorical_compare_unordered(): cat = pd.Categorical(['a', 'a', 'b', 'c', 'a'], categories=['a', 'b', 'c']) pdsr = pd.Series(cat) sr = Series.from_any(cat) # test equal out = sr == sr assert out.dtype == np.bool_ assert type(out[0]) == np.bool_ assert np.all(out) assert np.all(pdsr == pdsr) # test inequal out = sr != sr assert not np.any(out) assert not np.any(pdsr != pdsr) assert not pdsr.cat.ordered assert not sr.cat.ordered # test using ordered operators with pytest.raises(TypeError) as raises: pdsr < pdsr raises.match("Unordered Categoricals can only compare equality or not") with pytest.raises(TypeError) as raises: sr < sr raises.match("Unordered Categoricals can only compare equality or not")
def test_series_std(dtype): arr = np.random.random(100) if issubclass(arr.dtype.type, np.integer): arr *= 100 arr = arr.astype(dtype) sr = Series.from_any(arr) np.testing.assert_almost_equal(arr.std(), sr.std())
def test_series_scale(): arr = np.random.randint(low=-10, high=10, size=100) sr = Series.from_any(arr) vmin = arr.min() vmax = arr.max() scaled = (arr - vmin) / (vmax - vmin) assert scaled.min() == 0 assert scaled.max() == 1 np.testing.assert_equal(sr.scale().to_array(), scaled)
def test_categorical_binary_add(): cat = pd.Categorical(['a', 'a', 'b', 'c', 'a'], categories=['a', 'b', 'c']) pdsr = pd.Series(cat) sr = Series.from_any(cat) with pytest.raises(TypeError) as raises: pdsr + pdsr raises.match('Categorical cannot perform the operation \+') with pytest.raises(TypeError) as raises: sr + sr raises.match('Categorical cannot perform the operation: add')
def test_series_indexing(): a1 = np.arange(20) series = Series.from_any(a1) # Indexing sr1 = series[:12] assert not sr1.has_null_mask np.testing.assert_equal(sr1.to_array(), a1[:12]) sr2 = sr1[3:] assert not sr2.has_null_mask np.testing.assert_equal(sr2.to_array(), a1[3:12]) # Index with stride sr3 = sr2[::2] assert not sr3.has_null_mask np.testing.assert_equal(sr3.to_array(), a1[3:12:2])
def test_series_basic(): # Make series from buffer a1 = np.arange(10, dtype=np.float64) series = Series.from_any(a1) assert len(series) == 10 np.testing.assert_equal(series.to_array(), np.hstack([a1])) # Add new buffer a2 = np.arange(5) series = series.append(a2) assert len(series) == 15 np.testing.assert_equal(series.to_array(), np.hstack([a1, a2])) # Ensure appending to previous buffer a3 = np.arange(3) series = series.append(a3) assert len(series) == 18 a4 = np.hstack([a1, a2, a3]) np.testing.assert_equal(series.to_array(), a4)
def test_categorical_missing(): cat = pd.Categorical(['a', '_', '_', 'c', 'a'], categories=['a', 'b', 'c']) pdsr = pd.Series(cat) sr = Series.from_any(cat) np.testing.assert_array_equal(cat.codes, sr.to_array(fillna='pandas')) assert sr.null_count == 2 np.testing.assert_array_equal(pdsr.cat.codes.data, sr.cat.codes.to_array(fillna='pandas')) np.testing.assert_array_equal(pdsr.cat.codes.dtype, sr.cat.codes.dtype) string = str(sr) expect_str = """ 0 a 1 2 3 c 4 a """ assert string.split() == expect_str.split()
def test_series_ceil(): arr = np.random.random(100) sr = Series.from_any(arr) np.testing.assert_equal(sr.ceil().to_array(), np.ceil(arr))
def test_series_floor(): arr = np.random.random(100) * 100 sr = Series.from_any(arr) np.testing.assert_equal(sr.floor().to_array(), np.floor(arr))
def test_series_min(): arr = np.random.random(100) sr = Series.from_any(arr) np.testing.assert_almost_equal(arr.min(), sr.min())
def test_series_compare_scalar(cmpop): arr1 = np.random.random(100) sr1 = Series.from_any(arr1) rhs = np.asscalar(random.choice(arr1)) np.testing.assert_equal(cmpop(sr1, rhs).to_array(), cmpop(arr1, rhs)) np.testing.assert_equal(cmpop(rhs, sr1).to_array(), cmpop(rhs, arr1))
def test_series_binop(binop): arr = np.random.random(100) sr = Series.from_any(arr) np.testing.assert_equal(binop(sr, sr).to_array(), binop(arr, arr))