|
| 1 | +import unittest |
| 2 | +import numpy as np |
| 3 | +import pandas as pd |
| 4 | +import polars as pl |
| 5 | +from dte_adj.util import _convert_to_ndarray |
| 6 | + |
| 7 | + |
| 8 | +class TestConvertToNdarray(unittest.TestCase): |
| 9 | + """Test that _convert_to_ndarray correctly converts various array-like inputs.""" |
| 10 | + |
| 11 | + def test_ndarray(self): |
| 12 | + data = np.array([1, 2, 3]) |
| 13 | + result = _convert_to_ndarray(data) |
| 14 | + self.assertIsInstance(result, np.ndarray) |
| 15 | + np.testing.assert_array_equal(result, data) |
| 16 | + |
| 17 | + def test_ndarray_2d(self): |
| 18 | + data = np.array([[1, 2], [3, 4]]) |
| 19 | + result = _convert_to_ndarray(data) |
| 20 | + self.assertIsInstance(result, np.ndarray) |
| 21 | + np.testing.assert_array_equal(result, data) |
| 22 | + |
| 23 | + def test_pandas_series(self): |
| 24 | + data = pd.Series([1, 2, 3]) |
| 25 | + result = _convert_to_ndarray(data) |
| 26 | + self.assertIsInstance(result, np.ndarray) |
| 27 | + np.testing.assert_array_equal(result, np.array([1, 2, 3])) |
| 28 | + |
| 29 | + def test_pandas_dataframe(self): |
| 30 | + data = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) |
| 31 | + result = _convert_to_ndarray(data) |
| 32 | + self.assertIsInstance(result, np.ndarray) |
| 33 | + np.testing.assert_array_equal(result, np.array([[1, 3], [2, 4]])) |
| 34 | + |
| 35 | + def test_polars_series(self): |
| 36 | + data = pl.Series([1, 2, 3]) |
| 37 | + result = _convert_to_ndarray(data) |
| 38 | + self.assertIsInstance(result, np.ndarray) |
| 39 | + np.testing.assert_array_equal(result, np.array([1, 2, 3])) |
| 40 | + |
| 41 | + def test_polars_dataframe(self): |
| 42 | + data = pl.DataFrame({"a": [1, 2], "b": [3, 4]}) |
| 43 | + result = _convert_to_ndarray(data) |
| 44 | + self.assertIsInstance(result, np.ndarray) |
| 45 | + np.testing.assert_array_equal(result, np.array([[1, 3], [2, 4]])) |
| 46 | + |
| 47 | + def test_list(self): |
| 48 | + data = [1, 2, 3] |
| 49 | + result = _convert_to_ndarray(data) |
| 50 | + self.assertIsInstance(result, np.ndarray) |
| 51 | + np.testing.assert_array_equal(result, np.array([1, 2, 3])) |
| 52 | + |
| 53 | + def test_tuple(self): |
| 54 | + data = (1, 2, 3) |
| 55 | + result = _convert_to_ndarray(data) |
| 56 | + self.assertIsInstance(result, np.ndarray) |
| 57 | + np.testing.assert_array_equal(result, np.array([1, 2, 3])) |
0 commit comments