|
18 | 18 | import numpy as np
|
19 | 19 | import numpy.ma as ma
|
20 | 20 | import numpy.testing as npt
|
| 21 | +import pandas as pd |
21 | 22 | import pytest
|
22 | 23 | import scipy.sparse as sps
|
23 | 24 |
|
|
47 | 48 | from pymc.vartypes import int_types
|
48 | 49 |
|
49 | 50 |
|
| 51 | +@pytest.mark.parametrize( |
| 52 | + argnames="np_array", |
| 53 | + argvalues=[ |
| 54 | + np.array([[1.0], [2.0], [-1.0]]), |
| 55 | + np.array([[1.0, 1.0, 1.0], [0.0, 0.0, 0.0]]), |
| 56 | + np.ones(shape=(10, 1)), |
| 57 | + ], |
| 58 | +) |
| 59 | +def test_pd_dataframe_as_tensor_variable(np_array: np.ndarray) -> None: |
| 60 | + df = pd.DataFrame(np_array) |
| 61 | + np.testing.assert_array_equal(x=at.as_tensor_variable(x=df).eval(), y=np_array) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize( |
| 65 | + argnames="np_array", |
| 66 | + argvalues=[np.array([1.0, 2.0, -1.0]), np.ones(shape=4), np.zeros(shape=10), [1, 2, 3, 4]], |
| 67 | +) |
| 68 | +def test_pd_series_as_tensor_variable(np_array: np.ndarray) -> None: |
| 69 | + df = pd.Series(np_array) |
| 70 | + np.testing.assert_array_equal(x=at.as_tensor_variable(x=df).eval(), y=np_array) |
| 71 | + |
| 72 | + |
| 73 | +def test_pd_as_tensor_variable_multiindex() -> None: |
| 74 | + |
| 75 | + tuples = [("L", "Q"), ("L", "I"), ("O", "L"), ("O", "I")] |
| 76 | + |
| 77 | + index = pd.MultiIndex.from_tuples(tuples, names=["Id1", "Id2"]) |
| 78 | + |
| 79 | + df = pd.DataFrame({"A": [12.0, 80.0, 30.0, 20.0], "B": [120.0, 700.0, 30.0, 20.0]}, index=index) |
| 80 | + np_array = np.array([[12.0, 80.0, 30.0, 20.0], [120.0, 700.0, 30.0, 20.0]]).T |
| 81 | + assert isinstance(df.index, pd.MultiIndex) |
| 82 | + np.testing.assert_array_equal(x=at.as_tensor_variable(x=df).eval(), y=np_array) |
| 83 | + |
| 84 | + |
50 | 85 | def test_change_rv_size():
|
51 | 86 | loc = at.as_tensor_variable([1, 2])
|
52 | 87 | rv = normal(loc=loc)
|
@@ -224,7 +259,6 @@ def test_convert_observed_data(input_dtype):
|
224 | 259 | Ensure that convert_observed_data returns the dense array, masked array,
|
225 | 260 | graph variable, TensorVariable, or sparse matrix as appropriate.
|
226 | 261 | """
|
227 |
| - pd = pytest.importorskip("pandas") |
228 | 262 | # Create the various inputs to the function
|
229 | 263 | sparse_input = sps.csr_matrix(np.eye(3)).astype(input_dtype)
|
230 | 264 | dense_input = np.arange(9).reshape((3, 3)).astype(input_dtype)
|
@@ -300,7 +334,6 @@ def test_convert_observed_data(input_dtype):
|
300 | 334 |
|
301 | 335 |
|
302 | 336 | def test_pandas_to_array_pandas_index():
|
303 |
| - pd = pytest.importorskip("pandas") |
304 | 337 | data = pd.Index([1, 2, 3])
|
305 | 338 | result = convert_observed_data(data)
|
306 | 339 | expected = np.array([1, 2, 3])
|
|
0 commit comments