Open In App

Python | Pandas TimedeltaIndex.factorize

Last Updated : 28 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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.




# importing pandas as pd
import pandas as pd
  
# Create the TimedeltaIndex object
tidx = pd.TimedeltaIndex(start ='1 days 06:05:01.000030',
                   periods = 5, freq ='D', name ='Koala')
  
# Print the TimedeltaIndex object
print(tidx)


Output :

Now we will use the TimedeltaIndex.factorize() function to encode the tidx object.




# encode the tidx object
tidx.factorize()


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.




# importing pandas as pd
import pandas as pd
  
# Create the TimedeltaIndex object
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 the TimedeltaIndex object
print(tidx)


Output :

Now we will use the TimedeltaIndex.factorize() function to encode the tidx object.




# encode the tidx object
tidx.factorize()


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
Previous
Next
Share your thoughts in the comments

Similar Reads