Skip to content

Commit

Permalink
COMPAT: Use consistent import location for pandas testing
Browse files Browse the repository at this point in the history
Add pandas testing to compat
  • Loading branch information
bashtage committed Sep 15, 2018
1 parent a08d4f6 commit 75f857d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 17 deletions.
7 changes: 1 addition & 6 deletions statsmodels/base/tests/test_generic_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Author: Josef Perktold
"""
from statsmodels.compat.pandas import assert_series_equal, assert_index_equal
from statsmodels.compat.python import range

import numpy as np
Expand Down Expand Up @@ -660,12 +661,6 @@ def test_predict_missing(self):
ex.iloc[0, 1] = np.nan
predicted1 = self.res.predict(ex)
predicted2 = self.res.predict(ex[1:])
from pandas.util.testing import assert_series_equal
try:
from pandas.util.testing import assert_index_equal
except ImportError:
# for old pandas
from numpy.testing import assert_array_equal as assert_index_equal

assert_index_equal(predicted1.index, ex.index)
assert_series_equal(predicted1[1:], predicted2)
Expand Down
2 changes: 1 addition & 1 deletion statsmodels/base/tests/test_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"""
Tests for Results.predict
"""
from statsmodels.compat.pandas import testing as pdt

import numpy as np
import pandas as pd

from numpy.testing import assert_allclose, assert_equal
import pandas.util.testing as pdt

from statsmodels.regression.linear_model import OLS
from statsmodels.genmod.generalized_linear_model import GLM
Expand Down
9 changes: 9 additions & 0 deletions statsmodels/compat/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,12 @@ def sort_values(df, *args, **kwargs):

data_klasses = (pandas.Series, pandas.DataFrame, pandas.Panel,
pandas.WidePanel)

try:
import pandas.testing as testing
except ImportError:
import pandas.util.testing as testing

assert_frame_equal = testing.assert_frame_equal
assert_index_equal = testing.assert_index_equal
assert_series_equal = testing.assert_series_equal
5 changes: 1 addition & 4 deletions statsmodels/stats/tests/test_influence.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
Author: Josef Perktold
"""
from statsmodels.compat.pandas import testing as pdt

import os.path
import numpy as np
from numpy.testing import assert_allclose
import pandas as pd
try:
import pandas.testing as pdt
except ImportError:
import pandas.util.testing as pdt

import pytest

Expand Down
2 changes: 1 addition & 1 deletion statsmodels/tools/testing.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""assert functions from numpy and pandas testing
"""
from statsmodels.compat.pandas import testing as pdt

import re

import numpy.testing as npt
import pandas
import pandas.util.testing as pdt

# for pandas version check
def strip_rc(version):
Expand Down
10 changes: 5 additions & 5 deletions statsmodels/tsa/tests/test_stattools.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from statsmodels.compat.pandas import assert_index_equal
from statsmodels.compat.python import lrange

import os
Expand Down Expand Up @@ -752,35 +753,34 @@ def test_innovations_errors():
def test_innovations_filter_brockwell_davis():
ma = -0.9
acovf = np.array([1 + ma ** 2, ma])
theta, sigma2 = innovations_algo(acovf, nobs=4)
theta, _ = innovations_algo(acovf, nobs=4)
e = np.random.randn(5)
endog = e[1:] + ma * e[:-1]
resid = innovations_filter(endog, theta)
expected = [endog[0]]
for i in range(1, 4):
expected.append(endog[i] + theta[i, 0] * expected[-1])
print(expected)
expected = np.array(expected)
assert_allclose(resid, expected)


def test_innovations_filter_pandas():
ma = np.array([-0.9, 0.5])
acovf = np.array([1 + (ma ** 2).sum(), ma[0] + ma[1] * ma[0], ma[1]])
theta, sigma2 = innovations_algo(acovf, nobs=10)
theta, _ = innovations_algo(acovf, nobs=10)
endog = np.random.randn(10)
endog_pd = pd.Series(endog,
index=pd.date_range('2000-01-01', periods=10))
resid = innovations_filter(endog, theta)
resid_pd = innovations_filter(endog_pd, theta)
assert_allclose(resid, resid_pd.values)
pd.testing.assert_index_equal(endog_pd.index, resid_pd.index)
assert_index_equal(endog_pd.index, resid_pd.index)


def test_innovations_filter_errors():
ma = -0.9
acovf = np.array([1 + ma ** 2, ma])
theta, sigma2 = innovations_algo(acovf, nobs=4)
theta, _ = innovations_algo(acovf, nobs=4)
with pytest.raises(ValueError):
innovations_filter(np.empty((2, 2)), theta)
with pytest.raises(ValueError):
Expand Down

0 comments on commit 75f857d

Please sign in to comment.