In this article, we will cover pandas DateTime features and offer insights into their need while working with time series datasets in python.
Python is among the top five programming languages in terms of usage across the globe. About 44% of the world’s developers use it regularly for data science-related tasks. And it is the vast variety of python libraries that need to be credited for this. Pandas is one such data analysis library.
It is purely written in C or python, making way for highly optimized back-end source code. Moreover, python has clear syntax and a low learning curve, ideal for beginners. One can implement anything from micro-projects to macro enterprises with the knowledge of this language.
Python developers who have an adequate level of knowledge and experience with the Pandas library are also in demand for several data science jobs, including data analyst, business analyst, and machine learning engineer. These career trajectories require candidates to be familiar with statistics, big data analytics, predictive analytics (using python), visualization, etc.
Engineering students interested in analytics can, therefore, go for specialized degrees that emphasize these skills. You can either pursue short-term certifications like the PG Diploma in Data Science from IIIT-Bangalore or consider joining globally accredited programs, such as M.Sc. Data Science from Liverpool John Moores University (LJMU), England.
Now that we’ve given you a brief background of the relevance of python and pandas in the modern-day tech field, let’s begin our step-by-step tutorial on pandas DateTime.
Explaining DateTime Variables
You are likely to encounter DateTime in the intermediate stage of learning python, say when you are working on a project. Suppose you have to implement an e-commerce project that requires you to strategize about the supply chain pipeline. This would include figuring out the time for shipping orders, the number of days for delivery, among other things.
If you are not familiar with the date and time components in Python, this data science aspect of this problem can be tough to crack for a novice. On the other hand, if you know how to handle these features, you can gather deep insights from almost any dataset.
For the uninitiated, the date class in python deals with the dates from the Gregorian calendar. This class accepts “year, month, and day” as integer arguments. Whereas, the time class comprises integer arguments up to microseconds.
Here is an overview of python’s DateTime variable, along with the Pandas function, to get you started!
Working with DateTime in Python
Consider the example statements given below to understand how to create a date object of the DateTime class in python.
from datetime import date
d1 = date (2021,2,23)
Now, let us extract some other features, such as day, month, and year, from the date object created above. We will do it using the current local day date object, which involves using the today() function.
# present day date
d1 = date.today()
print (‘Day : ‘ , d1.day)
print( ‘Month : ‘ , d1.month)
print( ‘Year : ‘ ,d1.year)
Returned DateTime object
Day : 23
Month : 2
Year : 2021
Another class of the DateTime module that accepts integer values and returns an object is time. Let us look at how it’s done in python.
from datetime import time
t1 = (12,20,12,40)
As you can see, the above time object is down to the microseconds. So, you can now extract time attributes like the hour, minute, second, and microsecond from the object.
You can repeat the same thing for seconds and microseconds.
Here are some other methods that you would find useful:
- replace(): To update old dates.
- weekday(): To return an integer value for any day of the week; Monday is 0 and Sunday is 6.
- isoweekday(): For integer values of a weekday that are between 1 to 7.
- isocalendar(): To slice the value of the day of the ‘year’ from a given dataset.
- isleap(): To check whether it is a leap year.
- fromisoformat(): To convert string form in ISO format into a DateTime object.
- isoformat(): To generate ISO format date from the DateTime object.
- format(): To define your unique format.
Now that you have understood how to create DateTime objects in python, let’s see how the Pandas library supports them.
Pandas to_datetime Example
With pandas, you can perform a variety of data analysis tasks, especially with python DateTime objects. Some of the prominent methods include to_datetime(). Here is how you handle it:
- With the pandas to_datetime method, you can convert string format date and time into DateTime objects
date = pd.to_datetime( ’24th of April, 2020′)
Can you notice something strange here? The object returned by pandas to_datetime is not the same. It is a Timestamp instead of a DateTime object. This is how the Pandas library returns an object; timestamp is its equivalent of python’s DateTime feature.
The Need for DateTime
There are several real-life scenarios where information is gathered over a period, allowing you to extract the date and time attributes to understand a particular problem. For instance, you want to analyze your reading habits. You can dig up your patterns to deconstruct whether you prefer reading during weekends or weekdays, at night or in the morning, and so on. Then, you can accumulate all the interesting books and articles you want to read in a month and organize your schedule.
With this, we have given you a ‘how to’ summary on handling date-time manipulation in python as well as pandas DateTime. We hope you practice what we learned in this article and master the art of working with time-series datasets!
If you’re interested in learning more about Python, its various libraries, including Pandas, and its application in data science, check out IIIT-B & upGrad’s PG Diploma in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.