下载wrds表并上传到postgresql,将sas文件上传到pg
wrdstopg的Python项目详细描述
WRDS到PG的迁移
此软件有两个功能:
- 从wrds下载表格并上传到pg.
- 将SAS文件(
*.sas7dbat
)上载到pg.
1.Python
该软件使用python 3,依赖于pandas、sqlalchemy和paramiko。
2.WRDS设置
用wrds_id=your_wrds_id
设置WRDS_ID
,否则软件将从os环境变量中grep。
要使用公钥身份验证访问wrd,请遵循提示here。首先设置一个公钥,然后将该密钥从终端复制到wrds服务器。
(注意,此代码假定您的主目录中有一个目录.ssh
。如果没有,请通过ssh登录wrds,然后键入mkdir ~/.ssh
来创建此文件。)
下面是创建密钥并将其发送到wrds的代码。
ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub | ssh your_wrds_id@wrds-cloud.wharton.upenn.edu "cat >> ~/.ssh/authorized_keys"
在设置我的密钥时使用空密码短语,以便脚本可以在没有用户干预的情况下运行。
3.PG设置
如果您将PGHOST
、PGDATABASE
、PGUSER
设置为环境变量,则软件可以对它们进行grep。否则,用户在使用wrds_udpate()
时需要指定它们。默认的PGPORT
是5432
。
需要两个变量table
和schema
。
4.表格设置
要调整表格,请指定以下变量:
fix_missing
:设置为True
以修复缺少的值。默认值是False
。
fix_cr
:设置为True
以修复字符。默认值是False
。
drop
:添加要删除的列名。例如,drop="id name"
将删除列id
和name
。
^ {CD24>}:加入最大数量的观测值。例如,obs=10
将从表中导出前10行。
rename
:重命名列。例如,rename="fee=mngt_fee"
将fee
重命名为mngt_fee
。
force
:设置为True
以强制更新。默认值是False
。
5。上载SAS文件
该软件还可以直接上传SAS文件到PG。您需要有本地SAS才能使用此功能。
使用fpath
指定文件路径。
6.示例
这里有一些例子。
fromwrdstopgimportwrdstopg# 1. Download crsp.mcti from wrds and upload to pg as crps.mcti# Simplest versionwrdstopg.wrds_update(table="mcti",schema="crsp")# Tailor table to your needswrdstopg.wrds_update(table="mcti",schema="crsp",host=your_pghost,dbname=your_pg_database,fix_missing=True,fix_cr=True,drop="b30ret b30ind",obs=10,rename="caldt=calendar_date",force=True)# 2. Upload test.sas7dbat to pg as crsp.mctiwrdstopg.wrds_update(table="mcti",schema="crsp",fpath="your_path/test.sas7dbat")
7.报告错误
作者:Ian Gow,ian.gow@unimelb.edu.au
撰稿人:张靖宇,jingyu.zhang@chicagobooth.edu