python pandas extracting day of year weekof year leap year from date columns

Table of Contents

  1. Extracting day name
    1. Day number of the month
    2. Day number of the year
  2. Extracting days
  3. Extracting weeks
  4. Extracting months
  5. Extracting quarter
  6. Is leap year or not
  7. Start of the month or end of the month?
  8. 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.

Figure 1: Importing the data set

1.1  Day number of the month

  1. Display the whole data frame by removing the head() function.
  2. 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.

Figure 2: Day number of the month

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. 

Figure 3: Day number of the year

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.

Figure 4: The day names

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. 

Figure-5

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. 

Figure 6: Week of the year

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.

Figure 7: Quarter of the year

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.

Figure 8: Finding whether the year is a leap year or not

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

Figure 9: Adding a new column which displays whether it’s a leap year or not

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.

Figure 10: Using is_month_start function
Figure 11: Using is_month_end function

Video Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *