Pandas Date and Time Practice Questions with Solutions

Introduction

Working with dates and times is an essential skill in data analysis. Pandas provides powerful date and time functions such as to_datetime(), Timestamp, date_range(), and the .dt accessor to manipulate, analyze, and extract date-related information. These features are widely used in sales reporting, financial analysis, attendance systems, time-series analysis, and business intelligence dashboards. Pandas Date and Time practice questions with solutions help to understand the concepts.


1. Python Program to Convert Strings into DateTime

Problem Statement

Write a Python program to convert a column of date strings into DateTime format.

Python Solution

import pandas as pd

data = {
    "Joining_Date": [
        "2024-01-15",
        "2024-03-20",
        "2024-05-10"
    ]
}

df = pd.DataFrame(data)

df["Joining_Date"] = pd.to_datetime(
    df["Joining_Date"]
)

print(df)

Sample Output

  Joining_Date
0   2024-01-15
1   2024-03-20
2   2024-05-10

Explanation

The to_datetime() function converts text values into Pandas DateTime objects.

Concepts Covered

  • to_datetime()
  • Date Conversion
  • DateTime Objects

2. Python Program to Display the Current Date and Time

Problem Statement

Write a Python program to display the current date and time.

Python Solution

import pandas as pd

current_time = pd.Timestamp.now()

print(current_time)

Sample Output

2026-08-04 10:30:45.123456

Explanation

The Timestamp.now() function returns the current system date and time.

Concepts Covered

  • Timestamp
  • Current Date
  • Current Time

3. Python Program to Extract the Year from a Date Column

Problem Statement

Write a Python program to extract the year from a DateTime column.

Python Solution

import pandas as pd

data = {
    "Date": [
        "2023-06-10",
        "2024-07-20",
        "2025-08-15"
    ]
}

df = pd.DataFrame(data)

df["Date"] = pd.to_datetime(df["Date"])

df["Year"] = df["Date"].dt.year

print(df)

Sample Output

        Date  Year
0 2023-06-10  2023
1 2024-07-20  2024
2 2025-08-15  2025

Explanation

The .dt.year attribute extracts the year from each DateTime value.

Concepts Covered

  • .dt.year
  • Date Extraction
  • DateTime Accessor

4. Python Program to Extract the Month from a Date Column

Problem Statement

Write a Python program to extract the month from a DateTime column.

Python Solution

import pandas as pd

data = {
    "Date": [
        "2024-01-10",
        "2024-06-25",
        "2024-12-05"
    ]
}

df = pd.DataFrame(data)

df["Date"] = pd.to_datetime(df["Date"])

df["Month"] = df["Date"].dt.month

print(df)

Sample Output

        Date  Month
0 2024-01-10      1
1 2024-06-25      6
2 2024-12-05     12

Explanation

The .dt.month attribute extracts the month number from each date.

Concepts Covered

  • .dt.month
  • Month Extraction
  • Date Components

5. Python Program to Extract the Day from a Date Column

Problem Statement

Write a Python program to extract the day from a DateTime column.

Python Solution

import pandas as pd

data = {
    "Date": [
        "2024-01-15",
        "2024-02-28",
        "2024-03-10"
    ]
}

df = pd.DataFrame(data)

df["Date"] = pd.to_datetime(df["Date"])

df["Day"] = df["Date"].dt.day

print(df)

Sample Output

        Date  Day
0 2024-01-15   15
1 2024-02-28   28
2 2024-03-10   10

Explanation

The .dt.day attribute extracts the day of the month from DateTime values.

Concepts Covered

  • .dt.day
  • Day Extraction
  • Date Analysis

6. Python Program to Extract the Day Name from a Date

Problem Statement

Write a Python program to display the day name for each date.

Python Solution

import pandas as pd

data = {
    "Date": [
        "2024-01-15",
        "2024-01-16",
        "2024-01-17"
    ]
}

df = pd.DataFrame(data)

df["Date"] = pd.to_datetime(df["Date"])

df["Day_Name"] = df["Date"].dt.day_name()

print(df)

Sample Output

        Date   Day_Name
0 2024-01-15     Monday
1 2024-01-16    Tuesday
2 2024-01-17  Wednesday

Explanation

The day_name() function returns the weekday name for each date.

Concepts Covered

  • .dt.day_name()
  • Weekday
  • Date Analysis

7. Python Program to Extract the Month Name from a Date

Problem Statement

Write a Python program to display the month name for each date.

Python Solution

import pandas as pd

data = {
    "Date": [
        "2024-01-15",
        "2024-06-18",
        "2024-12-25"
    ]
}

df = pd.DataFrame(data)

df["Date"] = pd.to_datetime(df["Date"])

df["Month_Name"] = df["Date"].dt.month_name()

print(df)

Sample Output

        Date Month_Name
0 2024-01-15    January
1 2024-06-18       June
2 2024-12-25   December

Explanation

The month_name() function returns the full name of the month.

Concepts Covered

  • .dt.month_name()
  • Month Name
  • Date Components

8. Python Program to Extract the Quarter from a Date

Problem Statement

Write a Python program to determine the quarter for each date.

Python Solution

import pandas as pd

data = {
    "Date": [
        "2024-02-15",
        "2024-05-20",
        "2024-10-10"
    ]
}

df = pd.DataFrame(data)

df["Date"] = pd.to_datetime(df["Date"])

df["Quarter"] = df["Date"].dt.quarter

print(df)

Sample Output

        Date  Quarter
0 2024-02-15        1
1 2024-05-20        2
2 2024-10-10        4

Explanation

The .dt.quarter attribute returns the quarter (1–4) of each date.

Concepts Covered

  • .dt.quarter
  • Quarter Extraction
  • Date Analysis

9. Python Program to Calculate the Difference Between Two Dates

Problem Statement

Write a Python program to calculate the number of days between two dates.

Python Solution

import pandas as pd

start_date = pd.to_datetime("2024-01-10")
end_date = pd.to_datetime("2024-02-15")

difference = end_date - start_date

print(difference)

Sample Output

36 days 00:00:00

Explanation

Subtracting two DateTime values returns a Timedelta object representing the time difference.

Concepts Covered

  • Date Difference
  • Timedelta
  • Date Arithmetic

10. Python Program to Create a Date Range

Problem Statement

Write a Python program to create a sequence of dates.

Python Solution

import pandas as pd

dates = pd.date_range(
    start="2024-01-01",
    periods=5
)

print(dates)

Sample Output

DatetimeIndex(['2024-01-01',
               '2024-01-02',
               '2024-01-03',
               '2024-01-04',
               '2024-01-05'],
              dtype='datetime64[ns]', freq='D')

Explanation

The date_range() function generates a sequence of dates based on the starting date and the number of periods.

Concepts Covered

  • date_range()
  • Date Sequence
  • Time Series Data

11. Python Program to Filter Records After a Specific Date

Problem Statement

Write a Python program to display records where the joining date is after 2024-03-01.

Python Solution

import pandas as pd

data = {
    "Employee": ["Rahul", "Aman", "Priya"],
    "Joining_Date": [
        "2024-01-15",
        "2024-04-10",
        "2024-06-20"
    ]
}

df = pd.DataFrame(data)

df["Joining_Date"] = pd.to_datetime(
    df["Joining_Date"]
)

result = df[
    df["Joining_Date"] > "2024-03-01"
]

print(result)

Sample Output

  Employee Joining_Date
1     Aman   2024-04-10
2    Priya   2024-06-20

Explanation

Pandas allows filtering DateTime columns using comparison operators.

Concepts Covered

  • Date Filtering
  • to_datetime()
  • Boolean Indexing

12. Python Program to Format Dates

Problem Statement

Write a Python program to display dates in DD-MM-YYYY format.

Python Solution

import pandas as pd

data = {
    "Date": [
        "2024-01-15",
        "2024-06-20"
    ]
}

df = pd.DataFrame(data)

df["Date"] = pd.to_datetime(df["Date"])

df["Formatted_Date"] = df["Date"].dt.strftime(
    "%d-%m-%Y"
)

print(df)

Sample Output

        Date Formatted_Date
0 2024-01-15     15-01-2024
1 2024-06-20     20-06-2024

Explanation

The strftime() function formats DateTime values into custom string formats.

Concepts Covered

  • strftime()
  • Date Formatting
  • DateTime Conversion

13. Python Program to Add Days to a Date

Problem Statement

Write a Python program to add 10 days to each date.

Python Solution

import pandas as pd

data = {
    "Date": [
        "2024-01-15",
        "2024-02-20"
    ]
}

df = pd.DataFrame(data)

df["Date"] = pd.to_datetime(df["Date"])

df["New_Date"] = df["Date"] + pd.Timedelta(days=10)

print(df)

Sample Output

        Date   New_Date
0 2024-01-15 2024-01-25
1 2024-02-20 2024-03-01

Explanation

The Timedelta object is used to perform date arithmetic such as adding or subtracting days.

Concepts Covered

  • Timedelta
  • Date Arithmetic
  • Add Days

14. Python Program to Calculate Employee Experience in Days

Problem Statement

Write a Python program to calculate the number of days since an employee joined the company.

Python Solution

import pandas as pd

data = {
    "Employee": ["Rahul", "Aman"],
    "Joining_Date": [
        "2023-01-15",
        "2024-02-20"
    ]
}

df = pd.DataFrame(data)

df["Joining_Date"] = pd.to_datetime(
    df["Joining_Date"]
)

today = pd.Timestamp.now()

df["Experience_Days"] = (
    today - df["Joining_Date"]
).dt.days

print(df)

Sample Output

  Employee Joining_Date  Experience_Days
0    Rahul   2023-01-15               932
1     Aman   2024-02-20               530

Explanation

Subtracting the joining date from the current date returns a Timedelta object. The .dt.days attribute extracts the total number of days.

Concepts Covered

  • Timestamp.now()
  • Timedelta
  • Experience Calculation

15. Python Program to Check Whether a Date Falls on a Weekend

Problem Statement

Write a Python program to determine whether each date is a weekend.

Python Solution

import pandas as pd

data = {
    "Date": [
        "2024-01-13",
        "2024-01-15"
    ]
}

df = pd.DataFrame(data)

df["Date"] = pd.to_datetime(df["Date"])

df["Weekend"] = df["Date"].dt.dayofweek >= 5

print(df)

Sample Output

        Date  Weekend
0 2024-01-13     True
1 2024-01-15    False

Explanation

The dayofweek attribute returns values from 0 (Monday) to 6 (Sunday). Values greater than or equal to 5 indicate weekends.

Concepts Covered

  • .dt.dayofweek
  • Weekend Detection
  • Date Analysis

Chapter Summary

In this chapter, you learned how to work with dates and times in Pandas. You practiced converting strings into DateTime objects, extracting year, month, day, weekday, month name, and quarter, calculating date differences, generating date ranges, filtering records by date, formatting dates, performing date arithmetic, calculating employee experience, and identifying weekends. These techniques are widely used in financial reporting, attendance systems, sales analysis, business intelligence, and time-series data analysis.


Key Takeaways

  • to_datetime() converts strings into DateTime objects.
  • .dt provides access to DateTime components.
  • Timestamp.now() returns the current date and time.
  • date_range() generates sequences of dates.
  • Timedelta performs date arithmetic.
  • strftime() formats dates into custom formats.
  • Date filtering can be performed using comparison operators.
  • day_name() and month_name() return readable names.
  • dayofweek helps identify weekdays and weekends.
  • Date and time functions are essential for time-series analysis and reporting.

Frequently Asked Questions (FAQs)

1. How do you convert text into DateTime in Pandas?

df["Date"] = pd.to_datetime(df["Date"])

2. How do you extract the year from a date?

df["Date"].dt.year

3. Which function creates a sequence of dates?

pd.date_range(
    start="2024-01-01",
    periods=5
)

4. How do you calculate the difference between two dates?

end_date - start_date

5. How do you format dates in Pandas?

df["Date"].dt.strftime("%d-%m-%Y")

6. How do you add days to a date?

df["Date"] + pd.Timedelta(days=10)

7. How do you identify weekends in Pandas?

df["Date"].dt.dayofweek >= 5

8. Why are DateTime functions important in Pandas?

DateTime functions simplify time-based analysis, reporting, trend analysis, attendance tracking, financial calculations, scheduling, and time-series data processing, making them an essential part of real-world data analysis.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top