Timestamp convertion

My friend passed to me a log pulled from a device with timestamp shown below.
How to plot this in python? Do I need to convert the values of timestamp first or python can handle it?
How to interpret this kind of timestamp format?

Noise Timestamp
234 1624284672
5.8 1624284672
478 1624284674

These are most likely Unix-style timestamps, which are defined as the number of seconds after the start of the 1st January 1970. You can convert that using datetime.datetime.fromtimestamp(), which gives you a datetime object with year, month, day, hour, etc attributes.

The timestamp looks like good old UNIX time. That is seconds since the
beginning of 1970, UTC, which is the “UNIX epoch” described in the time
and date documents for times in seconds.

Quick sanity check of the first timestamp here:

1624284672:    gmt: 2021-06-21 14:11:12    local: 2021-06-22 00:11:12

Does that look like when these logs were made?

If so, you are good to go! There are the “seconds since the epoch” times
used by any of the functions which take that.

Have you got access to plotting libraries, such as pandas or matplotlib?

If you provide those timestamps as the time axis and describe it as UNIX
timestamps it will probably just work. If your libraries do not accept
UNIX timestamps, they probably accepts Python datetime objects - the
datetime module describes how to make one of these from a UNIX
timestamp.

Cheers,
Cameron Simpson cs@cskk.id.au

Thanks Cameron,

Say I have a csv file with content shown below, and I want to plot it, do I need to convert the timestamp as my x axis or python lib can already use it right away to plot graph.

Also, in my csv file, can i include all the data in just one plotting (e.g. data for John, Peter and Mark) using different line colors as my legend?

import pandas as pd
import matplotlib.pyplot as plt

read data from csv file

data = pd.read_csv(r’C:\Users\jeremiah\PycharmProjects\pythonProject\venv\jeremiah.csv’,encoding=‘utf-8’).fillna(0)
x=data[‘Jitter’].iloc[0:2041].values
print(x)
fig=plt.figure()
ax=fig.add_subplot(111)
ax.set_title(“Plot of Jitter for John”)
ax.set_xlabel(“Time Samples”)
ax.set_ylabel(“Data”)
ax.plot(x,c=‘r’,label=“Jitter”)
leg = ax.legend()
plt.show()

Example:
csv file content:
Name Jitter Latency Timestamp
John 0.073233 15.612933 1624284672
John 0.823452 12.63276 1624284673
. . . .
. . . .
Peter 0.43645 11.42355 1624284674
. . . .
Mark 0.23454 15.7389 1624284675

python code:
import pandas as pd
import datetime

data = pd.read_csv(r’C:\Users\juan\PycharmProjects\pythonProject\venv\mycsv.csv’)

timestamp = data[‘Timestamp’]

mytime = datetime.datetime.fromtimestamp(timestamp)

print(timestamp)
print(mytime)

ex. timestamp column
1624284672
1624284672
1624284673

I am receiving an error message if I passed the “Timestamp” column values to:
mytime = datetime.datetime.fromtimestamp(timestamp)

but if i do: i dont receive errors and it shows the converted time/date
mytime = datetime.datetime.fromtimestamp(1624284672)

How i can convert the “Timestamp” column using datetime.datetime.fromtime?

Thanks

Can you show us the error message you get, or is it a secret?

It really helps us to see what the error message is, but if I had to
guess, my guess is that you might be getting something like this:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required (got type str)

If that is the error, then it tells you what the problem is. You have a
string, but you need an integer.

Do you know how to turn a string like “1624284672” into an integer
like 1624284672?

You use the int() function:

mytimestamp = "1624284672"  # a str
mytimestamp = int(mytimestamp)  # now an int
datetime.datetime.fromtimestamp(mytimestamp)
# returns datetime.datetime(2021, 6, 22, 0, 11, 12)

If my guess is wrong, and you have some other error, I’m out of guesses.
I would have to see the actual error to have any hope of solving the
problem.

sharing you with error message

ex. timestamp column in csv file
1624284672
1624284672
1624284673

python code:
import pandas as pd
from matplotlib import pyplot as plt
import datetime

data = pd.read_csv(r’C:\Users\john\PycharmProjects\pythonProject\venv\olifile.csv’)

mytimestamp = data[‘Timestamp’]
time = int(mytimestamp)
mytime = datetime.datetime.fromtimestamp(time)

print(mytimestamp)
print(mytime)

error:
Traceback (most recent call last):
File “C:\Users\john\PycharmProjects\pythonProject\venv\time_conv.py”, line 8, in
time = int(mytimestamp)
File “C:\Users\john\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\series.py”, line 185, in wrapper
raise TypeError(f"cannot convert the series to {converter}")
TypeError: cannot convert the series to <class ‘int’>

Hi Oliver,

Okay, that error message is very helpful, or at least it will be very
helpful to people who know Pandas better than me.

I think what is happening is that your line:

mytimestamp = data['Timestamp']

makes mytimestamp a dataframe representing a column.

So you can’t convert a dataframe into an int, and the call:

int(mytimestamp)

fails with TypeError.

I think what you can do is work with one value at a time:

for value in data['Timestamp']:
    mytimestamp = int(value)
    mytime = datetime.datetime.fromtimestamp(mytimestamp)
    print(mytimestamp, mytime)

but that’s just a guess.

Hopefully somebody else who knows pandas well will speak up.

thanks Steven, question, do I really need to convert timestamp of this value 1624284673, before I can plot it as my x-axis?