Repository: statsmodels/statsmodels

Issue:
X13 path problems
#### Describe the bug

The STATSMODELS tsa x13_arima_analysis function runs a  step
    x12path = _check_x12(x12path)  
that calls 
   x12path = _find_x12(x12path)
This step that can  incorrectly prune a valid a path.

For example r'C:\Users\--username--l\Desktop\WinX13\x13as' is a valid path to the external x13as.exe Windows program that would be used. But _find_x12 will flag the ending  'x13as' as a potential binary file name, remove it and then create

x12path = r'C:\Users\--username--l\Desktop\WinX13\x13as.exe'

instead of the proper full path + executable

x12path = r'C:\Users\--username--l\Desktop\WinX13\x13as\x13as.exe'

Commenting out a  step in _find_x12 as below fixes the problem

 if x12path is not None and x12path.endswith(_binary_names):
        # remove binary from path if given
        #x12path = os.path.dirname(x12path)
        pass

ALSO RELATED:  in x13.py looking for  and using binaries  "x13as_ascii",  "x13as_html",  (in _binary_names as below) will create problems because they produce different output from     "x13as.exe".  Prior version of x13.py did not include these as valid external binaries
 
_binary_names = (
    "x13as.exe",
    "x13as",
    "x12a.exe",
    "x12a",
    "x13as_ascii",
    "x13as_html",
)

#### Code Sample,
import statsmodels.tsa.x13original as x13o
x12path1 = r'C:\Users\mdbol\Desktop\WinX13\WinX13\x13as'
print(x12path1)
x12path2 = x13o._check_x12(x12path1)
print(x12path2)

#### Expected Output

C:\\Users\\mdbol\\Desktop\\WinX13\\WinX13\\x13as'
C:\\Users\\mdbol\\Desktop\\WinX13\\WinX13\\x13as\\x13as.exe'

ERROR (current code)

C:\Users\mdbol\Desktop\WinX13\WinX13\x13as

---------------------------------------------------------------------------
X13NotFoundError                          Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_16712/517492357.py in <module>
      3 x12path1 = r'C:\Users\mdbol\Desktop\WinX13\WinX13\x13as'
      4 print(x12path1)
----> 5 x12path2 = x13o._check_x12(x12path1)
      6 print(x12path2)

~\AppData\Local\Programs\Python\Python37\lib\site-packages\statsmodels\tsa\x13original.py in _check_x12(x12path)
     96     if not x12path:
     97         raise X13NotFoundError(
---> 98             "x12a and x13as not found on path. Give the "
     99             "path, put them on PATH, or set the "
    100             "X12PATH or X13PATH environmental variable."

X13NotFoundError: x12a and x13as not found on path. Give the path, put them on PATH, or set the X12PATH or X13PATH environmental variable.




#### Output of ``import statsmodels.api as sm; sm.show_versions()``


INSTALLED VERSIONS
------------------
Python: 3.7.8.final.0

statsmodels
===========

Installed: 0.13.2 (C:\Users\mdbol\AppData\Local\Programs\Python\Python37\lib\site-packages\statsmodels)

Required Dependencies
=====================

cython: 0.29.21 (C:\Users\mdbol\AppData\Local\Programs\Python\Python37\lib\site-packages\Cython)
numpy: 1.19.5 (C:\Users\mdbol\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy)
scipy: 1.5.2 (C:\Users\mdbol\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy)
pandas: 1.0.3 (C:\Users\mdbol\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas)
    dateutil: 2.8.1 (C:\Users\mdbol\AppData\Local\Programs\Python\Python37\lib\site-packages\dateutil)
patsy: 0.5.2 (C:\Users\mdbol\AppData\Local\Programs\Python\Python37\lib\site-packages\patsy)

Optional Dependencies
=====================

matplotlib: 3.3.0 (C:\Users\mdbol\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib)
    backend: module://matplotlib_inline.backend_inline 
cvxopt: 1.2.6 (C:\Users\mdbol\AppData\Local\Programs\Python\Python37\lib\site-packages\cvxopt)
joblib: 0.16.0 (C:\Users\mdbol\AppData\Local\Programs\Python\Python37\lib\site-packages\joblib)

Developer Tools
================

IPython: 7.28.0 (C:\Users\mdbol\AppData\Local\Programs\Python\Python37\lib\site-packages\IPython)
    jinja2: 2.11.2 (C:\Users\mdbol\AppData\Local\Programs\Python\Python37\lib\site-packages\jinja2)
sphinx: Not installed
    pygments: 2.6.1 (C:\Users\mdbol\AppData\Local\Programs\Python\Python37\lib\site-packages\pygments)
pytest: Not installed
virtualenv: 20.4.0 (C:\Users\mdbol\AppData\Local\Programs\Python\Python37\lib\site-packages\virtualenv)



Discussion:

Resolving patch:
diff --git a/statsmodels/tsa/x13.py b/statsmodels/tsa/x13.py
--- a/statsmodels/tsa/x13.py
+++ b/statsmodels/tsa/x13.py
@@ -54,8 +54,9 @@ def _find_x12(x12path=None, prefer_x13=True):
     """
     global _binary_names
     if x12path is not None and x12path.endswith(_binary_names):
-        # remove binary from path if given
-        x12path = os.path.dirname(x12path)
+        # remove binary from path if path is not a directory
+        if not os.path.isdir(x12path):
+            x12path = os.path.dirname(x12path)
 
     if not prefer_x13:  # search for x12 first
         _binary_names = _binary_names[::-1]


Test patch: