- I run into an error.
- please tell me there is another way of doing this. I need to be able to dynamically select tickers.
code:
client = InfluxDBClient(url=url, token=token,org=org)
query_api = client.query_api()
tickers = ['ADMIE.AT','AEGN.AT', 'OPAP.AT']
first_ticker = tickers[0]
rest_tickers = [ x for x in tickers if x not in tickers[0] ]
line_string = "or r[\"_field\"] == \"('Adj Close' , "
def make_string(my_list, my_string):
x= []
for i in my_list:
item = (f' {my_string}"{i}"'+")")
x.append(item)
return x
string_from_rest_tickers = " ". join(make_string(tickers,line_string))
q = '''
from(bucket:"stocksdb")
|> range(start: 0, stop: now())
|> filter(fn: (r) => r._measurement == "stock_data_df")
|> filter(fn: (r) => r["_field"] == "('Adj Close','{}')" {})
|> drop(columns: ["_start", "_stop", "_measurement"])
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> limit(n:10, offset: 0)
|> yield(name: "custom-name")
'''.format(first_ticker,string_from_rest_tickers)
with client:
"""
Querying ingested data
"""
q = str(q)
"""
Query: using Pandas DataFrame
"""
df = query_api.query_data_frame(query=q)
print(df)
"""
Close client
"""
client.close()
error:
Traceback (most recent call last):
File "/home/yannis/webapp/influxdb/query_new.py", line 52, in <module>
df = query_api.query_data_frame(query=q)
File "/home/yannis/.local/lib/python3.10/site-packages/influxdb_client/client/query_api.py", line 253, in query_data_frame
_generator = self.query_data_frame_stream(query, org=org, data_frame_index=data_frame_index, params=params)
File "/home/yannis/.local/lib/python3.10/site-packages/influxdb_client/client/query_api.py", line 286, in query_data_frame_stream
response = self._query_api.post_query(org=org, query=self._create_query(query, self.default_dialect, params,
File "/home/yannis/.local/lib/python3.10/site-packages/influxdb_client/service/query_service.py", line 281, in post_query
(data) = self.post_query_with_http_info(**kwargs) # noqa: E501
File "/home/yannis/.local/lib/python3.10/site-packages/influxdb_client/service/query_service.py", line 307, in post_query_with_http_info
return self.api_client.call_api(
File "/home/yannis/.local/lib/python3.10/site-packages/influxdb_client/_sync/api_client.py", line 343, in call_api
return self.__call_api(resource_path, method,
File "/home/yannis/.local/lib/python3.10/site-packages/influxdb_client/_sync/api_client.py", line 173, in __call_api
response_data = self.request(
File "/home/yannis/.local/lib/python3.10/site-packages/influxdb_client/_sync/api_client.py", line 388, in request
return self.rest_client.POST(url,
File "/home/yannis/.local/lib/python3.10/site-packages/influxdb_client/_sync/rest.py", line 307, in POST
return self.request("POST", url,
File "/home/yannis/.local/lib/python3.10/site-packages/influxdb_client/_sync/rest.py", line 257, in request
raise ApiException(http_resp=r)
influxdb_client.rest.ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json; charset=utf-8', 'Vary': 'Accept-Encoding', 'X-Influxdb-Build': 'OSS', 'X-Influxdb-Version': 'v2.4.0', 'X-Platform-Error-Code': 'invalid', 'Date': 'Fri, 30 Sep 2022 05:43:24 GMT', 'Transfer-Encoding': 'chunked'})
HTTP response body: b'{"code":"invalid","message":"compilation failed: error @5:16-10:12: expected RPAREN, got EOF\\n\\nerror @5:144-5:167: string literal key AEGN.AT must have a value\\n\\nerror @5:144-5:156: invalid expression @5:154-5:155: \'\\n\\nerror @5:144-5:156: unexpected token for property key: ILLEGAL (\')\\n\\nerror @9:40-10:12: got unexpected token in string expression @10:12-10:12: EOF"}'