1
+ from pandas .core .internals .construction import to_arrays
2
+ import numpy as np
3
+ from pandas .core .indexes .api import ensure_index
4
+ from numpy import array
5
+
6
+ def test_to_arrays ():
7
+ # GH 59717
8
+ data = np .array ([
9
+ ('John' , 25 , 'New York' , 50000 ),
10
+ ('Jane' , 30 , 'San Francisco' , 75000 ),
11
+ ('Bob' , 35 , 'Chicago' , 65000 ),
12
+ ('Alice' , 28 , 'Los Angeles' , 60000 )
13
+ ], dtype = [('name' , 'U10' ), ('age' , 'i4' ), ('city' , 'U15' ), ('salary' , 'i4' )])
14
+
15
+ columns = ['name' , 'salary' , 'city' ]
16
+ indexed_columns = ensure_index (columns )
17
+
18
+ actual_arrays , actual_cols = to_arrays (data , indexed_columns )
19
+ expected_arrays = [array (['John' , 'Jane' , 'Bob' , 'Alice' ], dtype = '<U10' ),
20
+ array ([50000 , 75000 , 65000 , 60000 ], dtype = int ),
21
+ array (['New York' , 'San Francisco' , 'Chicago' , 'Los Angeles' ], dtype = '<U15' )]
22
+
23
+ for actual , expected in zip (actual_arrays , expected_arrays ):
24
+ assert np .array_equal (actual , expected ), f"Arrays do not match:\n Actual: { actual } \n Expected: { expected } "
25
+
26
+ assert actual_cols .equals (indexed_columns ), "Columns do not match"
0 commit comments