Hi
I have a final computed dataframe that looks like this. sample set alone listed here (15 rows)
There are 570 records. I need to create timeseries for sumprod and derivedYields columns as per the value in Yieldcode. I have written the code and it is running and timeseries is getting created. But it is running slow. How to make it get done faster?
Any best method? Please advise
sumprod | Yieldcode | Rank | Group | derivedYields | MBBLD Vol | LP-Class | ||
1 | 532 | 0.009636 | VBALFG1 | 1 | VBAL | 0.009636 | 3.524946 | FG1 |
2 | 533 | 0 | VBALLP1 | 2 | VBAL | 0 | 0 | LP1 |
3 | 534 | 0.210269 | VBALFN1 | 3 | VBAL | 0.218807 | 80.0382 | FN1 |
4 | 535 | 0.037774 | VBALLW1 | 4 | VBAL | 0 | 0 | LW1 |
5 | 536 | 0.048263 | VBALSK1 | 5 | VBAL | 0.094867 | 34.70158 | SK1 |
6 | 537 | 0.017368 | VBALHK1 | 6 | VBAL | 0 | 0 | HK1 |
7 | 538 | 0.128149 | VBALSR1 | 7 | VBAL | 0.142624 | 52.17094 | SR1 |
8 | 539 | 0.014722 | VBALDS1 | 8 | VBAL | 0 | 0 | DS1 |
9 | 540 | 0.034342 | VBALAG1 | 9 | VBAL | 0.034588 | 12.65213 | AG1 |
10 | 541 | 0.00965 | VBALLV1 | 10 | VBAL | 0.00965 | 3.529802 | LV1 |
11 | 542 | 0.12273 | VBALMV1 | 11 | VBAL | 0.151158 | 55.29262 | MV1 |
12 | 543 | 0.028428 | VBALSV1 | 12 | VBAL | 0 | 0 | SV1 |
13 | 544 | 0.042119 | VBALHV1 | 13 | VBAL | 0.077783 | 28.45268 | HV1 |
14 | 545 | 0.035664 | VBALXV1 | 14 | VBAL | 0 | 0 | XV1 |
15 | 546 | 0.260887 | VBALVR1 | 15 | VBAL | 0.260887 | 95.43076 | VR1 |
# Function to create or update timeseries and datapoints
def create_or_update_timeseries_and_datapoint(yieldcode, column_name, value):
timeseries_external_id = f"ts-YT-LP-{yieldcode}-{column_name.lower()}"
existing_timeseries = client.time_series.retrieve(external_id=timeseries_external_id)
if not existing_timeseries:
# Create the timeseries if it doesn't exist
ts = TimeSeries(asset_id=1186599902844586,external_id=timeseries_external_id,name=f"ts-YT-LP-{yieldcode}-{column_name}",is_string=False)
timeseries = client.time_series.create(time_series=ts)
else:
timeseries = existing_timeseries
# Prepare datapoint
datapoint = [
(datetime.datetime.utcnow(),value),
]
# Store the datapoint
client.time_series.data.insert(id=timeseries.id,datapoints=datapoint)
# Loop through the DataFrame and create/update timeseries and datapoints
for index, row in yld_lp_final.iterrows():
yieldcode = row['Yieldcode']
create_or_update_timeseries_and_datapoint(yieldcode, 'vol_sprod', row['sumprod'])
create_or_update_timeseries_and_datapoint(yieldcode, 'derived_yld', row['derivedYields'])