Data of date

Aditya Kumar
3 min readApr 5, 2023

--

While working with the data sets, many times we come across date and time columns. This column may contain the simple date in the day-month-year format, but there is further information hidden in it. For example, a date can tell you about the day of the week or quarter of the year.

Let's extract some information from the date column. Before playing with the date we need to check the format of the column.

Fig 1. Data frame column types

The above image shows that the date is an object type. And on the object type, we cannot extract data related data. So we need to convert the same into date format.

Fig 2:Converting object to DateTime type

Extract year: We can extract the year from the date column using pandas.Series.dt.year. This will extract the year from the date column and add it to the same data frame.

Fig 3. Extracting year from date

Extracting Month: Month details can be extracted from the date column using pandas.series.dt.month and pandas.series.dt.month_name(). This will extract the month and month name from the date and add it to the data frame.

Fig 4. Extracting the month from the date
Fig 5: Extracting month name from date column

Extracting day: We can extract the day of the month from the date column using pandas.series.dt.day.

Fig 6. Extracting day from date column

Extracting day of the week: The date can easily tell you about the day of the month, but pandas can help us to get the day of the week using pandas.Series.dt.day_of_week.

Fig 7: Extracting day of the week

Extracting day of week name: We can extract the day of week name using pandas.Series.dt.day_name.

Fig 8: Extracting Week day name

Extract Week of year: In a year we have approx 48–50 weeks. Using dt.week we can extract the number of weeks of the year.

Fig 9: Extracting weeks of the year

Extracting Quater: We have 4 quarters in a year. Using pandas.Series.dt.quarter we can extract the quarter of the year.

Fig 10: Extracting quarter from the year

Extracting day of the year: A year has 365 days and pandas.Series.dt.day_of_year allows us to find the day of the year.

Fig 11: Extracting day of the year

Pandas provides a variety of classes that can help us to extract more information from date column. Please refer to https://pandas.pydata.org/docs/reference/series.html for more details.

Hope you would have a good understanding of the date column.

--

--

Aditya Kumar
Aditya Kumar

Written by Aditya Kumar

Data Scientist with 6 years of experience. To find out more connect with me on https://www.linkedin.com/in/adityakumar529/

No responses yet