I'm learning Python and it has gone relatively well so far. However I have run into an obstacle.
I have a CSV file (336 rows and 2657 columns) which contains daily data of spot rates of different instruments of different maturities:
I also have a function called importdata, which imports the data for one instrument, say all the 3M USD, at one one date, say on 03/09/2021 (m/d/y). name is the instrument (so 3M USD):
def importdata (fileloc, date, name, max_maturity=None):
'Imports data from a given location, date and name'
data = pd.read_csv(fileloc) # file location
result = data[ (data['date']) == date] # getting the date of the curve
data = result.loc[:, result.columns.str.startswith(name)] # getting the curve wanted at the date
data = data.T # Transposing the data
data = data.reset_index()
data.columns = ['maturity','spot rate'] # renaming columns
data['maturity'] = data.maturity.str.rsplit(n=1).str[-1]
if max_maturity:
data = data.iloc[:data.loc[data.maturity.str.contains(max_maturity,na=False)].index[0]+1]
return data
An example output of importdata of all the 3M USD instruments at the 9th March 2021:
maturity
0 1Y 0.188
1 2Y 0.245
2 3Y 0.431
3 4Y 0.672
4 5Y 0.892
5 6Y 1.083
6 7Y 1.2429999999999999
7 8Y 1.369
8 9Y 1.474
9 10Y 1.561
10 12Y 1.699
11 15Y 1.8259999999999998
12 20Y 1.9240000000000002
13 25Y 1.96
14 30Y 1.975
This dataframe would then be used as an input into another function which takes the above data and calculates forward rates. I want to progress this further by being able to run importdate for each date in one go by looping over the dates. So far, importdata I would have to change the dates manually, and as there are 2657 dates, this would take a long time.
I know I have to create a for loop to loop over the dates, but I am not sure how to put it as as input to importdata so that it can run for all the dates.
for dt in all_data['date']:
print (dt)
I'd be grateful for any help.
