def test_series_argsort(nelem, dtype, asc): np.random.seed(0) sr = Series((100 * np.random.random(nelem)).astype(dtype)) res = sr.argsort(ascending=asc) if asc: expected = np.argsort(sr.to_array(), kind='mergesort') else: expected = np.argsort(-sr.to_array(), kind='mergesort') np.testing.assert_array_equal(expected, res.to_array())
def test_categorical_missing(): cat = pd.Categorical(['a', '_', '_', 'c', 'a'], categories=['a', 'b', 'c']) pdsr = pd.Series(cat) sr = Series(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.fillna(-1).to_array()) np.testing.assert_equal(pdsr.cat.codes.dtype, sr.cat.codes.dtype) # integer doesn't have nan value with pytest.raises(TypeError) as raises: sr.cat.codes.to_array(fillna='pandas') raises.match('NaN value') string = str(sr) expect_str = """ 0 a 1 2 3 c 4 a """ assert string.split() == expect_str.split()
def test_series_sort_index(nelem, asc): np.random.seed(0) sr = Series((100 * np.random.random(nelem))) orig = sr.to_array() got = sr.sort_values().sort_index(ascending=asc).to_array() if not asc: # Reverse the array for descending sort got = got[::-1] np.testing.assert_array_equal(orig, got)
def test_series_basic(): # Make series from buffer a1 = np.arange(10, dtype=np.float64) series = Series(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_empty(): cat = pd.Categorical([]) pdsr = pd.Series(cat) sr = Series(cat) np.testing.assert_array_equal(cat.codes, sr.to_array()) assert sr.dtype == pdsr.dtype # Test attributes assert tuple(pdsr.cat.categories) == tuple(sr.cat.categories) assert pdsr.cat.ordered == sr.cat.ordered np.testing.assert_array_equal(pdsr.cat.codes.data, sr.cat.codes.to_array()) np.testing.assert_array_equal(pdsr.cat.codes.dtype, sr.cat.codes.dtype)
def test_categorical_integer(): cat = pd.Categorical(['a', '_', '_', 'c', 'a'], categories=['a', 'b', 'c']) pdsr = pd.Series(cat) sr = Series(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.fillna(-1).to_array()) np.testing.assert_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_categorical_missing(): cat = pd.Categorical(['a', '_', '_', 'c', 'a'], categories=['a', 'b', 'c']) pdsr = pd.Series(cat) sr = Series(cat) fillna = lambda x: np.where(np.isnan(x), -1, x) np.testing.assert_array_equal(cat.codes, fillna(sr.to_array(fillna='pandas'))) assert sr.null_count == 2 np.testing.assert_array_equal(pdsr.cat.codes.data, fillna(sr.cat.codes.to_array(fillna='pandas'))) np.testing.assert_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_categorical_basic(): cat = pd.Categorical(['a', 'a', 'b', 'c', 'a'], categories=['a', 'b', 'c']) pdsr = pd.Series(cat) sr = Series(cat) np.testing.assert_array_equal(cat.codes, sr.to_array()) assert sr.dtype == pdsr.dtype # Test attributes assert tuple(pdsr.cat.categories) == tuple(sr.cat.categories) assert pdsr.cat.ordered == sr.cat.ordered np.testing.assert_array_equal(pdsr.cat.codes.data, sr.cat.codes.to_array()) np.testing.assert_array_equal(pdsr.cat.codes.dtype, sr.cat.codes.dtype) string = str(sr) expect_str = """ 0 a 1 a 2 b 3 c 4 a """ assert all(x == y for x, y in zip(string.split(), expect_str.split()))