Add query_dataframe function to be able to then use the labels in pandas#8
Add query_dataframe function to be able to then use the labels in pandas#8n-peugnet wants to merge 1 commit intodcoles:masterfrom
Conversation
|
Thanks for the PR! I'll need to think a little about this, since the Query returning instant vector
|
Ah, yes you are right, I didn't pay attention to this and thought the basic query always returned a vector. 😅
This is strange, considering I'm calling the For the rest of your reply, my bad, I was not thinking about these types of query. But I am completely open to another API, provided that I am able to get this kind of results for the basic vector query, as you showed it:
|
Looks like it was a copy-paste bug in my test script. Sorry to cause confusion. 🙇 I think I now understand what's going on and how I tried to tackle it when first writing the library. Potentially the right tool would have been a Query returning instant vector
|
|
This would be the alternate def to_pandas2(data: dict) -> pd.Series:
"""Convert Prometheus data object to Pandas Series."""
result_type = data['resultType']
if result_type == 'vector':
index_frame = pd.DataFrame(
r['metric'] | {'@': pd.Timestamp(r['value'][0], unit='s')}
for r in data['result'])
return pd.Series(
data=(np.float64(r['value'][1]) for r in data['result']),
index=pd.MultiIndex.from_frame(index_frame)
)
elif result_type == 'matrix':
index_frame = pd.DataFrame(
r['metric'] | {'@': pd.Timestamp(v[0], unit='s')}
for r in data['result'] for v in r['values'])
return pd.Series(
data=(np.float64(v[1]) for r in data['result'] for v in r['values']),
index=pd.MultiIndex.from_frame(index_frame)
)
elif result_type == 'scalar':
return pd.Series(
data=[np.float64(data['result'][1])],
index=[pd.Timestamp(data['result'][0], unit='s')])
elif result_type == 'string':
return pd.Series(
data=[data['result'][1]],
index=[pd.Timestamp(data['result'][0], unit='s')])
else:
raise ValueError('Unknown type: {}'.format(result_type)) |
I just tried it and works nicely for my use case. I can simply call But I think directly replacing |
|
I've created an updated |


This is especially useful to use the labels later in plots, for exemple to group by configuration and such.