Python | Pandas Timestamp.fromordinal
Last Updated :
14 Jan, 2019
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas Timestamp.fromordinal()
function return a Timestamp object when passed an ordinal, the function translate and convert it to a ts object. By definition there cannot be any tz info on the ordinal itself.
Syntax :Timestamp.fromordinal()
Parameters :
ordinal : date corresponding to a proleptic Gregorian ordinal
freq : Offset which Timestamp will have
tz : Time zone for time which Timestamp will have
Return : Timestamp
Example #1: Use Timestamp.fromordinal()
function to translate the given Timestamp object based on the passed ordinal value.
import pandas as pd
ts = pd.Timestamp(year = 2011 , month = 11 , day = 21 ,
hour = 10 , second = 49 , tz = 'US/Central' )
print (ts)
|
Output :
Now we will use the Timestamp.fromordinal()
function to convert the given Timestamp object.
ts.fromordinal(ordinal = 733829 )
|
Output :
As we can see in the output, the Timestamp.fromordinal()
function has returned a new Timestamp object based on the passed ordinal value.
Example #2: Use Timestamp.fromordinal()
function to translate the given Timestamp object based on the passed ordinal value. Also set the timezone.
import pandas as pd
ts = pd.Timestamp(year = 2009 , month = 5 , day = 31 ,
hour = 4 , second = 49 , tz = 'Europe/Berlin' )
print (ts)
|
Output :
Now we will use the Timestamp.fromordinal()
function to convert the given Timestamp object.
ts.fromordinal(ordinal = 634816 , tz = 'Asia / Calcutta' )
|
Output :
As we can see in the output, the Timestamp.fromordinal()
function has returned a new Timestamp object based on the passed ordinal value. The function has also set the timezone for the returned Timestamp object.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...