|
| 1 | +####################################################################### |
| 2 | +# Copyright (c) 2019-present, Blosc Development Team <[email protected]> |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under a BSD-style license (found in the |
| 6 | +# LICENSE file in the root directory of this source tree) |
| 7 | +####################################################################### |
| 8 | + |
| 9 | +import numpy as np |
| 10 | + |
| 11 | +import blosc2 |
| 12 | + |
| 13 | + |
| 14 | +class TestPandasUDF: |
| 15 | + def test_map_1d(self): |
| 16 | + def add_one(x): |
| 17 | + return x + 1 |
| 18 | + |
| 19 | + data = np.array([1, 2]) |
| 20 | + |
| 21 | + result = blosc2.jit.__pandas_udf__.map( |
| 22 | + data, |
| 23 | + add_one, |
| 24 | + args=(), |
| 25 | + kwargs={}, |
| 26 | + decorator=blosc2.jit, |
| 27 | + skip_na=False, |
| 28 | + ) |
| 29 | + assert result.shape == (2,) |
| 30 | + assert result[0] == 2 |
| 31 | + assert result[1] == 3 |
| 32 | + |
| 33 | + def test_map_1d_with_args(self): |
| 34 | + def add_numbers(x, num1, num2): |
| 35 | + return x + num1 + num2 |
| 36 | + |
| 37 | + data = np.array([1, 2]) |
| 38 | + |
| 39 | + result = blosc2.jit.__pandas_udf__.map( |
| 40 | + data, |
| 41 | + add_numbers, |
| 42 | + args=(10,), |
| 43 | + kwargs={"num2": 100}, |
| 44 | + decorator=blosc2.jit, |
| 45 | + skip_na=False, |
| 46 | + ) |
| 47 | + assert result.shape == (2,) |
| 48 | + assert result[0] == 111 |
| 49 | + assert result[1] == 112 |
| 50 | + |
| 51 | + def test_map_2d(self): |
| 52 | + def add_one(x): |
| 53 | + return x + 1 |
| 54 | + |
| 55 | + data = np.array([[1, 2], [3, 4]]) |
| 56 | + |
| 57 | + result = blosc2.jit.__pandas_udf__.map( |
| 58 | + data, |
| 59 | + add_one, |
| 60 | + args=(), |
| 61 | + kwargs={}, |
| 62 | + decorator=blosc2.jit, |
| 63 | + skip_na=False, |
| 64 | + ) |
| 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 |
| 70 | + |
| 71 | + def test_apply_1d(self): |
| 72 | + def add_one(x): |
| 73 | + return x + 1 |
| 74 | + |
| 75 | + data = np.array([1, 2]) |
| 76 | + |
| 77 | + result = blosc2.jit.__pandas_udf__.apply( |
| 78 | + data, |
| 79 | + add_one, |
| 80 | + args=(), |
| 81 | + kwargs={}, |
| 82 | + decorator=blosc2.jit, |
| 83 | + axis=0, |
| 84 | + ) |
| 85 | + assert result.shape == (2,) |
| 86 | + assert result[0] == 2 |
| 87 | + assert result[1] == 3 |
| 88 | + |
| 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): |
| 108 | + def add_one(x): |
| 109 | + return x + 1 |
| 110 | + |
| 111 | + data = np.array([[1, 2], [3, 4]]) |
| 112 | + |
| 113 | + result = blosc2.jit.__pandas_udf__.apply( |
| 114 | + data, |
| 115 | + add_one, |
| 116 | + args=(), |
| 117 | + kwargs={}, |
| 118 | + decorator=blosc2.jit, |
| 119 | + axis=0, |
| 120 | + ) |
| 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 |
0 commit comments