๐ Pandas Date Functionality โ Parse, Manipulate, and Extract Time Features
๐งฒ Introduction โ Why Use Date Functions in Pandas?
Pandas provides rich date/time functionality to parse, generate, and manipulate temporal data using the datetime module and DatetimeIndex. Whether you’re handling time-based columns, performing time math, or extracting date parts, Pandas makes it simple and efficient.
๐ฏ In this guide, youโll learn:
- How to parse and format dates
- Extract components like year, month, day
- Perform date arithmetic
- Use vectorized
.dtaccessor for fast operations
๐ฅ 1. Parse Strings into DateTime Objects
import pandas as pd
df = pd.DataFrame({
'Date': ['2023-01-01', '2023-01-05', '2023-01-10']
})
df['Date'] = pd.to_datetime(df['Date'])
โ๏ธ Converts string to proper datetime64 format, enabling time operations.
๐งฎ 2. Extract Date Components with .dt
df['Year'] = df['Date'].dt.year
df['Month'] = df['Date'].dt.month
df['Day'] = df['Date'].dt.day
df['Weekday'] = df['Date'].dt.day_name()
โ๏ธ Extracts year, month, day, weekday names, etc.
๐ 3. Extract Time Components (if time included)
df['Hour'] = df['Date'].dt.hour
df['Minute'] = df['Date'].dt.minute
df['Second'] = df['Date'].dt.second
โ๏ธ Applies to datetime with timestamps.
๐ 4. Date Arithmetic
df['Next Week'] = df['Date'] + pd.Timedelta(days=7)
df['Prev Day'] = df['Date'] - pd.Timedelta(days=1)
โ๏ธ Enables shifting dates forward or backward.
๐ 5. Calculate Date Differences
df['Delta'] = df['Date'].diff()
โ๏ธ Computes difference between consecutive rows.
๐๏ธ 6. Filter by Date Ranges
df[df['Date'] > '2023-01-05']
โ๏ธ Filter using string-like comparisons with datetime columns.
๐งฑ 7. Floor, Ceil, Round Dates
df['Date'].dt.floor('D') # Floor to nearest day
df['Date'].dt.ceil('H') # Ceil to nearest hour
df['Date'].dt.round('min') # Round to nearest minute
โ๏ธ Useful for rounding timestamps in time-based reports.
๐ 8. Create Custom Date Ranges
pd.date_range(start='2023-01-01', periods=5, freq='D')
โ๏ธ Generates regular date sequences with specified frequency.
๐งพ 9. Convert to Different Formats
df['Formatted'] = df['Date'].dt.strftime('%d-%b-%Y')
โ๏ธ Format dates for export, display, or string comparison.
๐ Summary โ Key Takeaways
Pandas offers a comprehensive and efficient set of tools to work with date/time values, helping you parse, analyze, and manipulate time series data like a pro.
๐ Key Takeaways:
- Use
pd.to_datetime()to parse date strings - Use
.dtaccessor to extract year, month, day, hour, etc. - Perform date math with
Timedeltaand.diff() - Round/floor/ceil timestamps for interval binning
- Use
strftime()to customize display formats
โ๏ธ Real-world relevance: Common in time series preprocessing, temporal grouping, log analysis, and feature extraction for ML models.
โ FAQs โ Date Functions in Pandas
โ Whatโs the best way to convert strings to datetime?
Use:
pd.to_datetime(df['column'])
โ How do I extract just the month or year?
Use:
df['column'].dt.month
df['column'].dt.year
โ Can I format dates into strings?
Yes:
df['column'].dt.strftime('%Y-%m-%d')
โ How do I create a date range for an index?
Use:
pd.date_range(start='2023-01-01', periods=7)
โ Can I round timestamps to the nearest interval?
โ
Yes:
df['Date'].dt.round('H') # Nearest hour
Share Now :
