Skip to main content

pdexplorer is a Stata emulator for Python/pandas.

Installation

pdexplorer is available on PyPI. Run pip to install:

pip install pdexplorer

Usage

pdexplorer can be run in three modes:

1. Stata-like Emulation

from pdexplorer import *

This import adds Stata-like commands into the Python namespace. For example,

webuse('auto')
reg('mpg price')

2. Pure Stata Emulation

from pdexplorer import do
do() # Launches a Stata emulator that can run normal Stata commands

Now you can run regular Stata commands e.g.,

webuse auto
reg mpg price

do() also supports running the contents of do-file e.g.,

do('working.do')

Under the hoods, the Stata emulator translates pure Stata commands into their Pythonic equivalents. For example, reg mpg price becomes reg('mpg price').

3. Inline Stata
For example,

from pdexplorer import do, current
do(inline="""
webuse auto
reg mpg price
""") # Launches a Stata emulator that can run normal Stata commands
print(current.df) # access DataFrame object in Python

The rest of this documentation shows examples using Stata-like emulation, but these commands can all be run using pure Stata emulation as well.

How pdexplorer differs from Stata

  • pdexplorer uses Python libraries under the hood. (The result of a command reflects the output of those libraries and may differ slightly from equivalent Stata output.)
  • There is no support for mata. Under the hood, pdexplorer is just the Python data stack.

Philosophy

Stata is great for its conciseness and readability. But Python/pandas is free and easier to integrate with other applications. For example, you can build a web server in Python, but not Stata; You can run Python in AWS SageMmaker, but not Stata.

pdexplorer enables Stata to be easily integrated into the Python ecosystem.

In contrast to raw Python/pandas, Stata syntax achieves succinctness by:

  • Using spaces and commas rather than parentheses, brackets, curly braces, and quotes (where possible)
  • Specifying a set of concise commands on the "current" dataset rather than cluttering the namespace with multiple datasets
  • Being verbose by default i.e., displaying output that represents the results of the command
  • Having sensible defaults that cover the majority of use cases and demonstrate common usage
  • Allowing for namespace abbreviations for both commands and variable names
  • Employing two types of column names: Variable name are concise and used for programming. Variable labels are verbose and used for presentation.
  • Packages are imported lazily e.g., import statsmodels is loaded only when it's first used by a command. This ensures that from pdexplorer import * runs quickly.

Examples

Load Stata dataset and perform exploratory data analysis

webuse('auto')
li() # List the contents of the data

See https://www.stata.com/manuals/dwebuse.pdf

Summarize Data by Subgroups

webuse('auto')
with by('foreign'):
    summarize('mpg weight')

See https://www.stata.com/manuals/rsummarize.pdf

Ordinary Least Squares (OLS) Regression

webuse('auto')
regress('mpg weight foreign')
ereturnlist()

Return Values

In the last example, note the use of ereturnlist(), corresponding to the Stata command ereturn list. Additionally, a Python object may also be available as the command's return value. For example,

webuse('auto')
results = regress('mpg weight foreign')

Here, results is a RegressionResultsWrapper object from the statsmodels package.

Syntax summary

With few exceptions, the basic Stata language syntax (as documented here) is

[by varlist:] command [subcommand] [varlist] [=exp] [if exp] [in range] [weight] [, options]

where square brackets distinguish optional qualifiers and options from required ones. In this diagram, varlist denotes a list of variable names, command denotes a Stata command, exp denotes an algebraic expression, range denotes an observation range, weight denotes a weighting expression, and options denotes a list of options.

The by varlist: prefix causes Stata to repeat a command for each subset of the data for which the values of the variables in varlist are equal. When prefixed with by varlist:, the result of the command will be the same as if you had formed separate datasets for each group of observations, saved them, and then gave the command on each dataset separately. The data must already be sorted by varlist, although by has a sort option.

In pdexplorer, this gets translated to

with by('varlist'):
    command("[subcommand] [varlist] [=exp] [if exp] [in range] [weight] [, options]", *args, **kwargs)

where *args, and **kwargs represent additional arguments that might available in a pdexplorer command but not in the equivalent Stata command. (This is rarely used.)

Sometimes, Stata commands are two words. In such cases, the pdexplorer command is a concatenation of the two words. For example,

label data "label"

becomes

labeldata("label")

Command Dependencies

pdexplorer command package dependency
cf ydata-profiling or sweetviz
browse xlwings
regress statsmodels

Python-In-Excel Support

pdexplorer can be used with Excel's new Python-in-Excel feature.

Usage:

insert_pdexplorer my_excel_sheet.xlsm

This script inserts (or overrides) a worksheet called _pdexplorer into my_excel_sheet.xlsm which contains the core modules of pdexplorer.

References

Metadata

Release files for pdexplorer 0.0.40

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pdexplorer 0.0.40
File Size Uploaded
pdexplorer-0.0.40.tar.gz 110.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pdexplorer 0.0.40
File Interpreter ABI Platform
pdexplorer-0.0.40-py3-none-any.whl Python 3 none any Details

Total release size: 247.7 kB

Release files / pdexplorer-0.0.40.tar.gz

Download URL pdexplorer-0.0.40.tar.gz
Size 110.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e2c86b03e445b97dae4a01997b18b2a3fb7acf4bfeb7e72a19b470ddc163c215
BLAKE2b-256 checksum
How to use checksums
6c2d6e58c13db38e4f8801e4640ba49c322b56d2a14e79c6ef6853e940daa808
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.1

Release files / pdexplorer-0.0.40-py3-none-any.whl

Download URL pdexplorer-0.0.40-py3-none-any.whl
Size 137.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
da27cfccea870dec5778ed6b385368968291dcf28dbf4a890ce16f39b345e25a
BLAKE2b-256 checksum
How to use checksums
d3e13504268cf47e062093b72d94ea60f9db49686786329951d57f940979f8cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.1

Release history Release notifications | RSS feed

This release

0.0.40 This release

2 release files

0.0.39

2 release files

0.0.38

2 release files

0.0.37

2 release files

0.0.36

2 release files

0.0.35

2 release files

0.0.32

2 release files

0.0.31

2 release files

0.0.30

2 release files

0.0.29

2 release files

0.0.28

2 release files

0.0.27

2 release files

0.0.26

2 release files

0.0.25

2 release files

0.0.24

2 release files

0.0.23

2 release files

0.0.22

2 release files

0.0.21

2 release files

0.0.20

2 release files

0.0.19

2 release files

0.0.18

2 release files

0.0.17

2 release files

0.0.16

2 release files

0.0.15

2 release files

0.0.14

2 release files

0.0.13

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page