Part 7: Data Manipulation in Date and Time Handling
Last Updated on March 11, 2026 by Editorial Team Author(s): Raj kumar Originally published on Towards AI. Time is the invisible thread that runs through almost every dataset you’ll encounter. Sales happen on specific dates. Transactions occur at precise moments. Events unfold across hours, days, and years. Yet despite how fundamental time is to data analysis, working with dates and times often trips up even experienced analysts. The challenge is not just technical. It’s conceptual. Time zones shift. Months have different lengths. Leap years throw off calculations. Daylight saving time creates hours that don’t exist or happen twice. Business weeks don’t align with calendar weeks. Fiscal quarters start on different months for different companies. The list goes on. This is where pandas datetime operations become essential. They handle the complexity of temporal data so you can focus on analysis rather than calendar arithmetic. Need to find the last Friday of every month? Extract the quarter from a date? Calculate the business days between two dates? Pandas has you covered. Why DateTime Operations Matter for Business Consider a retail analyst examining sales trends. Without proper datetime handling, they might miss that sales spike every third Monday, or fail to account for holiday effects, or compare weekday traffic to weekend traffic incorrectly. These aren’t just technical mistakes. They lead to wrong conclusions and bad business decisions. Or think about financial reporting. Regulatory bodies require specific date formats. Quarters need to align with fiscal calendars. Year-over-year comparisons must account for leap years. Time series analysis demands proper temporal indexing. Get the dates wrong, and your entire analysis falls apart. The operations covered in this guide represent the core temporal manipulations you’ll need. Converting strings to datetime objects. Extracting components like year, month, and day. Calculating time differences. Adding or subtracting time periods. Resampling time series data. These operations appear in nearly every time-based analysis. The Power of Pandas DateTime What makes pandas datetime handling special is the .dt accessor. Similar to .str for strings, the .dt accessor gives you access to datetime-specific operations. Once your data is in datetime format, you can extract any component, perform calculations, and manipulate temporal data with single lines of code. The pattern is consistent throughout: df['date_column'].dt.property_or_method() This design makes datetime operations intuitive. If you want the year from a date column, it’s .dt.year. Want the day of the week? It's .dt.dayofweek. The methods mirror how you think about dates, making your code readable and maintainable. What You’ll Learn This guide walks through ten essential datetime operations with practical examples from real business scenarios. You’ll see how to convert strings to datetime objects, extract date components for analysis, calculate time differences for duration analysis, and resample time series data for trend analysis. More importantly, you’ll see these operations in context. The complete example at the end demonstrates a realistic sales analysis pipeline that combines multiple datetime operations to generate insights. This reflects how you’ll actually use these tools in practice. A Note on Time Zones and Localization This guide focuses on fundamental datetime operations. For production systems dealing with international data, you’ll also need to handle time zones using pandas’ timezone-aware datetime functionality. That’s a topic worth its own deep dive, but the operations here form the foundation you’ll build on. Let’s start with the most fundamental operation: converting strings to datetime objects. Everything else builds from here. Understanding DateTime Conversion Before you can work with dates, you need to convert them from strings or other formats into datetime objects. This is the gateway to all other temporal operations. 1. Convert to DateTime import pandas as pdimport numpy as np# Sample data with dates as stringsdata = { 'transaction_id': [1001, 1002, 1003, 1004, 1005], 'date_string': ['2024-01-15', '2024-02-20', '2024-03-10', '2024-04-05', '2024-05-12'], 'amount': [150.00, 200.00, 175.50, 300.00, 225.75]}df = pd.DataFrame(data)# Convert string to datetimedf['date'] = pd.to_datetime(df['date_string'])print("Original vs Converted:")print(df[['date_string', 'date']])print(f"\nData type of date_string: {df['date_string'].dtype}")print(f"Data type of date: {df['date'].dtype}") Output: Original vs Converted: date_string date0 2024-01-15 2024-01-151 2024-02-20 2024-02-202 2024-03-10 2024-03-103 2024-04-05 2024-04-054 2024-05-12 2024-05-12Data type of date_string: objectData type of date: datetime64[ns] Why this matters: The datetime64[ns] type enables all temporal operations. Without conversion, dates are just strings, and you can't extract components or perform date arithmetic. Extracting Date Components Once you have datetime objects, you can extract any component for analysis, filtering, or grouping. 2. Extract Year # Extract year for year-over-year analysisdf['year'] = df['date'].dt.yearprint("Transactions with Year:")print(df[['date', 'year', 'amount']]) Output: Transactions with Year: date year amount0 2024-01-15 2024 150.001 2024-02-20 2024 200.002 2024-03-10 2024 175.503 2024-04-05 2024 300.004 2024-05-12 2024 225.75 Use case: Group sales by year for annual reports, filter data by specific years, or create year-over-year comparison reports. 3. Extract Month # Extract month for seasonal analysisdf['month'] = df['date'].dt.monthdf['month_name'] = df['date'].dt.month_name()print("Transactions with Month:")print(df[['date', 'month', 'month_name', 'amount']]) Output: Transactions with Month: date month month_name amount0 2024-01-15 1 January 150.001 2024-02-20 2 February 200.002 2024-03-10 3 March 175.503 2024-04-05 4 April 300.004 2024-05-12 5 May 225.75 Use case: Identify seasonal patterns, create monthly sales reports, or analyze which months perform best. 4. Extract Day # Extract day of monthdf['day'] = df['date'].dt.dayprint("Transactions with Day:")print(df[['date', 'day', 'amount']]) Output: Transactions with Day: date day amount0 2024-01-15 15 150.001 2024-02-20 20 200.002 2024-03-10 10 175.503 2024-04-05 5 300.004 2024-05-12 12 225.75 Use case: Analyze if certain days of the month have higher transaction volumes (payday effects, bill payment patterns). 5. Extract Weekday # Extract day of week (0=Monday, 6=Sunday)df['weekday_num'] = df['date'].dt.dayofweekdf['weekday_name'] = df['date'].dt.day_name()print("Transactions with Weekday:")print(df[['date', 'weekday_num', 'weekday_name', 'amount']]) Output: Transactions with Weekday: date weekday_num weekday_name amount0 2024-01-15 0 Monday 150.001 2024-02-20 1 Tuesday 200.002 2024-03-10 6 Sunday 175.503 2024-04-05 4 Friday 300.004 2024-05-12 6 Sunday 225.75 Use case: Compare weekday vs weekend performance, identify optimal days for promotions, or staff scheduling based on traffic patterns. 6. Extract Week of Year # Extract ISO calendar week numberdf['week_of_year'] = df['date'].dt.isocalendar().weekprint("Transactions with Week Number:")print(df[['date', 'week_of_year', 'amount']]) Output: Transactions with Week Number: date week_of_year amount0 2024-01-15 3 150.001 2024-02-20 8 200.002 2024-03-10 10 175.503 2024-04-05 14 300.004 2024-05-12 19 225.75 Use case: Create weekly sales reports, track […]
