Time series data is everywhere in stock prices, weather data,server logs, sales records ,website traffic per hour,machine logs per second. Pandas has tools for parsing,indexing,resampling and analyzing datasets. *Note * - The .dt accessor works for period and timedelta dtypes. 1.Converting to Datetime. The first process convert the date column to date time before doing the time series analysis. df['date'] = pd.to_datetime(df['date']) 2.Extracting day from a column date a.Having the output as 0,1,2,3... df['day_of_week_num'] = df['date'].dt.weekday df['day_of_week_num']: It extracts the numeric day of the week, where Monday is 0 and Sunday is 6. b. Having the output as name Monday,tuesday,Wednesday,thursday df['day_of_week_name'] = df['date'].dt.day_name() It returns the name of the day for each date in the Series or DataFrame, such as "Monday", "Tuesday" c. Abbreviated day name (e.g., "Mon", "Tue", "Wed") df['day_of_week_abbr'] = df['date'].dt.strftime('%a') This gives you a short form for the day name, which is often used when you want to save space or display compact labels. 3. Check for Weekend. df['is_weekend'] = df['date'].dt.weekday >= 5 This create a new column in your DataFrame that flags whether a date falls on a weekend (Saturday or Sunday). 4. Extracting time attributes . Here we will extract Hours,Minutes and seconds. .dt.hour: - This Extracts the hour from the datetime (0–23). .dt.minute: - It Extracts the minute from the datetime (0–59) .dt.second: It Extracts the second from the datetime (0–59). 5. Extract Month (Number and Name). df['month'] = df['date'].dt.month Extracts the numeric month (1–12) from the date column. df['month_name'] = df['date'].dt.month_name() Extracts the full month name (e.g., "January", "February"). C. Three-letter abbreviation df['month_abbr'] = df['month_name'].str[:3] 6.To Extract the quarter of the year from a datetime column use .dt.quarter df['quarter'] = df['date'].dt.quarter - 1 7.Half of the Year (0 = Jan–Jun, 1 = Jul–Dec) df['half'] = np.where(df['month'] < 6, 0, 1) It assumes your month column is zero-based (i.e., 0 = January, 11 = December). Months 0–5 → First Half (0) Months 6–11 → Second Half (1) 8 . Week of the Year (.dt.isocalendar().week) df['week_of_year'] = df['date'].dt.isocalendar().week Note: As of Pandas 1.1.0, .dt.week is deprecated and replaced by .dt.isocalendar().week. Note.What is ISO Week? ISO weeks follow the ISO 8601 standard: Weeks start on Monday Week 1 is the week that contains the first Thursday of the year Can have 52 or 53 weeks. 9. Year. df['year']=df['date'].dt.year Extracts the year as an interger. It's useful for grouping,filtering or feature engineering. Splits time series data by year. Groups data for annual trends. Creates Pivot tables or plots year-wise date year 2022-04-05 2022 2023-11-12 2023 2024-01-01 2024 10.

Time series data is everywhere in stock prices, weather data,server logs, sales records ,website traffic per hour,machine logs per second. Pandas has tools for parsing,indexing,resampling and analyzing datasets.
*Note * - The .dt accessor works for period and timedelta dtypes.
1.Converting to Datetime.
The first process convert the date column to date time before doing the time series analysis.
df['date'] = pd.to_datetime(df['date'])
2.Extracting day from a column date
a.Having the output as 0,1,2,3...
df['day_of_week_num'] = df['date'].dt.weekday
df['day_of_week_num']: It extracts the numeric day of the week, where Monday is 0 and Sunday is 6.
b. Having the output as name Monday,tuesday,Wednesday,thursday
df['day_of_week_name'] = df['date'].dt.day_name()
- It returns the name of the day for each date in the Series or DataFrame, such as "Monday", "Tuesday"
c. Abbreviated day name (e.g., "Mon", "Tue", "Wed")
df['day_of_week_abbr'] = df['date'].dt.strftime('%a')
- This gives you a short form for the day name, which is often used when you want to save space or display compact labels.
3. Check for Weekend.
df['is_weekend'] = df['date'].dt.weekday >= 5
- This create a new column in your DataFrame that flags whether a date falls on a weekend (Saturday or Sunday).
4. Extracting time attributes .
Here we will extract Hours,Minutes and seconds.
.dt.hour: - This Extracts the hour from the datetime (0–23).
.dt.minute: - It Extracts the minute from the datetime (0–59)
.dt.second: It Extracts the second from the datetime (0–59).
5. Extract Month (Number and Name).
df['month'] = df['date'].dt.month
- Extracts the numeric month (1–12) from the date column.
df['month_name'] = df['date'].dt.month_name()
- Extracts the full month name (e.g., "January", "February").
C. Three-letter abbreviation
df['month_abbr'] = df['month_name'].str[:3]
6.To Extract the quarter of the year from a datetime column use .dt.quarter
df['quarter'] = df['date'].dt.quarter - 1
7.Half of the Year (0 = Jan–Jun, 1 = Jul–Dec)
df['half'] = np.where(df['month'] < 6, 0, 1)
It assumes your month column is zero-based (i.e., 0 = January, 11 = December).
- Months 0–5 → First Half (0)
- Months 6–11 → Second Half (1)
8 . Week of the Year (.dt.isocalendar().week)
df['week_of_year'] = df['date'].dt.isocalendar().week
Note: As of Pandas 1.1.0, .dt.week is deprecated and replaced by .dt.isocalendar().week.
Note.What is ISO Week?
- ISO weeks follow the ISO 8601 standard:
- Weeks start on Monday
- Week 1 is the week that contains the first Thursday of the year
- Can have 52 or 53 weeks.
9. Year.
df['year']=df['date'].dt.year
- Extracts the year as an interger.
- It's useful for grouping,filtering or feature engineering.
- Splits time series data by year.
- Groups data for annual trends.
- Creates Pivot tables or plots year-wise
date | year |
---|---|
2022-04-05 | 2022 |
2023-11-12 | 2023 |
2024-01-01 | 2024 |