Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Python Integration: Pandas Package Causing Stata to Close

    After setting my python_userpath and python_exec, attempting to execute the following lines from a do file causes Stata to exit immediately:

    python
    import os
    import time
    import csv
    import datetime
    import pandas as pd
    import numpy as np
    df_resp = pd.read_stata("responses.dta")
    df_resp
    end


    I am using Stata 16 on a Mac (Catalina OS: 10.15.7). Following Chuck Huber's blog posts, I have had some success in using Python from within Stata. I have a recent version of Anaconda installed, and Stata is recently updated.

    However, when I try to load a Stata dataset into Python using the Pandas command, "read_stata," Stata immediately exits. There is no error warning or message, only immediate exit from Stata.

    Initially, this same immediate exit behavior occurred whenever I typed "python which pandas". Another Stata user described the same behavior in the comments of the previously linked blog post. By re-installing Pandas, I was able to run "python which pandas" successfully.

    Strangely, the do file above ran successfully once and returned the expected result. I cannot determine what was different in that lone successful instance. The entire do file is:

    *prepare python
    set python_exec "~/opt/anaconda3/bin/python3"
    set python_userpath "~/opt/anaconda3/lib/python3.8/site-packages"
    python query

    *python modules
    python which os
    python which time
    python which csv
    python which datetime
    python which pandas
    python which numpy

    *load data into python using pandas (read_stata)
    python
    import os
    import time
    import csv
    import datetime
    import pandas as pd
    import numpy as np
    df_resp = pd.read_stata("responses.dta")
    df_resp
    end


    Any suggestions?

  • #2
    I had a similar issue, except Stata would close when I tried to import pandas. Try this in terminal

    Code:
    $ conda activate
    $ pip uninstall pandas
    $ pip install pandas

    Comment


    • #3
      Thanks for the suggestion, Justin. I entered your proposed commands in terminal, but I'm still encountering the same behavior from Stata.

      I should note that your proposed solution was what I had to do previously to import the pandas package successfully. Essentially, the exiting behavior from Stata was first encountered when I was trying to import pandas, and re-installing with pip solved that issue. Now, I'm encountering the same behavior when trying to use read_stata, and re-installing with pip did not resolve the issue.

      Comment


      • #4
        Hi John Hansen,

        Can you try the following in your Unix terminal?

        Code:
        $ ln -s ~/opt/anaconda3/lib YOUR_STATA_PATH/utilities/mac_lib
        It will create a symbolic mac_lib pointing to the library directory of the Anaconda you are using. Before running it, you need to replace your Stata installation directory with YOUR_STATA_PATH. You can find this path by typing
        Code:
        di c(sysdir_stata)
        in Stata.

        After that, type:

        Code:
        $ conda activate
        $ pip uninstall scipy
        $ pip install scipy
        in your terminal. Next try your code to see whether your problem still exists.

        Comment

        Working...
        X