Setup - Authenticate to WRDS#
Connect through Python#
To connect to the WRDS API through Python, install the wrds package. From the command line, type:
# install the `wrds` package.
! pip install wrds
Next, import the wrds package in Python.
# packages
import wrds
import pandas as pd
You can establish a connection with the WRDS server by inserting your username in the following Python command. If you have already created a .pgpass file, this line will look for the file that contains your username and corresponding password.
conn = wrds.Connection(wrds_username='best-user-ever')
#db = wrds.Connection()
# AT THIS POINT, EXPECT A DUO PUSH
Loading library list...
Done
Note that the first time you perform this step, it will trigger two-factor-authentication. Please follow the instructions here to set this up: https://wrds-www.wharton.upenn.edu/pages/about/log-in-to-wrds-using-two-factor-authentication/
You can test your connection with:
df = conn.get_table(library="crsp", table="dsf", obs=10)
# print column names
df.columns
Index(['permno', 'namedt', 'nameenddt', 'shrcd', 'exchcd', 'siccd', 'ncusip',
'ticker', 'comnam', 'shrcls', 'permco', 'hexcd', 'cusip', 'st_date',
'end_date', 'namedum'],
dtype='object')
# print number of rows
len(df)
10
Sample python setup code can be found here: Python Setup Code
Connect through R#
To connect to the WRDS through R, you’ll need to install the RPostgres library.
(Note that the lines in the cell below are used to call R in this jupyter notebook.)
import rpy2
%load_ext rpy2.ipython
The rpy2.ipython extension is already loaded. To reload it, use:
%reload_ext rpy2.ipython
Within R, type:
%%R
#install.packages("RPostgres")
NULL
Next, load the library.
%%R
library(DBI)
library(RPostgres)
Finally, create a connection with the following:
%%R
wrds <- dbConnect(
Postgres(),
host = 'wrds-pgdata.wharton.upenn.edu',
port = 9737,
dbname = 'wrds',
sslmode = 'require',
user = 'best-user-ever'
)
# AT THIS POINT, EXPECT A DUO PUSH
You can test your connection with:
%%R
res <- dbSendQuery(wrds, "select * from crsp.dsf")
data <- dbFetch(res, n=10)
%%R
# print column names
names(data)
[1] "cusip" "permno" "permco" "issuno" "hexcd" "hsiccd" "date"
[8] "bidlo" "askhi" "prc" "vol" "ret" "bid" "ask"
[15] "shrout" "cfacpr" "cfacshr" "openprc" "numtrd" "retx"
%%R
# print number of rows
nrow(data)
[1] 10
Sample R setup code can be found here: R Setup Code
Lab 1#
Lab 1 - Create your pgpass file and connect to WRDS
Either in R or Python:
Create and set permissions on your own hidden
pgpassfile.Establish a
wrdsconnection and grab the first 10 rows of data from “company” table in Compustat (“comp”)Save the results as a csv file in your home directory and version control your file with git.