|
Size: 2239
Comment:
|
← Revision 14 as of 2025-10-24 16:20:01 ⇥
Size: 3359
Comment: Rewrite
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 1: | Line 1: |
| = Setup = | = Stata Python = |
| Line 3: | Line 3: |
| The path to the Python executable is set using: | Stata supports calling out to an embedded [[Python]] interpretter. |
| Line 5: | Line 5: |
| {{{ python set exec "path_string" }}} Stata can list recognized Python environments with `python search`. To make this setting permanent, use the `permanent` option. |
<<TableOfContents>> |
| Line 17: | Line 11: |
| = Python REPL = | == Installation == |
| Line 19: | Line 13: |
| A Python REPL is entered with the `python` command. It prints to the screen a reminder that the `end` command is used to exit the environment. Stata local variables are accessed with quotations. |
To list recognized Python environments, try `python search`. If an valid environment is known to exist but was not recognized, try: |
| Line 24: | Line 16: |
| . local int_var = 3 . local str_var = "This is a Stata string" . python ---------------------------------------- python (type end to exit) ----------- >>> `int_var' 3 >>> "`str_var'".split(" ") ['This', 'is', 'a', 'Stata', 'string'] |
python set exec "C:\path\to\python\installation" |
| Line 34: | Line 19: |
| Within the REPL, the `stata` context submits commands to the parent Stata shell. | To manipulate (i.e., append or prepend) the `PYTHONPATH` environment variable, try: |
| Line 37: | Line 22: |
| >>> stata: sysuse auto, clear | python set userpath "C:\foo" "C:\bar" "C:\baz" python set userpath "C:\foo" "C:\bar" "C:\baz", prepend |
| Line 40: | Line 26: |
== Python Scope == A Python scope can be entered with `python:` (similar to defining function in Stata). This Python environment is exited upon leaving the scope, i.e. the end of the statement. To submit multiple statements, delimit them with semicolons. |
To make these settings permanent, add the '''`permanent`''' option. |
| Line 50: | Line 32: |
| = Python SFI Module = | == Usage == |
| Line 52: | Line 34: |
| A foreign function interface module is also available. | Much like [[Stata/Mata|Mata]], there is a '''Python scope'''. |
| Line 55: | Line 37: |
| from sfi import Data pymake = Data.get('make') # do something |
python: print("Hello, world") |
| Line 59: | Line 39: |
This can even be used within the Python REPL that is running within the Stata shell. |
|
| Line 64: | Line 42: |
| == Stata Do Files == | === Subsessions === |
| Line 66: | Line 44: |
| Python scopes within a Do file are entered with `python:` and terminated with `end`. | Within an interactive Stata session, enter a Python interactive subsession with the '''`-python-`''' command. |
| Line 69: | Line 47: |
| sysuse auto, clear | . // This is an interactive Stata session . local int_var = 3 . local str_var = "This is a Stata string" . . // Now start the Python subsession . python ---------------------------------------- python (type end to exit) ----------- >>> >>> myint = 3 >>> mystr = "This is a Python string >>> >>> # Stata macros evaluate here >>> `int_var' 3 >>> "`str_var'".split(" ") ['This', 'is', 'a', 'Stata', 'string'] >>> >>> # Call back out to the Stata session >>> stata: webuse auto, clear }}} === Programs === Within an [[Stata/AdoFiles|ado file]], run Python code like: {{{ |
| Line 71: | Line 78: |
| from sfi import Data pymake = Data.get(’make’) # do something |
import sqlite3 import pandas as pd con = sqlite3.connect("`a'") df = pd.read_sql_query("SELECT * from `b'", con) con.close() |
| Line 77: | Line 87: |
| A common strategy is to define Python functions within a Do file, then create Stata functions to interface. | Note that objects in the `__main__` namespace are retained across Python sessions. If the `con` [[Python/Sqlite3|sqlite3.Connection]] object was not closed, it would have remained in memory until the Stata process ended. Best practices for bundling Python in an ado file are: * Write as a function to be called, not as a script that evaluates immediately * Write an accompanying Stata program to wrap it, and handle everything user-facing there * Call the function from a scope |
| Line 97: | Line 112: |
| . sysuse auto, clear | . webuse auto |
| Line 109: | Line 124: |
== Stata Interoperability == To move data between Python and Stata processes, use the `sfi` module. {{{ python: import pandas as pd from sfi import Data # initialize N cases Data.setObsTotal(len(df)) # initialize variables Data.addVarDouble("id") Data.addVarStr("name",5) # copy columnar data Data.store("id", None, df["id"], None) Data.store("zipcode", None, df["name"], None) # free memory del df end }}} This module can be imported into both programs and interactive sessions. It is not a publicly available module. ---- == See also == [[https://www.stata.com/manuals/ppystataintegration.pdf|Stata manual on PyStata integration]] ---- |
Stata Python
Stata supports calling out to an embedded Python interpretter.
Installation
To list recognized Python environments, try python search. If an valid environment is known to exist but was not recognized, try:
python set exec "C:\path\to\python\installation"
To manipulate (i.e., append or prepend) the PYTHONPATH environment variable, try:
python set userpath "C:\foo" "C:\bar" "C:\baz" python set userpath "C:\foo" "C:\bar" "C:\baz", prepend
To make these settings permanent, add the permanent option.
Usage
Much like Mata, there is a Python scope.
python: print("Hello, world")
Subsessions
Within an interactive Stata session, enter a Python interactive subsession with the -python- command.
. // This is an interactive Stata session
. local int_var = 3
. local str_var = "This is a Stata string"
.
. // Now start the Python subsession
. python
---------------------------------------- python (type end to exit) -----------
>>>
>>> myint = 3
>>> mystr = "This is a Python string
>>>
>>> # Stata macros evaluate here
>>> `int_var'
3
>>> "`str_var'".split(" ")
['This', 'is', 'a', 'Stata', 'string']
>>>
>>> # Call back out to the Stata session
>>> stata: webuse auto, clear
Programs
Within an ado file, run Python code like:
python:
import sqlite3
import pandas as pd
con = sqlite3.connect("`a'")
df = pd.read_sql_query("SELECT * from `b'", con)
con.close()
endNote that objects in the __main__ namespace are retained across Python sessions. If the con sqlite3.Connection object was not closed, it would have remained in memory until the Stata process ended.
Best practices for bundling Python in an ado file are:
- Write as a function to be called, not as a script that evaluates immediately
- Write an accompanying Stata program to wrap it, and handle everything user-facing there
- Call the function from a scope
program varsum
version 16.0
syntax varname [if] [in]
python: _varsum("`varlist'", "`touse'")
display as txt " sum of ‘varlist’: " as res r(sum)
end
python:
from sfi import Data, Scalar
def _varsum(varname, touse):
x = Data.get(varname, None, touse)
Scalar.setValue("r(sum)", sum(x))
end. webuse auto (1978 Automobile Data) . varsum price sum of price: 456229 . varsum price if foreign sum of price: 140463
Stata Interoperability
To move data between Python and Stata processes, use the sfi module.
python:
import pandas as pd
from sfi import Data
# initialize N cases
Data.setObsTotal(len(df))
# initialize variables
Data.addVarDouble("id")
Data.addVarStr("name",5)
# copy columnar data
Data.store("id", None, df["id"], None)
Data.store("zipcode", None, df["name"], None)
# free memory
del df
endThis module can be imported into both programs and interactive sessions. It is not a publicly available module.
See also
Stata manual on PyStata integration
