Solved

Timseries linked to an asset

  • 2 August 2023
  • 3 replies
  • 50 views

Badge +2

How to fetch all the timeseries linked to an asset? I have a root asset that has 16 child assets. Each of those child has 18 timeseries objects. How do I write a code so that I can loop through this setup and read each of those timeseries objects and read the datapoint for a particular day and gather them.

 

root = client.assets.list(name='liquid_asset')
children=client.assets.list(parent_ids=[root[0].id])
for i in children:
print(i.time_series)


Result is this: <bound method Asset.time_series of <cognite.client.data_classes.assets.Asset object at 0x3468500>>


I could not iterate through the i.timeseries and read the datapoint from a very specific timeseries object named as 'PVOL'. 

Please help. 

icon

Best answer by HaydenH 2 August 2023, 10:32

View original

3 replies

Hi @eashwar11, there are a couple of ways to do this. One such way would be to use

client.time_series.list()

Inspecting the docs, we can see that the list method accepts an argument:

asset_subtree_external_ids

which can be used if you know the external ID (or ID if you use the id argument instead). If you then convert to a Dataframe with the to_pandas() method, then you can take the column of external IDs and then retrieve data of these time series to do further processing if you desire. Let me know if that helps or if further clarification is needed.

Badge +2

Thanks @HaydenH 

I tried with asset_subtree_ids. It gave the expected result. 

root = client.assets.list(name='liquid_asset')
child_assets=client.assets.list(parent_ids=[root[0].id])
list_assets = []
for list_item in child_assets:
(list_assets.append(list_item.id))

time_series_obj = (client.time_series.list(asset_subtree_ids=list_assets,limit=None))


df_timeobj = (time_series_obj.to_pandas())

df_timeobj[df_timeobj['name'].str.contains('Product_Vol',na=False)].loc[:,'id','external_id','name']]

It took around 30 seconds to pull the final table that I wanted. Hope that is transient and won't be a problem when I include this code block in cognite function. 

Userlevel 4
Badge

I could not iterate through the i.timeseries and read the datapoint from a very specific timeseries object named as 'PVOL'. 

That’s becuase .time_series is a method, not a property (i.e. you need to call it .time_series())

 

Another tip: This use case is so common that the SDK has several helper methods; here’s a suggestion assuming you know the identifier of you root asset:

root_asset = client.assets.retrieve(external_id="my-root")
all_time_series = root_asset.subtree().time_series()

The nice part of this is that the final TimeSeriesList will be de-duplicated automatically for you.

Reply