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

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:

Output

Get the day of the week

To get the day of the week, use the Pandas.dayofweek attribute in Python Pandas:

Output

Get the day of the year

To get the day of the year, use the Pandas.dayofyear attribute in Python Pandas:

Output

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:

Output

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:

Output

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:

Output

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:

Output

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:

Output

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:

Output


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
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading