Python Pandas Date & Time
Returns datetime object by using various inputs including Timestamp.
Timestamp : time elapsed since 1st Jan 1970 to present date or any other date. This is given in seconds ( elapsed )Cast dtype to a datetime with options.
Generate timestamp by using any date and time
Parameters:
ts_input : Value to be converted to Timestamp
freq : str : offset
tz : Time Zone ( Example 'Asia/Kolkata')
unit : Unit used . ( See example below )
year, month, day : Int value
hour, minute, second, microsecond : integer
Examples
Generate Timestamp in seconds by using this tool. . Use the value to create datetime object.
import pandas as pd
td=pd.Timestamp(1587099167, unit='s',tz='Asia/Kolkata')
print(td)
print(td.value)
Output
2020-04-17 10:22:47+05:30
1587099167000000000
We have used units='s' and time zone as tz='Asia/Kolkata' in above example.
Using year month day etc as input
import pandas as pd
td=pd.Timestamp(year=2020,month=12,day=23 , hour=18,minute=20,second=5)
print(td.day)
print(td.asm8)
print(td.dayofweek)
print(td.daysinmonth)
Output
23
2020-12-23T18:20:05.000000000
2
31
Using any date and time
import pandas as pd
td=pd.Timestamp('2020-12-23 18:20:05')
print(td.day)
print(td.asm8)
print(td.weekday())
print(td.dayofweek)
print(td.dayofyear)
Output
23
2020-12-23T18:20:05.000000000
2
2
358
1608747605000000000
5
1608747605.0
Present timestrap
import pandas as pd
td=pd.Timestamp('now')
print(td.now('Asia/Kolkata'))
print(td.value)
print(td.second)
print(td.timestamp())
Output
2020-04-19 10:12:42.476310+05:30
1587291162476310000
42
1587291162.47631
datetime to Timestamp
dt_my = pd.date_range('2020-12-18 18:15:05', periods=4, freq='D')
#print(dt_my)
df=dt_my - pd.Timestamp("1970-01-01")
print(df.astype(int)/1000000000)
Output
Float64Index([1608315305.0, 1608401705.0, 1608488105.0, 1608574505.0], dtype='float64')
« Pandas date& time
date_range()
period_range()
strftime()
← Subscribe to our YouTube Channel here