-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pytest test cases for Arrow support
- Loading branch information
Zhang Zhang
committed
Jun 17, 2020
1 parent
dbefba8
commit 2bcf1b9
Showing
3 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# -*- coding: utf-8 -*- | ||
import unittest | ||
import pytest | ||
import numpy as np | ||
|
||
import testing as tm | ||
import xgboost as xgb | ||
|
||
try: | ||
import pyarrow as pa | ||
import pyarrow.csv as pc | ||
import pandas as pd | ||
except ImportError: | ||
pass | ||
|
||
pytestmark = pytest.mark.skipif( | ||
tm.no_arrow()['condition'] or tm.no_pandas()['condition'], | ||
reason=tm.no_arrow()['reason'] + ' or ' + tm.no_pandas()['reason']) | ||
|
||
dpath = 'demo/data/' | ||
|
||
class TestArrowTable(unittest.TestCase): | ||
|
||
def test_arrow_table(self): | ||
df = pd.DataFrame([[0, 1, 2., 3.], [1, 2, 3., 4.]], | ||
columns=['a', 'b', 'c', 'd']) | ||
table = pa.Table.from_pandas(df) | ||
dm = xgb.DMatrix(table) | ||
assert dm.num_row() == 2 | ||
assert dm.num_col() == 4 | ||
|
||
def test_arrow_table_with_label(self): | ||
df = pd.DataFrame([[0, 1, 2., 3.], [1, 2, 3., 4.]], | ||
columns=['label', 'a', 'b', 'c']) | ||
table = pa.Table.from_pandas(df) | ||
dm = xgb.DMatrix(table, label='label') | ||
assert dm.num_row() == 2 | ||
assert dm.num_col() == 3 | ||
np.testing.assert_array_equal(dm.get_label(), np.array([0, 1])) | ||
|
||
def test_arrow_table_from_np(self): | ||
coldata = np.array([[1., 1., 0., 0.], | ||
[2., 0., 1., 0.], | ||
[3., 0., 0., 1.]]) | ||
cols = list(map(pa.array, coldata)) | ||
table = pa.Table.from_arrays(cols, ['a', 'b', 'c']) | ||
dm = xgb.DMatrix(table) | ||
assert dm.num_row() == 4 | ||
assert dm.num_col() == 3 | ||
|
||
def test_arrow_table_from_csv(self): | ||
dfile = dpath + 'veterans_lung_cancer.csv' | ||
table = pc.read_csv(dfile) | ||
dm = xgb.DMatrix(table) | ||
assert dm.num_row() == 137 | ||
assert dm.num_col() == 13 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters