7
7
#######################################################################
8
8
9
9
import numpy as np
10
+ import pytest
10
11
11
12
import blosc2
12
13
13
14
14
15
class TestPandasUDF :
15
- def test_map_1d (self ):
16
+ def test_map (self ):
16
17
def add_one (x ):
17
18
return x + 1
18
19
19
20
data = np .array ([1 , 2 ])
20
21
21
- result = blosc2 .jit .__pandas_udf__ .map (
22
+ with pytest .raises (NotImplementedError ):
23
+ blosc2 .jit .__pandas_udf__ .map (
24
+ data ,
25
+ add_one ,
26
+ args = (),
27
+ kwargs = {},
28
+ decorator = blosc2 .jit ,
29
+ skip_na = False ,
30
+ )
31
+
32
+ def test_apply_1d (self ):
33
+ def add_one (x ):
34
+ return x + 1
35
+
36
+ data = np .array ([1 , 2 ])
37
+
38
+ result = blosc2 .jit .__pandas_udf__ .apply (
22
39
data ,
23
40
add_one ,
24
41
args = (),
25
42
kwargs = {},
26
43
decorator = blosc2 .jit ,
27
- skip_na = False ,
44
+ axis = 0 ,
28
45
)
29
46
assert result .shape == (2 ,)
30
47
assert result [0 ] == 2
31
48
assert result [1 ] == 3
32
49
33
- def test_map_1d_with_args (self ):
50
+ def test_apply_1d_with_args (self ):
34
51
def add_numbers (x , num1 , num2 ):
35
52
return x + num1 + num2
36
53
37
54
data = np .array ([1 , 2 ])
38
55
39
- result = blosc2 .jit .__pandas_udf__ .map (
56
+ result = blosc2 .jit .__pandas_udf__ .apply (
40
57
data ,
41
58
add_numbers ,
42
59
args = (10 ,),
43
60
kwargs = {"num2" : 100 },
44
61
decorator = blosc2 .jit ,
45
- skip_na = False ,
62
+ axis = 0 ,
46
63
)
47
64
assert result .shape == (2 ,)
48
65
assert result [0 ] == 111
49
66
assert result [1 ] == 112
50
67
51
- def test_map_2d (self ):
68
+ def test_apply_2d (self ):
52
69
def add_one (x ):
70
+ assert x .shape == (2 , 3 )
53
71
return x + 1
54
72
55
- data = np .array ([[1 , 2 ], [ 3 , 4 ]])
73
+ data = np .array ([[1 , 2 , 3 ], [ 4 , 5 , 6 ]])
56
74
57
- result = blosc2 .jit .__pandas_udf__ .map (
75
+ result = blosc2 .jit .__pandas_udf__ .apply (
58
76
data ,
59
77
add_one ,
60
78
args = (),
61
79
kwargs = {},
62
80
decorator = blosc2 .jit ,
63
- skip_na = False ,
81
+ axis = None ,
64
82
)
65
- assert result .shape == (2 , 2 )
66
- assert result [0 , 0 ] == 2
67
- assert result [0 , 1 ] == 3
68
- assert result [1 , 0 ] == 4
69
- assert result [1 , 1 ] == 5
83
+ expected = np .array ([[2 , 3 , 4 ], [5 , 6 , 7 ]])
84
+ assert np .array_equal (result , expected )
70
85
71
- def test_apply_1d (self ):
86
+ def test_apply_2d_by_column (self ):
72
87
def add_one (x ):
88
+ assert x .shape == (2 ,)
73
89
return x + 1
74
90
75
- data = np .array ([1 , 2 ])
91
+ data = np .array ([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ] ])
76
92
77
93
result = blosc2 .jit .__pandas_udf__ .apply (
78
94
data ,
@@ -82,44 +98,23 @@ def add_one(x):
82
98
decorator = blosc2 .jit ,
83
99
axis = 0 ,
84
100
)
85
- assert result .shape == (2 ,)
86
- assert result [0 ] == 2
87
- assert result [1 ] == 3
101
+ expected = np .array ([[2 , 3 , 4 ], [5 , 6 , 7 ]])
102
+ assert np .array_equal (result , expected )
88
103
89
- def test_apply_1d_with_args (self ):
90
- def add_numbers (x , num1 , num2 ):
91
- return x + num1 + num2
92
-
93
- data = np .array ([1 , 2 ])
94
-
95
- result = blosc2 .jit .__pandas_udf__ .apply (
96
- data ,
97
- add_numbers ,
98
- args = (10 ,),
99
- kwargs = {"num2" : 100 },
100
- decorator = blosc2 .jit ,
101
- axis = 0 ,
102
- )
103
- assert result .shape == (2 ,)
104
- assert result [0 ] == 111
105
- assert result [1 ] == 112
106
-
107
- def test_apply_2d (self ):
104
+ def test_apply_2d_by_row (self ):
108
105
def add_one (x ):
106
+ assert x .shape == (3 ,)
109
107
return x + 1
110
108
111
- data = np .array ([[1 , 2 ], [ 3 , 4 ]])
109
+ data = np .array ([[1 , 2 , 3 ], [ 4 , 5 , 6 ]])
112
110
113
111
result = blosc2 .jit .__pandas_udf__ .apply (
114
112
data ,
115
113
add_one ,
116
114
args = (),
117
115
kwargs = {},
118
116
decorator = blosc2 .jit ,
119
- axis = 0 ,
117
+ axis = 1 ,
120
118
)
121
- assert result .shape == (2 , 2 )
122
- assert result [0 , 0 ] == 2
123
- assert result [0 , 1 ] == 3
124
- assert result [1 , 0 ] == 4
125
- assert result [1 , 1 ] == 5
119
+ expected = np .array ([[2 , 3 , 4 ], [5 , 6 , 7 ]])
120
+ assert np .array_equal (result , expected )
0 commit comments