Handling DateTime in Pandas
It's **Day 114**, and we're dealing with time. Working with dates in raw Python is hard; in Pandas, it's a dream.
Step 1: Conversion
First, make sure your column is a true `datetime` type.
df['date'] = pd.to_datetime(df['date'])
Step 2: The .dt Accessor
Once it's a datetime, شما have access to the `.dt` property.
# Extract features
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month_name()
df['is_weekend'] = df['date'].dt.dayofweek > 4
Time Differences (Timedelta)
Calculating the gap between two events is as simple as subtraction:
df['shipping_time'] = df['delivery_date'] - df['order_date']
print(df['shipping_time'].mean())
Your Task for Today
Extract the Day of the Week from a date column and calculate the difference in days between two date columns.
*Day 115: Time-Series Analysis: Resampling and Rolling.*