pandas Timestamp class usage details

  • 2020-06-15 09:36:44
  • OfStack

Due to the lack of information about Timestamp on the Internet and the vagueness on the official website, this article only gives a brief introduction of how to create Timestamp objects. Please refer to the documentation for details.

There are two ways to create an Timestamp object:

1. Construction method of Timestamp()


import pandas as pd
from datetime import datetime as dt
p1=pd.Timestamp(2017,6,19)
p2=pd.Timestamp(dt(2017,6,19,hour=9,minute=13,second=45))
p3=pd.Timestamp("2017-6-19 9:13:45")

print("type of p1:",type(p1))
print(p1)
print("type of p2:",type(p2))
print(p2)
print("type of p3:",type(p3))
print(p3)

Output:


('type of p1:', <class 'pandas.tslib.Timestamp'>)
2017-06-19 00:00:00
('type of p2:', <class 'pandas.tslib.Timestamp'>)
2017-06-19 09:13:45
('type of p3:', <class 'pandas.tslib.Timestamp'>)
2017-06-19 09:13:45

2. to_datetime () method


import pandas as pd
from datetime import datetime as dt

p4=pd.to_datetime("2017-6-19 9:13:45")
p5=pd.to_datetime(dt(2017,6,19,hour=9,minute=13,second=45))

print("type of p4:",type(p4))
print(p4)
print("type of p5:",type(p5))
print(p5)

Output:


('type of p4:', <class 'pandas.tslib.Timestamp'>)
2017-06-19 09:13:45
('type of p5:', <class 'pandas.tslib.Timestamp'>)
2017-06-19 09:13:45

Related articles: