Brazilian calendars using Pandas-python

Asked

Viewed 1,707 times

2

How do I import the holiday calendar from Brazil using the pandas library in python. For example, if I use the following code:

from pandas.tseries.holiday import *

feriados= USFederalHolydayCalendar()

for feriado in feriados.holidays(start="2020-01-01", end="2020-12-31"):
    print(feriado)

I get every holiday in the United States. How do I get the same thing, but with the Brazilian calendar?

2 answers

4


First, it is good to check if there is a method specifically for Brazil in this module. To check the methods of this and any other module, use the function builtin dir(). So we have:

from pandas.tseries import holiday
    
print(dir(holiday))

returning:

['AbstractHolidayCalendar', 'DateOffset', 'Day', 'Easter', 'EasterMonday', 'FR', 'GoodFriday', 'Holiday', 'HolidayCalendarFactory', 'HolidayCalendarMetaClass', 'List', 'MO', 'PerformanceWarning', 'SA', 'SU', 'Series', 'TH', 'TU', 'Timestamp', 'USColumbusDay', 'USFederalHolidayCalendar', 'USLaborDay', 'USMartinLutherKingJr', 'USMemorialDay', 'USPresidentsDay', 'USThanksgivingDay', 'WE', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'after_nearest_workday', 'before_nearest_workday', 'date_range', 'datetime', 'get_calendar', 'holiday_calendars', 'nearest_workday', 'next_monday', 'next_monday_or_tuesday', 'next_workday', 'np', 'previous_friday', 'previous_workday', 'register', 'sunday_to_monday', 'timedelta', 'warnings', 'weekend_to_monday']

I mean, it really seems to have data in this module just for the US. The second option is to check if there is another module that does the same thing with data from other countries. In fact, there is a module just for this that can be found in this link. Doing the same test for this new package, we have:

import holidays

print(dir(holidays))

returning:

['APR', 'AR', 'AT', 'AU', 'AUG', 'AW', 'Argentina', 'Aruba', 'Australia', 'Austria', 'BE', 'BG', 'BR', 'BY', 'Belarus', 'Belgium', 'Brazil', 'Bulgaria', 'CA', 'CH', 'CL', 'CO', 'CZ', 'Canada', 'Chile', 'Colombia', 'CountryHoliday', 'Croatia', 'Czech', 'Czechia', 'DE', 'DEC', 'DK', 'DO', 'Denmark', 'DominicanRepublic', 'ECB', 'EE', 'EG', 'ES', 'Egypt', 'England', 'Estonia', 'EuropeanCentralBank', 'FEB', 'FI', 'FRA', 'FRI', 'Finland', 'France', 'GB', 'GR', 'Germany', 'Greece', 'HK', 'HND', 'HR', 'HU', 'HolidayBase', 'Honduras', 'HongKong', 'Hungary', 'IE', 'IL', 'IND', 'IS', 'ISR', 'IT', 'Iceland', 'India', 'Ireland', 'IsleOfMan', 'Israel', 'Italy', 'JAN', 'JP', 'JUL', 'JUN', 'Japan', 'KE', 'Kenya', 'LT', 'LU', 'Lithuania', 'Luxembourg', 'MAR', 'MAY', 'MON', 'MX', 'Mexico', 'NG', 'NI', 'NL', 'NO', 'NOV', 'NZ', 'Netherlands', 'NewZealand', 'Nicaragua', 'Nigeria', 'NorthernIreland', 'Norway', 'OCT', 'PE', 'PL', 'PT', 'PTE', 'PY', 'Paraguay', 'Peru', 'Poland', 'Polish', 'Portugal', 'PortugalExt', 'RS', 'RU', 'Russia', 'SAT', 'SE', 'SEP', 'SG', 'SI', 'SK', 'SUN', 'Scotland', 'Serbia', 'Singapore', 'Slovak', 'Slovakia', 'Slovenia', 'SouthAfrica', 'Spain', 'Sweden', 'Switzerland', 'TAR', 'THU', 'TUE', 'UA', 'UK', 'US', 'Ukraine', 'UnitedKingdom', 'UnitedStates', 'WED', 'WEEKEND', 'Wales', 'ZA', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', 'argentina', 'aruba', 'australia', 'austria', 'belarus', 'belgium', 'brazil', 'bulgaria', 'canada', 'chile', 'colombia', 'constants', 'countries', 'createHolidaySum', 'croatia', 'czechia', 'denmark', 'dominican_republic', 'egypt', 'estonia', 'european_central_bank', 'finland', 'france', 'germany', 'greece', 'holiday_base', 'honduras', 'hongkong', 'hungary', 'iceland', 'india', 'ireland', 'israel', 'italy', 'japan', 'kenya', 'list_supported_countries', 'lithuania', 'luxembourg', 'mexico', 'netherlands', 'new_zealand', 'nicaragua', 'nigeria', 'norway', 'paraguay', 'peru', 'poland', 'portugal', 'russia', 'serbia', 'singapore', 'slovakia', 'slovenia', 'south_africa', 'spain', 'sweden', 'switzerland', 'ukraine', 'united_kingdom', 'united_states', 'utils']

Now just rewrite the code with the new module:

import holidays
    
feriados= holidays.Brazil()
    
for feriado in feriados['2020-01-01': '2020-12-31'] :
    print(feriado)

returning:

2020-01-01
2020-02-25
2020-02-26
2020-04-10
2020-04-12
2020-04-21
2020-05-01
2020-06-11
2020-09-07
2020-10-12
2020-11-02
2020-11-15
2020-12-25
  • Forgot to add Link.

  • Truth @Netocosta I will add now. Thank you

-1

There are several ways:

  1. Use the package Holidays;
  2. Use the package workalendar;
  3. Use the package pandas_market_calendars (for BM&F BOVESPA);
  4. Register your own holidays in Pandas.

As you specifically asked for Pandas, follow an example of code:

In:

from datetime import datetime, date
import pandas as pd
from pandas.tseries.holiday import AbstractHolidayCalendar, GoodFriday, Holiday, Easter, Day

class Feriados_SP(AbstractHolidayCalendar):
    rules = [
        Holiday('Confraternização Universal', month=1, day=1),
        Holiday('Aniversário de São Paulo', month=1, day=25),
        Holiday('Segunda-Feira de Carnaval', month=1, day=1, offset=[Easter(), Day(-48)]),
        Holiday('Terça-Feira de Carnaval', month=1, day=1, offset=[Easter(), Day(-47)]),
        Holiday('Quarta-Feira de Cinzas', month=1, day=1, offset=[Easter(), Day(-46)]),
        # Sexta-feira Santa
        GoodFriday,
        Holiday('Corpus Christi', month=1, day=1, offset=[Easter(), Day(60)]),
        Holiday('Tiradentes', month = 4, day = 21),
        Holiday('Dia do Trabalho', month = 5, day = 1),
        Holiday('Revolução Constitucionalista', month=7, day=9, start_date='1997-01-01'),
        Holiday('Independência do Brasil', month = 9, day = 7),
        Holiday('Nossa Senhora Aparecida', month = 10, day = 12),
        Holiday('Finados', month = 11, day = 2),
        Holiday('Proclamação da República', month = 11, day = 15),
        Holiday('Dia da Consciencia Negra', month=11, day=20, start_date='2004-01-01'),
        Holiday('Vespera de Natal', month=12, day=24),
        Holiday('Natal', month = 12, day = 25)]

sp_cal = Feriados_SP()
sp_feriados = pd.offsets.CustomBusinessDay(calendar=sp_cal)
feriados_sp = sp_cal.holidays(datetime(2000, 12, 31), datetime(2079, 12, 31))

start = datetime(2020, 3, 1)
end = datetime(2020, 5, 5)
pd.bdate_range(start, end, freq='C', holidays=feriados_sp)

Out:

DatetimeIndex(['2020-03-02', '2020-03-03', '2020-03-04', '2020-03-05',
               '2020-03-06', '2020-03-09', '2020-03-10', '2020-03-11',
               '2020-03-12', '2020-03-13', '2020-03-16', '2020-03-17',
               '2020-03-18', '2020-03-19', '2020-03-20', '2020-03-23',
               '2020-03-24', '2020-03-25', '2020-03-26', '2020-03-27',
               '2020-03-30', '2020-03-31', '2020-04-01', '2020-04-02',
               '2020-04-03', '2020-04-06', '2020-04-07', '2020-04-08',
               '2020-04-09', '2020-04-13', '2020-04-14', '2020-04-15',
               '2020-04-16', '2020-04-17', '2020-04-20', '2020-04-22',
               '2020-04-23', '2020-04-24', '2020-04-27', '2020-04-28',
               '2020-04-29', '2020-04-30', '2020-05-04', '2020-05-05'],
              dtype='datetime64[ns]', freq='C')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.