简体   繁体   中英

Faster way to iterate in numpy / pandas?

I have a big portfolio of bonds and I want to create a table with days as index, the bonds as columns and the notional of the bonds as values.

I need to put at 0 the rows before the starting date and after the terminating date of each bond.

Is there a more efficient way than this:

[[np.where( (day>=bonds.inception[i]) & 
(day + relativedelta(months=+m) >= bonds.maturity[i] ) & 
(day <= bonds.maturity[i]), 

bonds.principal[i],

0)   

for i in range(bonds.shape[0])] for day in idx_d]

input example:

id nom inception maturity
38 200 22/04/2022 22/04/2032
87 100 22/04/2022 22/04/2052

output example:

day 38 87
21/04/2022 0 0
22/04/2022 100 200

The solution below still requires a loop. I don't know if it's faster, or whether you find it clear, but I'll offer it as an alternative.

Create an example dataframe (with a few extra bonds for demonstration purposes):

import pandas as pd

df = pd.DataFrame({'id': [38, 87, 49, 51, 89], 
                   'nom': [200, 100, 150, 50, 250],
                   'start_date': ['22/04/2022', '22/04/2022', '01/01/2022', '01/05/2022', '23/04/2012'],
                   'end_date': ['22/04/2032', '22/04/2052', '01/01/2042', '01/05/2042', '23/04/2022']})
df['start_date'] = pd.to_datetime(df['start_date'])
df['end_date'] = pd.to_datetime(df['end_date'])
df = df.set_index('id')
print(df)

This then looks like:

id nom start_date end_date
38 200 2022-04-22 00:00:00 2032-04-22 00:00:00
87 100 2022-04-22 00:00:00 2052-04-22 00:00:00
49 150 2022-01-01 00:00:00 2042-01-01 00:00:00
51 50 2022-01-05 00:00:00 2042-01-05 00:00:00
89 250 2012-04-23 00:00:00 2022-04-23 00:00:00

Now, create a new blank dataframe, with 0 as the default value:

new = pd.DataFrame(data=0, columns=df.index, index=pd.date_range('2022-04-20', '2062-04-22'))
new.index.rename('day', inplace=True)

Then, iterate over the columns (or index of the original dataframe), selecting the relevant interval and set the column value to the relevant 'nom' for that selected interval:

for column in new.columns:
    sel = (new.index >= df.loc[column, 'start_date']) & (new.index <= df.loc[column, 'end_date'])
    new.loc[sel, column] = df.loc[df.index == column, 'nom'].values
print(new)

which results in:

day 38 87 49 51 89
2022-04-20 00:00:00 0 0 150 50 250
2022-04-21 00:00:00 0 0 150 50 250
2022-04-22 00:00:00 200 100 150 50 250
2022-04-23 00:00:00 200 100 150 50 250
2022-04-24 00:00:00 200 100 150 50 0
...
2062-04-21 00:00:00 0 0 0 0 0
2062-04-22 00:00:00 0 0 0 0 0

[14613 rows x 5 columns]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM