Table of Contents
- Extracting day name
- Day number of the month
- Day number of the year
- Extracting days
- Extracting weeks
- Extracting months
- Extracting quarter
- Is leap year or not
- Start of the month or end of the month?
- Video Tutorial
1 Extracting day number
To start the practical, import Pandas and read the data file. Convert the string data type in the Year column to DateTime data type with to_datetime() function. Then display the first 3 rows of the data frame using head() function as shown in figure 1.
1.1 Day number of the month
- Display the whole data frame by removing the head() function.
- The day number of the month can be extracted by passing the dt and day functions to the data frame as shown below, make sure to specify the column.
car_sales.Year.dt.day
Execute it as shown in figure 2. It can be seen the day number of the month is shown for each record.
1.2 Day number of the year
The day number of the year can be extracted by passing the dt and dayofyear functions to the data frame as shown below, make sure to specify the column.
car_sales.Year.dt.dayofyear
Then execute the code. Refer to the figure 2, It can be seen the day number of the year is shown for each record.
2 Extracting day name
In order to extract the day name (Sunday, Monday, Tuesday, etc.), the weekday_namefunction can be used. The code should be, car_sales.Year.dt.weekday_name
Execute the command as shown in figure 4. To verify the day names, you can use a calendar.
3 Extracting months
The month number of the year can be extracted by the month function along with the dt function. make sure to specify the data frame and the column.
car_sales.Year.dt.month
Then execute the code. Refer to the figure 5, It can be seen the month number of the year is shown for each record.
4 Extracting weeks
The week number of the year can be extracted by passing the dt and weekofyear functions to the data frame as shown below, make sure to specify the column.
car_sales.Year.dt.weekofyear
Then execute the code. Refer to the figure 6, It can be seen the week number of the year is shown for each record.
5 Extracting quarters
The month number of the year can be extracted by quarter function along with the dt function. make sure to specify the data frame and the column.
car_sales.Year.dt.quarter
Then execute the code. Refer to figure 7, It can be seen the quarter number of the year is shown for each record. As an example, the second record, the month is the 9th month, hence the quarter is the 3rd quarter.
6. Is leap year or not
The is_leap_yearfunction along with dt function and the data frame can be used to find out whether the recorded year is a leap year or not. Make sure to specify the column. If the value is false, that means it’s not a leap year and the value is true, otherwise. This is shown in figure 8.
The above leap year or not findings can be added to the data frame as a new column as shown in figure 9. In another word, we can add a new column that shows whether each record is a leap year or not. The new column name is “Leap_Year”.
car_sales[‘Leap_Year’] = car_sales.Year.dt.is_leap_year
7 Month start or month end?
We can find out whether it’s the month start date or month-end date using is_month_startand is_month_endfunctions. Pass these to the relevant column of the data frame along with dt function as shown in figure 10 and figure 11.
To verify this, see the 0th record. It’s September 1st. Which means it’s the start of a month. Refer the figure 10, for is_month_start function it displays True. Then refer the figure 10, for is_month_end function it displays False.