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:

  1. Get the current date and time
  2. Get the day of the week
  3. Get the day of the year
  4. Get the number of days in a month
  5. Check if the year is a leap year
  6. Check if the date is the last day of the month
  7. Check if the date is the first day of the month
  8. Check if the date is the last day of the year
  9. Check if the date is the first day of the year

Before moving further, we’ve prepared a video tutorial to implement date-time operations in Pandas:

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:

import pandas as pd
 
print("Current Date and Time\n",pd.Timestamp.now())

Output

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:

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

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:

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

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:

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

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:

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

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:

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

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:

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

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:

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

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:

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

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


 

Iterate over rows and columns in a Pandas DataFrame
Top Pandas Interview Questions and Answers (MCQs)
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment