Pandas Lesson 16 – Date and Time | Dataplexa

Date & Time Handling in Pandas

Date and time data is extremely common in real-world datasets — order dates, transaction timestamps, login times, and more.

Pandas provides powerful tools to parse, analyze, and manipulate date and time values efficiently.


Loading the Dataset

We continue working with the same dataset used throughout the Pandas course.

import pandas as pd

df = pd.read_csv("dataplexa_pandas_sales.csv")

Understanding Date Columns

Date columns are often stored as strings when reading CSV files.

Before performing date operations, we must convert them into datetime format.


Converting Strings to Datetime

Use pd.to_datetime() to convert a column to datetime.

Example: converting the order date column.

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

Once converted, Pandas understands the column as a date.


Checking Datetime Format

You can confirm the data type using:

df.dtypes

The date column should now display as datetime64.


Extracting Date Components

Pandas allows you to extract useful parts of a date:

  • Year
  • Month
  • Day
  • Weekday
df["year"] = df["order_date"].dt.year
df["month"] = df["order_date"].dt.month
df["day"] = df["order_date"].dt.day

Getting Month and Day Names

You can also extract readable names.

df["month_name"] = df["order_date"].dt.month_name()
df["day_name"] = df["order_date"].dt.day_name()

This is useful for reports and summaries.


Filtering Data by Date

You can filter rows based on date conditions.

Example: get all orders from 2024.

df[df["order_date"].dt.year == 2024]

Filtering Between Two Dates

You can select records between specific dates.

df[(df["order_date"] >= "2024-01-01") & 
   (df["order_date"] <= "2024-06-30")]

Sorting by Date

Sorting by date is common in time-based analysis.

df.sort_values(by="order_date")

Calculating Time Differences

You can calculate differences between dates.

Example: days since each order.

df["days_since_order"] = (
    pd.Timestamp("today") - df["order_date"]
).dt.days

Handling Missing Dates

Missing date values should be handled carefully.

df["order_date"] = df["order_date"].fillna(pd.Timestamp("1970-01-01"))

Practice Exercise

Using the dataset:

  • Convert the date column to datetime
  • Extract year and month
  • Filter records for a specific year
  • Sort data by date

What’s Next?

In the next lesson, you will learn how to group data using GroupBy and Aggregations, one of the most powerful features in Pandas.