我正在使用pytreasurydirect提取数据,我想查询每个唯一的cusip,然后附加它们并创建一个pandas dataframe表。我在生成熊猫数据框时遇到困难。我相信这是因为数据的unicode结构。
import pandas as pd
from pytreasurydirect import TreasuryDirect
td = TreasuryDirect()
cusip_list = [['912796PY9','08/09/2018'],['912796PY9','06/07/2018']]
for i in cusip_list:
cusip =''.join(i[0])
issuedate =''.join(i[1])
cusip_value=(td.security_info(cusip, issuedate))
#pd.DataFrame(cusip_value.items())
df = pd.DataFrame(cusip_value, index=['a'])
td = td.append(df, ignore_index=False)
来自pytreasurydirect的数据示例:
Index([u'accruedInterestPer100', u'accruedInterestPer1000',
u'adjustedAccruedInterestPer1000', u'adjustedPrice',
u'allocationPercentage', u'allocationPercentageDecimals',
u'announcedCusip', u'announcementDate', u'auctionDate',
u'auctionDateYear',
...
u'totalTendered', u'treasuryDirectAccepted',
u'treasuryDirectTendersAccepted', u'type',
u'unadjustedAccruedInterestPer1000', u'unadjustedPrice',
u'updatedTimestamp', u'xmlFilenameAnnouncement',
u'xmlFilenameCompetitiveResults', u'xmlFilenameSpecialAnnouncement'],
dtype='object', length=116)
答案 0 :(得分:0)
我认为您想定义一个这样的函数:
def securities(type):
secs = td.security_type(type)
keys = secs[0].keys() if secs else []
seri = [pd.Series([sec[key] for sec in secs]) for key in keys]
return pd.DataFrame(dict(zip(keys, seri)))
然后使用它:
df = securities('Bond')
df[['cusip', 'issueDate', 'maturityDate']].head()
例如,获得这样的结果(TreasuryDirect返回许多附加列):
cusip issueDate maturityDate
0 912810SD1 2018-08-15T00:00:00 2048-08-15T00:00:00
1 912810SC3 2018-07-16T00:00:00 2048-05-15T00:00:00
2 912810SC3 2018-06-15T00:00:00 2048-05-15T00:00:00
3 912810SC3 2018-05-15T00:00:00 2048-05-15T00:00:00
4 912810SA7 2018-04-16T00:00:00 2048-02-15T00:00:00
至少今天是今天的结果。结果会随着时间的流逝而变化,因为债券已经发行并且很成熟。请注意,每个issueDates
有多个cusip
。
最后,根据TreasuryDirect网站(https://www.treasurydirect.gov/webapis/webapisecurities.htm),可能的安全性类型为:Bill
,Note
,Bond
,CMB
,{{1 }},TIPS
。