Aizzaac
Aizzaac

Reputation: 3318

How to get a timestamp (for Time Series analysis) when data is in seconds, using python?

I have a dataset (see image 1) that I want to analyze for anomalies.

The story of the dataset is the following: I measured temperature (TPU, CPU), memory (MemUsed) and time (Time) for each inference (an image classification task). I also made a cumulative sum of the 'Time' column in order to get the time that the whole process of classifying 70000 images will take. This is around 7000 seconds.

The following code is what I get when trying to get a Timestamp (aka '_time'). And in Image 2 you will see how does it look.

#Return the index with frequency (RangeIndex to DateTimeIndex)
df1_MAX['_time']=pd.to_datetime(df1_MAX['TimeTotal'])#string to DatetimeIndex
df1_MAX=df1_MAX.set_index('_time')
df1_MAX

As I am working with data in seconds, how can I get a proper Timestamp? what format do I need to use?

Thank you

Image 1

enter image description here

Image 2

enter image description here

------------------EDIT-------------- Using timedelta64

enter image description here

---------------EDIT------------

I changed 'TimeTotal' to ms. And also put 'timedelta64[]' in ms.

df1_MAX['_time']= pd.to_datetime("2020-07-06 10:53:00")+df1_MAX['TimeTotal'].astype('timedelta64[ms]')
df1_MAX=df1_MAX.set_index('_time')
df1_MAX

enter image description here

Upvotes: 0

Views: 687

Answers (1)

wwnde
wwnde

Reputation: 26676

Can make it a time delta then

df['TIME']= df['Time'].astype('timedelta64[s]')

If you wanted to create a datetime stamp, say you began at 2021-07-06 10:53:02. Just add the timedelta to the start datettime.

Data

df = pd.DataFrame({"Time": [121.83,101.22],"score": [1,2],"Label": ["trimaran", "trimaran"]})

Solution

 df['DateTime']= pd.to_datetime("2021-07-06 10:53:02")+df['Time'].astype('timedelta64[s]')

Outcome

   Time      score     Label            DateTime
0  121.83      1  trimaran  2021-07-06 10:55:03
1  101.22      2  trimaran  2021-07-06 10:54:43

Upvotes: 1

Related Questions