Repository: statsmodels/statsmodels

Issue:
BUG: SARIMAX does not accept NumPy integer periodicty
Passing a NumPy integer (instead of a Python integer) to the periodicty in the `seasonal_order` argument of `SARIMAX` raises the exception:

> TypeError: object of type 'numpy.int64' has no len()

The following minimal code snippet reproduces the problem:

```
from numpy import *
from statsmodels.tsa.statespace.sarimax import SARIMAX

t = linspace(0, 8.0*pi, num=40 + 1)
y = sin(t) + random.normal(scale=0.5, size=len(t))
model = SARIMAX(
    y,
    seasonal_order=(0, 1, 0, int64(10))).fit()
```

The problem is in fact related to `isinstance(int64(), int)` being `False`, see also https://github.com/numpy/numpy/issues/2951.
I am ussing statsmodels version 0.9.0, but looking at the master branch https://github.com/statsmodels/statsmodels/blob/master/statsmodels/tsa/statespace/tools.py#L181 I expect that this problem is not limited to my version only.
As a quick fix in my own code I do:
```
seasonal_order = tuple(int(i) for i in seasonal_order)
```
but it would be desirable if SARIMAX also works with NumPy integers.


Discussion:
Thanks for the report! This shouldn’t be too hard to fix, I think.

I never ran into this myself - is there a particular use case where Numpy
ints are used?

>

NumPy ints come up all the tie when automating runs where parameterizations are stored as NumPy arrays.  Best practice is to sanitize using `int(x)` (or long, it is can be big and you care about Python 2.x) at the user boundary. 
You could also check `isinstance(x, (int, np.integer))` to allow any numpy integer type through too.
> NumPy ints come up all the tie when automating runs where parameterizations are stored as NumPy arrays. Best practice is to sanitize using int(x) (or long, it is can be big and you care about Python 2.x) at the user boundary.

Thanks!

Resolving patch:
diff --git a/statsmodels/tsa/statespace/initialization.py b/statsmodels/tsa/statespace/initialization.py
--- a/statsmodels/tsa/statespace/initialization.py
+++ b/statsmodels/tsa/statespace/initialization.py
@@ -279,7 +279,8 @@ def set(self, index, initialization_type, constant=None,
         # Construct the index, using a slice object as an intermediate step
         # to enforce regularity
         if not isinstance(index, slice):
-            if isinstance(index, int):
+            if isinstance(index, (int, np.integer)):
+                index = int(index)
                 if index < 0 or index >= self.k_states:
                     raise ValueError('Invalid index.')
                 index = (index, index + 1)
@@ -422,7 +423,8 @@ def unset(self, index):
         initialization. To unset all initializations (including both global and
         block level), use the `clear` method.
         """
-        if isinstance(index, int):
+        if isinstance(index, (int, np.integer)):
+            index = int(index)
             if index < 0 or index > self.k_states:
                 raise ValueError('Invalid index.')
             index = (index, index + 1)
diff --git a/statsmodels/tsa/statespace/tools.py b/statsmodels/tsa/statespace/tools.py
--- a/statsmodels/tsa/statespace/tools.py
+++ b/statsmodels/tsa/statespace/tools.py
@@ -178,8 +178,9 @@ def companion_matrix(polynomial):
 
     """
     identity_matrix = False
-    if isinstance(polynomial, int):
-        n = polynomial
+    if isinstance(polynomial, (int, np.integer)):
+        # GH 5570, allow numpy integer types, but coerce to python int
+        n = int(polynomial)
         m = 1
         polynomial = None
     else:


Test patch:
diff --git a/statsmodels/tsa/statespace/tests/test_tools.py b/statsmodels/tsa/statespace/tests/test_tools.py
--- a/statsmodels/tsa/statespace/tests/test_tools.py
+++ b/statsmodels/tsa/statespace/tests/test_tools.py
@@ -30,7 +30,9 @@ class TestCompanionMatrix(object):
          np.array([[1, 2, 5, 6],
                    [3, 4, 7, 8],
                    [1, 0, 0, 0],
-                   [0, 1, 0, 0]]).T)
+                   [0, 1, 0, 0]]).T),
+        # GH 5570
+        (np.int64(2), np.array([[0, 1], [0, 0]]))
     ]
 
     def test_cases(self):