Repository: statsmodels/statsmodels

Issue:
TSA X13 ARIMA incompatible with Python 3
When trying to run tsa.x13_arima\* with Python 3.4, the following error occurs:
AttributeError: 'dict' object has no attribute 'iteritems'

Diagnosis:
Iteritems is no longer available in Python 3.x and should be replaced with an analogous function.

Sources:
Own experience
https://ry9n.me/questions/30150775/get-attributeerror-when-producing-seasonally-adjusted-time-series-with-statsmodels-on-python-34
http://stackoverflow.com/questions/30150775/get-attributeerror-when-producing-seasonally-adjusted-time-series-with-statsmode



Discussion:
AFAICS
https://github.com/statsmodels/statsmodels/blob/master/statsmodels/tsa/x13.py#L218
uses `kwargs.iteritems()` instead of `iteritems(kwargs)`

`iteritems` is already imported from `compat`, and later `iteritems(kwargs)` is used

I don't have x13 installed right now and cannot test this. And obviously TravisCI doesn't have any x13 either, so it doesn't run the unit tests for it, I assume.

@marcinbielecki Thanks for reporting.
Can you try out changing line 218 mentioned above? 
That's all I can see right now without running the code.

I installed X13, but the above change is not enough for python 3 compatibility.

On python 3.4, I get

```
  File "bug_py3_2411.py", line 14, in <module>
    sm.tsa.x13_arima_analysis(non_adjusted).seasadj
  File "m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34\statsmodels\tsa\x13
.py", line 434, in x13_arima_analysis
    ftempin.write(spec)
  File "C:\programs\WinPython-64bit-3.4.3.1\python-3.4.3.amd64\lib\tempfile.py",
 line 399, in func_wrapper
    return func(*args, **kwargs)
TypeError: 'str' does not support the buffer interface
```

@jseabold   I get it to work with using `encode('utf-8)` and using our compat StringIO

Do you know anything about file encodings required by X13? I didn't find anything explicit about it, I assume it's ASCII, but utf-8 should be safe since it's the same for the ASCII characters.

There isn't much unit test coverage in the test_x13 module.


Resolving patch:
diff --git a/statsmodels/tsa/x13.py b/statsmodels/tsa/x13.py
--- a/statsmodels/tsa/x13.py
+++ b/statsmodels/tsa/x13.py
@@ -187,7 +187,7 @@ def _convert_out_to_series(x, dates, name):
     Convert x to a DataFrame where x is a string in the format given by
     x-13arima-seats output.
     """
-    from StringIO import StringIO
+    from statsmodels.compat import StringIO
     from pandas import read_table
     out = read_table(StringIO(x), skiprows=2, header=None)
     return out.set_index(dates).rename(columns={1 : name})[name]
@@ -215,7 +215,7 @@ def create_spec(self, **kwargs):
 
     def set_options(self, **kwargs):
         options = ""
-        for key, value in kwargs.iteritems():
+        for key, value in iteritems(kwargs):
             options += "{0}={1}\n".format(key, value)
             self.__dict__.update({key : value})
         self.options = options
@@ -431,7 +431,7 @@ def x13_arima_analysis(endog, maxorder=(2, 1), maxdiff=(2, 1), diff=None,
     ftempin = tempfile.NamedTemporaryFile(delete=False, suffix='.spc')
     ftempout = tempfile.NamedTemporaryFile(delete=False)
     try:
-        ftempin.write(spec)
+        ftempin.write(spec.encode('utf8'))
         ftempin.close()
         ftempout.close()
         # call x12 arima


Test patch: