26 Oct Pandas – Date Time
In this lesson, learn how to work with the Date Time operations in Pandas. Let us first see how to get the current date and time, then we will check for leap year, last day of the month, week, etc. The following examples are covered:
- Get the current date and time
- Get the day of the week
- Get the day of the year
- Get the number of days in a month
- Check if the year is a leap year
- Check if the date is the last day of the month
- Check if the date is the first day of the month
- Check if the date is the last day of the year
- Check if the date is the first day of the year
Get the current date and time
The Timestamp.now() method is used in Python Pandas to get the current date and time in the local timezone:
1 2 3 4 5 |
import pandas as pd print("Current Date and Time\n",pd.Timestamp.now()) |
Output
1 2 3 4 |
Current Date and Time 2023-10-25 17:09:20.638664 |
Get the day of the week
To get the day of the week, use the Pandas.dayofweek attribute in Python Pandas:
1 2 3 4 5 6 7 8 9 10 11 12 |
import pandas as pd # Timestamp timeStamp = pd.Timestamp(2023, 10, 25) # Display the date and time print("Date and Time = ",timeStamp) # Display the day of week print("Day of Week = ",timeStamp.dayofweek) |
Output
1 2 3 4 |
Date and Time = 2023-10-25 00:00:00 Day of Week = 2 |
Get the day of the year
To get the day of the year, use the Pandas.dayofyear attribute in Python Pandas:
1 2 3 4 5 6 7 8 9 10 11 12 |
import pandas as pd # Timestamp timeStamp = pd.Timestamp(2023, 10, 25) # Display the date and time print("Date and Time = ",timeStamp) # Display the day of year print("Day of Year = ",timeStamp.dayofyear) |
Output
1 2 3 4 |
Date and Time = 2023-10-25 00:00:00 Day of Year = 298 |
Get the number of days in a month
To get the number of days in a month, use the Pandas.days_in_month attribute in Python Pandas:
1 2 3 4 5 6 7 8 9 10 11 12 |
import pandas as pd # Timestamp timeStamp = pd.Timestamp(2023, 10, 25) # Display the date and time print("Date and Time = ",timeStamp) # Display the number of days in the month print("Days in the month = ",timeStamp.daysinmonth) |
Output
1 2 3 4 |
Date and Time = 2023-10-25 00:00:00 Days in the month = 31 |
Check if the year is a leap year
To check if the year is a leap year, use the Pandas.is_leap_year attribute in Python Pandas:
1 2 3 4 5 6 7 8 9 10 11 12 |
import pandas as pd # Timestamp timeStamp = pd.Timestamp(2023, 10, 25) # Display the date and time print("Date and Time = ",timeStamp) # Check if the year is a leap year print("Is this year leap year? = ",timeStamp.is_leap_year ) |
Output
1 2 3 4 |
Date and Time = 2023-10-25 00:00:00 Is this year leap year? = False |
Check if the date is the last day of the month
To check if the date is the last day of the month, use the Pandas.is_month_end attribute in Python Pandas:
1 2 3 4 5 6 7 8 9 10 11 12 |
import pandas as pd # Timestamp timeStamp = pd.Timestamp(2023, 10, 31) # Display the date and time print("Date and Time = ",timeStamp) # Check if the date is the end of the month print("Is this the month end? = ",timeStamp.is_month_end) |
Output
1 2 3 4 |
Date and Time = 2023-10-31 00:00:00 Is this the month end? = True |
Check if the date is the first day of the month
To check if the date is the first day of the month, use the Pandas.is_month_start attribute in Python Pandas:
1 2 3 4 5 6 7 8 9 10 11 12 |
import pandas as pd # Timestamp timeStamp = pd.Timestamp(2023, 11, 1) # Display the date and time print("Date and Time = ",timeStamp) # Check if the date is the first day of the month print("Is this the first day of the month? = ",timeStamp.is_month_start) |
Output
1 2 3 4 |
Date and Time = 2023-11-01 00:00:00 Is this the first day of the month? = True |
Check if the date is the last day of the year
To check if the date is the last day of the year, use the Pandas.is_year_end attribute in Python Pandas:
1 2 3 4 5 6 7 8 9 10 11 12 |
import pandas as pd # Timestamp timeStamp = pd.Timestamp(2023, 12, 31) # Display the date and time print("Date and Time = ",timeStamp) # Check if the date is the last day of the year print("Is this the last day of the year? = ",timeStamp.is_year_end) |
Output
1 2 3 4 |
Date and Time = 2023-12-31 00:00:00 Is this the last day of the year? = True |
Check if the date is the first day of the year
To check if the date is the first day of the year, use the Pandas.is_year_start attribute in Python Pandas:
1 2 3 4 5 6 7 8 9 10 11 12 |
import pandas as pd # Timestamp timeStamp = pd.Timestamp(2024, 1, 1) # Display the date and time print("Date and Time = ",timeStamp) # Check if the date is the first day of the year print("Is this the first day of the year? = ",timeStamp.is_year_start) |
Output
1 2 3 4 |
Date and Time = 2024-01-01 00:00:00 Is this the first day of the year? = True |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
No Comments