@@ -151,13 +151,53 @@ def fetch_interval(time_start, time_end, step):
151151 return list_values
152152 @staticmethod
153153 def metricsToDataframe (metric , values ):
154+ """
155+ Convert raw metric records into a normalized, time-indexed DataFrame.
156+
157+ The function performs the following steps:
158+
159+ 1. Create a DataFrame from the raw records.
160+ 2. Rename columns to ["time [s]", <metric title>].
161+ 3. Convert the time column to integer.
162+ 4. Normalize time so that it starts at 0 seconds.
163+ 5. Set the time column as index.
164+ 6. Convert metric values to float.
165+
166+ :param metric: Dictionary containing metric metadata.
167+ Must contain the key ``'title'`` which will be
168+ used as the column name for the metric values.
169+ :type metric: dict
170+
171+ :param values: Iterable of records representing metric measurements.
172+ Each record must contain two elements:
173+ (timestamp, metric_value).
174+ :type values: iterable
175+
176+ :return: A pandas DataFrame indexed by normalized time (seconds)
177+ containing one float column with metric values.
178+ :rtype: pandas.DataFrame
179+ """
180+ df = pd .DataFrame .from_records (values )
181+ df .columns = ['time [s]' , metric ['title' ]]
182+ # Convert time column to integer
183+ df .iloc [:, 0 ] = pd .to_numeric (df .iloc [:, 0 ], errors = "coerce" ).astype ("Int64" )
184+ # Normalize time to start at 0
185+ minimum = df .iloc [:, 0 ].min ()
186+ df .iloc [:, 0 ] = df .iloc [:, 0 ] - minimum
187+ # Set time as index
188+ df = df .set_index (df .columns [0 ])
189+ # Convert metric values to float
190+ df .iloc [:, 0 ] = pd .to_numeric (df .iloc [:, 0 ], errors = "coerce" )
191+ return df
192+ def __OLD_metricsToDataframe (metric , values ):
154193 df = pd .DataFrame .from_records (values )
155194 df .columns = ['time [s]' , metric ['title' ]]
156195 df .iloc [0 :,0 ] = df .iloc [0 :,0 ].map (int )
157196 minimum = df .iloc [0 :,0 ].min ()
158197 df .iloc [0 :,0 ] = df .iloc [0 :,0 ].map (lambda x : x - minimum )
159198 df = df .set_index (df .columns [0 ])
160199 df .iloc [0 :,0 ] = df .iloc [0 :,0 ].map (float )
200+ #df.iloc[:, 0] = pd.to_numeric(df.iloc[:, 0], errors="coerce")
161201 return df
162202 @staticmethod
163203 def saveMetricsDataframe (filename , df ):
0 commit comments