Python | Pandas TimedeltaIndex.factorize
Last Updated :
28 Dec, 2018
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 TimedeltaIndex.factorize()
function encode the elements of the given TimedeltaIndex object as an enumerated type or categorical variable.
Syntax : TimedeltaIndex.factorize(sort=False, na_sentinel=-1)
Parameters :
sort : Sort by values
na_sentinel : Value to mark “not found”
Return : labels : the indexer to the original array
Example #1: Use TimedeltaIndex.factorize()
function to encode the elements of the given TimedeltaIndex objects.
import pandas as pd
tidx = pd.TimedeltaIndex(start = '1 days 06:05:01.000030' ,
periods = 5 , freq = 'D' , name = 'Koala' )
print (tidx)
|
Output :
Now we will use the TimedeltaIndex.factorize()
function to encode the tidx object.
Output :
As we can see in the output, the TimedeltaIndex.factorize()
function has encoded the elements of the tidx object. It has assigned a unique code to each element.
Example #2: Use TimedeltaIndex.factorize()
function to check if the elements contained in the two given TimedeltaIndex objects are same or not.
import pandas as pd
tidx = pd.TimedeltaIndex(data = [ '1 days 02:00:00' , '1 days 06:05:01.000030' ,
'1 days 02:00:00' , '1 days 02:00:00' , '21 days 06:15:01.000030' ])
print (tidx)
|
Output :
Now we will use the TimedeltaIndex.factorize()
function to encode the tidx object.
Output :
As we can see in the output, the TimedeltaIndex.factorize()
function has encoded the elements of the tidx object. It has assigned a unique code to each element.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...