Lesson 35 · Mastering Pandas
Converting Strings to Datetime and Extracting Date Components Using Pandas in Python
In this lesson, we will learn how to work with dates and times in pandas. You will see how to convert string columns to real datetime types. We will also…
- CourseMastering Pandas
- Lesson35 of 44
- Video16 min
- FormatJupyter notebook · 17 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbConverting Strings to Datetime and Extracting Components in Pandas#
In this lesson, we will learn how to work with dates and times in pandas.
You will see how to convert string columns to real datetime types.
We will also learn how to extract useful parts, like the year, month, or weekday, for analysis.
Handling dates is an essential skill in data analysis, finance, and much more.
import warnings
warnings.filterwarnings("ignore")
# Let us set up the Flights dataset, which is great for learning time handling.
import seaborn as sns
import numpy as np
import pandas as pd
np.random.seed(42)
df = sns.load_dataset('flights')
print(df.shape)
print(df.head(3))
Why are datetime types useful?#
Most real datasets have dates as strings.
Dates as true pandas datetime types let you sort, filter, plot, and extract features easily.
Let us look at the columns of our dataset.
print(df.columns)
print(df.dtypes)
# Creating a single column with both year and month as a string.
df['date_str'] = df['year'].astype(str) + '-' + df['month'].astype(str)
print(df[['year', 'month', 'date_str']].head())
# Try to sort by date_str.
print(df.sort_values('date_str').head(3))
Converting date strings to real datetime#
Pandas can turn many kinds of date strings into datetime type with the to_datetime function.
This will allow powerful date operations.
df['date'] = pd.to_datetime(df['date_str'], format='%Y-%b')
print(df[['date_str', 'date']].head())
# Check the type of the new column.
print(type(df.loc[0, 'date']))
Date features: extracting year, month, and more#
Once you have datetimes, you can easily get features like year, month, quarter, weekday, day, and more.
These are just attributes of the pandas datetime column.
df['year_extracted'] = df['date'].dt.year
df['month_number'] = df['date'].dt.month
df['weekday'] = df['date'].dt.weekday
print(df[['date', 'year_extracted', 'month_number', 'weekday']].head())
# You can get the weekday as the name, too.
df['weekday_name'] = df['date'].dt.day_name()
print(df[['date', 'weekday_name']].head())
# Challenge: What date had the most passengers?
max_idx = df['passengers'].idxmax()
print(df.loc[max_idx, ['date', 'passengers']])
# Example: Count total passengers by weekday name.
weekday_totals = df.groupby('weekday_name')['passengers'].sum().sort_values(ascending=False)
print(weekday_totals)
# You can also filter the DataFrame for weekends.
weekends = df[df['weekday_name'].isin(['Saturday', 'Sunday'])]
print(weekends.head(3))
# Visualize passengers over time using the datetime index.
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 4))
plt.plot(df['date'], df['passengers'], marker='o')
plt.title('Monthly Airline Passengers Over Time')
plt.xlabel('Date')
plt.ylabel('Passengers')
plt.grid(True)
plt.tight_layout()
plt.show()
Real-World Tip#
Always convert date columns to pandas datetime as soon as you load new data.
That way, you avoid sorting bugs and can easily use date filtering or extraction features.
# Try: Select all months in 1952.
in_1952 = df[df['date'].dt.year == 1952]
print(in_1952.head())
# Next: What is the average number of passengers in July flights?
july_avg = df[df['month'] == 'July']['passengers'].mean()
print(f"Average passengers in July: {july_avg:.2f}")
# Sometimes real datasets have multiple datetime columns or tricky formats.
example_dates = pd.Series(['2022/03/01', '01-04-2023', '2023.05.01'])
parsed = pd.to_datetime(example_dates, dayfirst=False, errors='coerce')
print(pd.DataFrame({'original': example_dates, 'parsed': parsed}))
# Slicing and resampling: What was the total number of passengers in 1950?
total_1950 = df[df['date'].dt.year == 1950]['passengers'].sum()
print(f"Total passengers in 1950: {total_1950}")
# Using input to enter your own date string to convert. Try entering '2024-05-15' (YYYY-MM-DD).
user_date_str = input("Type a date (YYYY-MM-DD): ")
user_date = pd.to_datetime(user_date_str)
print("You entered:", user_date)
print("Year:", user_date.year, "Month:", user_date.month, "Day:", user_date.day)
Review and Next Steps#
Today you learned how to convert strings to datetime and extract date features in pandas.
You practiced with the Flights dataset and saw common real-world tasks for dates.
Next, try using these skills with a new datasetperhaps Titanic or your own CSV with a date column.
Remember: Always check your date columns and convert them early!
If you liked this lesson, like and subscribe to our channel for more tutorials, tips, and projects.
Let us know how you use pandas dates in the comments below!
Happy coding!
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



