1515# specific language governing permissions and limitations
1616# under the License.
1717
18- import pyarrow as A
18+ import numpy as np
19+
20+ from pandas .util .testing import assert_frame_equal
21+ import pandas as pd
22+
23+ import pyarrow as pa
1924
2025
2126def test_recordbatch_basics ():
2227 data = [
23- A .from_pylist (range (5 )),
24- A .from_pylist ([- 10 , - 5 , 0 , 5 , 10 ])
28+ pa .from_pylist (range (5 )),
29+ pa .from_pylist ([- 10 , - 5 , 0 , 5 , 10 ])
2530 ]
2631
27- batch = A .RecordBatch .from_arrays (['c0' , 'c1' ], data )
32+ batch = pa .RecordBatch .from_arrays (['c0' , 'c1' ], data )
2833
2934 assert len (batch ) == 5
3035 assert batch .num_rows == 5
3136 assert batch .num_columns == len (data )
3237
3338
39+ def test_recordbatch_from_to_pandas ():
40+ data = pd .DataFrame ({
41+ 'c1' : np .array ([1 , 2 , 3 , 4 , 5 ], dtype = 'int64' ),
42+ 'c2' : np .array ([1 , 2 , 3 , 4 , 5 ], dtype = 'uint32' ),
43+ 'c2' : np .random .randn (5 ),
44+ 'c3' : ['foo' , 'bar' , None , 'baz' , 'qux' ],
45+ 'c4' : [False , True , False , True , False ]
46+ })
47+
48+ batch = pa .RecordBatch .from_pandas (data )
49+ result = batch .to_pandas ()
50+ assert_frame_equal (data , result )
51+
52+
3453def test_table_basics ():
3554 data = [
36- A .from_pylist (range (5 )),
37- A .from_pylist ([- 10 , - 5 , 0 , 5 , 10 ])
55+ pa .from_pylist (range (5 )),
56+ pa .from_pylist ([- 10 , - 5 , 0 , 5 , 10 ])
3857 ]
39- table = A .Table .from_arrays (('a' , 'b' ), data , 'table_name' )
58+ table = pa .Table .from_arrays (('a' , 'b' ), data , 'table_name' )
4059 assert table .name == 'table_name'
4160 assert len (table ) == 5
4261 assert table .num_rows == 5
@@ -50,15 +69,15 @@ def test_table_basics():
5069
5170def test_table_pandas ():
5271 data = [
53- A .from_pylist (range (5 )),
54- A .from_pylist ([- 10 , - 5 , 0 , 5 , 10 ])
72+ pa .from_pylist (range (5 )),
73+ pa .from_pylist ([- 10 , - 5 , 0 , 5 , 10 ])
5574 ]
56- table = A .Table .from_arrays (('a' , 'b' ), data , 'table_name' )
75+ table = pa .Table .from_arrays (('a' , 'b' ), data , 'table_name' )
5776
5877 # TODO: Use this part once from_pandas is implemented
5978 # data = {'a': range(5), 'b': [-10, -5, 0, 5, 10]}
6079 # df = pd.DataFrame(data)
61- # A .Table.from_pandas(df)
80+ # pa .Table.from_pandas(df)
6281
6382 df = table .to_pandas ()
6483 assert set (df .columns ) == set (('a' , 'b' ))
0 commit comments