PageRenderTime 93ms CodeModel.GetById 60ms app.highlight 9ms RepoModel.GetById 11ms app.codeStats 10ms

/statsmodels/tools/tests/test_data.py

http://github.com/statsmodels/statsmodels
Python | 54 lines | 43 code | 8 blank | 3 comment | 0 complexity | 0b26bdbced74bad11eb46b2532993ceb MD5 | raw file
 1import pandas
 2import numpy as np
 3import pytest
 4
 5from statsmodels.tools import data
 6
 7
 8def test_missing_data_pandas():
 9    """
10    Fixes GH: #144
11    """
12    X = np.random.random((10,5))
13    X[1,2] = np.nan
14    df = pandas.DataFrame(X)
15    vals, cnames, rnames = data.interpret_data(df)
16    np.testing.assert_equal(rnames.tolist(), [0,2,3,4,5,6,7,8,9])
17
18def test_structarray():
19    X = np.random.random((9,)).view([('var1', 'f8'),
20                                        ('var2', 'f8'),
21                                        ('var3', 'f8')])
22    with pytest.warns(FutureWarning, match="recarray support"):
23        vals, cnames, rnames = data.interpret_data(X)
24    np.testing.assert_equal(cnames, X.dtype.names)
25    np.testing.assert_equal(vals, X.view((float,3)))
26    np.testing.assert_equal(rnames, None)
27
28def test_recarray():
29    X = np.random.random((9,)).view([('var1', 'f8'),
30                                        ('var2', 'f8'),
31                                        ('var3', 'f8')])
32    with pytest.warns(FutureWarning, match="recarray support"):
33        vals, cnames, rnames = data.interpret_data(X.view(np.recarray))
34    np.testing.assert_equal(cnames, X.dtype.names)
35    np.testing.assert_equal(vals, X.view((float,3)))
36    np.testing.assert_equal(rnames, None)
37
38
39def test_dataframe():
40    X = np.random.random((10,5))
41    df = pandas.DataFrame(X)
42    vals, cnames, rnames = data.interpret_data(df)
43    np.testing.assert_equal(vals, df.values)
44    np.testing.assert_equal(rnames.tolist(), df.index.tolist())
45    np.testing.assert_equal(cnames, df.columns.tolist())
46
47def test_patsy_577():
48    X = np.random.random((10, 2))
49    df = pandas.DataFrame(X, columns=["var1", "var2"])
50    from patsy import dmatrix
51    endog = dmatrix("var1 - 1", df)
52    np.testing.assert_(data._is_using_patsy(endog, None))
53    exog = dmatrix("var2 - 1", df)
54    np.testing.assert_(data._is_using_patsy(endog, exog))