import pandas as pd
import datetime as dt

Working with Date and TimeΒΆ

Time-series data is one of the most common data types in data science – from stock prices and sensor readings to web traffic and e-commerce orders. This notebook covers Python’s built-in datetime module, Pandas Timestamp objects, creating DatetimeIndex ranges with pd.date_range() and various frequency strings (daily, weekly, monthly, hourly), accessing date components through the .dt accessor, filtering time-series data with .loc[] and boolean masks, date arithmetic with pd.DateOffset() and pd.Timedelta, truncating date ranges with .truncate(), and computing delivery times by subtracting datetime columns.

Why this matters: Proper datetime handling is critical for time-series forecasting, cohort analysis, seasonality detection, and any analysis where temporal ordering matters. Pandas datetime functionality enables efficient slicing, resampling, and windowing operations that would be cumbersome with raw string dates.

someday = dt.date(2018,4,14)
someday.year
someday.month
someday.day
str(dt.datetime(2018,4,14,8,50,44))
sometime =dt.datetime(2018,4,14,8,50,44)
sometime.day
sometime.month
sometime.year
sometime.hour
sometime.minute
pd.Timestamp('2015-1-6')
pd.Timestamp('2015/1/6')
pd.Timestamp('1/6/2015')
pd.Timestamp('12/19/2015')
pd.Timestamp('19/12/2015')
dates = ['2015-1-6','2015-5-6','2015-2-2']
dtIndex = pd.DatetimeIndex(dates)
values= [100,200,400]
pd.Series(data = values , index=dtIndex)
times = pd.date_range(start='2016', end='2018',freq='D')
type(times[0])
times = pd.date_range(start='2016', end='2018',freq='w')
times
pd.date_range(start='2016', end='2018',freq='w-FRI')
pd.date_range(start='2016', end='2018',freq='5H')
pd.date_range(start='2016', end='2018',freq='M')
pd.date_range(start='2016', end='2018',freq='A')
pd.date_range(start='2014', periods=25)
bunch_of_dates = pd.date_range(start='2000',end='2016', freq='2D')
a = pd.Series(bunch_of_dates)
a.head()
a.dt.day
a.dt.weekday_name
from pandas_datareader import data
company = "MSFT"
start =  "2010-01-01"
end = "2016-12-12"
stocks = data.DataReader(name = company,data_source="yahoo",start = start, end = end)
stocks.loc['2014-04-01']
stocks.iloc[299]
birthday = pd.date_range(start='2000-04-01', end='2016-04-01', freq=pd.DateOffset(years=1))
mask = stocks.index.isin(birthday)
stocks.loc[mask]
stocks.insert(0,"Day of the Week",stocks.index.weekday_name)

stocks.insert(1,"Week Day",stocks.index.weekday_namee)
someday = stocks.index[299]
someday.weekday_name
stocks.head()
stocks.insert(1,"Week Day",stocks.index.weekday_name)
stocks.truncate(before="2009-12-31", after="2011-12-31")
stocks.index + pd.DateOffset(months = 4)
stocks.index+ pd.tseries.offsets.MonthEnd()
timeA = pd.Timestamp('2010-01-31')
timeB = pd.Timestamp('2016-01-31')
timeA - timeB
type(timeA - timeB)
pd.Timedelta(days = 4, weeks = 8, hours = 12)
pd.Timedelta("6 hours 12 minutes")
shipping = pd.read_csv('ecommerce.csv', index_col='ID', parse_dates=['order_date','delivery_date'])
shipping['delivery_time'] = shipping['delivery_date'] - shipping['order_date']
shipping.head()
mask = shipping['delivery_time'] > '256 days'
shipping[mask]