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?
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.
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.
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
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’>