Solved

Timeseries upsert

  • 10 October 2023
  • 3 replies
  • 84 views

Badge +2

How do I perform upsert using this dataframe. I also need to associate the timeseries against a specified asset. Please advise. 

How to use this dataframe and perform associating to an asset id using this line:

    client.time_series.data.insert_dataframe(df)

 

I have a dataframe that looks like this as below:

 

DATE LP_Crude API_Meter LP_FRN_KERO_SW_CUTPT LP_KERO_DSL_SW_CUTPT LP_DSL_AGO_SW_CUTPT LP_MVG_HVGO_SW_CUTPT
1/1/2023 28.75472705 271.8662 440.9032 680.3416 928.875
1/2/2023 28.21111702 269.3863 466.2317 686.5167 924.1292
1/3/2023 27.78340123 268.8638 484.6189 684.5542 919.9917
1/4/2023 27.5781529 269.3117 506.7792 686.6708 921.9375
1/5/2023 28.03229217 268.1567 500.2208 688.2208 917.575
icon

Best answer by Håkon V. Treider 16 October 2023, 10:56

View original

3 replies

Userlevel 4
Badge

I have added some links to the documentation which has great examples for you to follow/copy/modify to your needs. If the column names of your data frame (df.columns) corresponds to the external ID of a time series, then you can...:

  1. If the time series is already created, use update (with the TimeSeriesUpdate object):
    https://cognite-sdk-python.readthedocs-hosted.com/en/latest/core_data_model.html#update-time-series
  2. If it doesn’t exist yet, add the asset link by passing asset_id when you create it (use the TimeSeries object).
    https://cognite-sdk-python.readthedocs-hosted.com/en/latest/core_data_model.html#create-time-series
  3. If you have several time series, some in need of updating and some to be created, you can use upsert:
    https://cognite-sdk-python.readthedocs-hosted.com/en/latest/core_data_model.html#upsert-time-series

 

Badge +2

Hi @Håkon V. Treider , the dataframe that I listed is the one based on which I need to create the timeseries objects. Each of those individual columns refer to the ext-id(s) and the values in each column correspond to the datapoints agains the date in the ‘date’ column. Based on this construct, how do I manage to create timeseries and add datapoints and while I create the timeseries objects, they have to be linked to a specific asset. 

Upsert doesnt give that option at all. 

Please advise.

Userlevel 4
Badge

Hi @eashwar11 . There’s two steps here:

  1. Making sure the time series exist and are linked to specific assets.
  2. Writing datapoints to these time series.

To tackle the first step, I sent you several links in my previous post. You can for example loop through df.columns and create TimeSeries objects that you then pass to time_series.create(...), something like the following:

from cognite.client.data_classes import TimeSeries

new_ts = [
TimeSeries(
name=...,
external_id=xid,
asset_id=...,
...,
)
xid for xid in df.columns
]

For step 2, you may simply use insert_dataframe once all time series have been created.

Reply