<jupyter_start><jupyter_text>AcF701 Python Bonus 2: WRDS and Postgres  Install WRDS module<jupyter_code># install wrds module using pip
!pip install wrds<jupyter_output><empty_output><jupyter_text>Connect WRDS within JupyterWRDS switched from `SAS` to `Postgres`, a rational database.You will need to learn a bit...`SQL/Postgres` language (easy).<jupyter_code>import wrds

conn = wrds.Connection()
# SELECT => pick variable(s), * for all
# FROM   => dataset name
# other SQL sytnax including, WHERE, ORDER BY, GROUP BY, and so on
stmt = "SELECT date, dji FROM djones.djdaily"

data = conn.raw_sql(stmt)
# Check data
data.head()
# Store data
data_path = "somewhere in your machine"
data.to_csv(data_path)<jupyter_output><empty_output><jupyter_text>Automate connection (only for your own machine, pls)<jupyter_code># Step 1: magic touch! create a file for storing credentials
!touch ~/.pgpass
# Step 2: enter the username and password
!echo "wrds-pgdata.wharton.upenn.edu:9737:wrds:your_username:your_password" >> ~/.pgpass
# Step 3: change mod of the file
!chmod 600 ~/.pgpass
# Step 4: now you should connect to WRDS automatically...Happy Hacking!
conn = wrds.Connection(wrds_username='your_username')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10560519902518278, 'fraction_numerical': 0.012185215272136474, 'mean_word_length': 5.96045197740113, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '136994', 'license': 'MIT', 'n_tokens_mistral': 437, 'n_tokens_neox': 412, 'n_words': 130, 'path': 'notebooks/Session_2/Episode_Bonus/minipy_bonus.ipynb', 'repo_name': 'drliangjin/little-python-book', 'size': '1231'}