Pandas Time Series Analysis - Part 3: Holidays(Nepali Context)
I am a Django developer driven by a deep passion for coding and a relentless pursuit of problem-solving. Over the years, I've cultivated my skills in web development, specializing in crafting powerful and scalable applications using the Django framework.
Every project is an exciting challenge for me, and I thrive on unraveling complex problems to create elegant and efficient solutions. My commitment to staying at the forefront of industry trends and technologies reflects my dedication to continuous learning.
Whether it's building innovative features from scratch or optimizing existing code, my enthusiasm for coding is at the core of everything I do. I find joy in the journey of creating impactful and user-friendly applications, leveraging the full potential of Django in the process.
Hey friends, namaste! If you’re diving into time series analysis with pandas and need to handle Nepali stock market schedules, you’ve come to the right place. In Nepal, the stock market (e.g., NEPSE) typically operates from Sunday to Thursday, with Friday and Saturday as weekends. Plus, there are unique national festivals and holidays—like Dashain and Tihar—that might close the market as well.
Let’s walk through how to configure pandas so your date ranges properly exclude non-trading days and incorporate any special holidays.
1. Setting Up Your Nepali Stock Data
Scenario
You have a CSV file of stock price data from 1st July to 21st July, but it’s missing a Date column.
The NEPSE runs Sunday to Thursday. That means Friday and Saturday are not trading days.
You want your time series to skip those days automatically.
You might also want to exclude specific holidays (e.g., major festival dates).
Basic Imports
import pandas as pd
from pandas.tseries.offsets import CustomBusinessDay
Assume you already have your CSV file in a DataFrame:
df = pd.read_csv('nepse_stock_data_no_dates.csv') # Contains prices but no dates
2. Defining a Custom Business Day for Nepal
In Nepal, the stock exchange is typically closed on Friday and Saturday. Pandas’ CustomBusinessDay can handle this through the weekmask parameter.
# In Nepal, workdays: Sun=1, Mon=2, Tue=3, Wed=4, Thu=5
# Weekend: Fri (6) and Sat (7) are off.
nepal_bd = CustomBusinessDay(weekmask='Sun Mon Tue Wed Thu')
# Let's generate a DateTime index from July 1st to July 21st
date_index = pd.date_range(start='2023-07-01', end='2023-07-21', freq=nepal_bd)
print(date_index)
When you print this out, you’ll notice:
It skips every Friday and Saturday automatically.
Only includes Sunday through Thursday.
Now, assign this custom index to your DataFrame:
df.index = date_index
print(df.head())
Your DataFrame’s rows should match the actual trading days, no more “weekend” gaps.
3. Handling Nepali Holidays and Festivals
Of course, NEPSE isn’t only closed on weekends. Major festivals like Dashain or Tihar can also shut down trading for a few days. If you know those specific dates, you can exclude them by passing a holidays list into CustomBusinessDay.
Example: Excluding Dashain & Tihar Dates
Let’s say you know the market will be closed on:
24th October 2023 (Dashain)
12th November 2023 (Tihar)
Here’s how you’d define that:
nepal_bd_holidays = CustomBusinessDay(
weekmask='Sun Mon Tue Wed Thu',
# List out the exact Gregorian dates of Nepali holidays (format: YYYY-MM-DD)
holidays=['2023-10-24', '2023-11-12']
)
date_index_festivals = pd.date_range(
start='2023-10-01',
end='2023-11-30',
freq=nepal_bd_holidays
)
print(date_index_festivals)
Check the output to confirm that October 24th and November 12th are missing. Perfect for aligning your data with actual NEPSE trading days.
4. Creating a Custom Holiday Calendar
If you want a more robust or dynamic holiday system (e.g., changing festival dates each year), you can subclass AbstractHolidayCalendar. This might be handy if you’re working on a long-term project and want to keep a central record of all the major holidays.
from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday
class NepaliHolidayCalendar(AbstractHolidayCalendar):
"""
Nepali holiday calendar for major festivals.
Just an example – fill in the actual dates/rules as needed.
"""
# Example: Dashain (10/24), Tihar (11/12)
rules = [
Holiday('Dashain', month=10, day=24),
Holiday('Tihar', month=11, day=12),
]
# Create a CustomBusinessDay with your new holiday calendar
from pandas.tseries.offsets import CustomBusinessDay
nepali_calendar = NepaliHolidayCalendar()
nepal_bd_calendar = CustomBusinessDay(weekmask='Sun Mon Tue Wed Thu', calendar=nepali_calendar)
# Generate a date range for Oct-Nov
date_range_calendar = pd.date_range(
start='2023-10-01',
end='2023-11-30',
freq=nepal_bd_calendar
)
print(date_range_calendar)
This approach is more scalable if you want to keep adding new holiday rules. Just update the NepaliHolidayCalendarclass, and everything else will follow.
5. Putting It All Together
Let’s say you have a CSV with trading prices from July 1st to July 31st, but no date column. You know:
NEPSE trades Sunday to Thursday.
You want to skip weekends (Fri, Sat).
You also want to skip July 9th as a special closure day.
Here’s how you can do it in one go:
import pandas as pd
from pandas.tseries.offsets import CustomBusinessDay
# Step 1: Read the CSV
df = pd.read_csv('nepse_stock_data_no_dates.csv')
# Step 2: Define a custom business day
nepal_bd_custom = CustomBusinessDay(
weekmask='Sun Mon Tue Wed Thu',
holidays=['2023-07-09'] # special closure day
)
# Step 3: Generate a date range
date_index_custom = pd.date_range(
start='2023-07-01',
end='2023-07-31',
freq=nepal_bd_custom
)
# Step 4: Assign this index to your DataFrame
df.index = date_index_custom
print(df)
Voilà! Your DataFrame now lines up exactly with NEPSE trading days for July, minus that special holiday.
6. Why This Matters
Accuracy in Analysis: Financial, forecasting, or machine learning models that use actual trading days can be more accurate.
Planning & Scheduling: If you’re building a tool for local investors or analysts, they’ll appreciate correct date alignment for NEPSE.
Global Comparisons: If you compare multiple markets (e.g., NEPSE with NYSE), each has its own weekends and holidays. Pandas can handle all of it!
By fine-tuning your date ranges, you’ll avoid the confusion of “phantom” Fridays or Saturdays when the market’s closed. You can also set up your code to skip major festivals so your data perfectly reflects real-world conditions.
7. Key Takeaways
Use
CustomBusinessDayto define working days and skip weekends specific to Nepal’s schedule (Sunday–Thursday).Add a
holidayslist or subclassAbstractHolidayCalendarto handle big festival closures like Dashain, Tihar, or any custom off days.Observance rules can shift a holiday if it falls on a weekend—handy if your system tracks floating holiday policies.
Experiment! The best way to learn is by trying these code snippets in a Jupyter Notebook or any Python environment.






