Skip to content

Instantly share code, notes, and snippets.

@legioz
Last active May 25, 2021 14:17
Show Gist options
  • Save legioz/4095ae9fde463425b88e3c29fabdf735 to your computer and use it in GitHub Desktop.
Save legioz/4095ae9fde463425b88e3c29fabdf735 to your computer and use it in GitHub Desktop.
Get Last Business Day Calendar
```python
import datetime
import pandas as pd
def get_last_business_day(frequency_type):
"""
* retorna uma lista com as últimas datas válidas para analise em um período de 60 dias
"""
freq = frequency_type.upper()
inicio = datetime.datetime.now()
fim = inicio + datetime.timedelta(60)
if freq == 'SEMANAL':
lista_datas = [i.to_pydatetime().strftime("%Y-%m-%d") for i in pd.date_range(inicio, fim, freq='W-FRI').tolist()]
elif freq == 'QUINZENAL':
lista_datas = [i.to_pydatetime().strftime("%Y-%m-%d") for i in pd.date_range(inicio, fim, freq='SM').tolist()]
elif freq == 'MENSAL':
lista_datas = [i.to_pydatetime().strftime("%Y-%m-%d") for i in pd.date_range(inicio, fim, freq='BM').tolist()]
elif freq == 'BIMESTRAL':
lista_bimestres = [2, 4, 6, 8, 10, 12]
lista_datas = [i.to_pydatetime().strftime("%Y-%m-%d") for i in pd.date_range(inicio, fim, freq='BM').tolist() if i.to_pydatetime().month in lista_bimestres]
elif freq == 'TRIMESTAL':
lista_datas = [i.to_pydatetime().strftime("%Y-%m-%d") for i in pd.date_range(inicio, fim, freq='BQ').tolist()]
elif freq == 'SEMESTRAL':
lista_semestres = [6, 12]
quarter = [i.to_pydatetime().strftime("%Y-%m-%d") for i in pd.date_range(inicio, fim, freq='BQ').tolist() if i.to_pydatetime().month in lista_semestres]
lista_datas = quarter
elif freq == 'ANUAL':
lista_datas = [i.to_pydatetime().strftime("%Y-%m-%d") for i in pd.date_range(inicio, fim, freq='BA').tolist()]
else:
raise Exception('freq invalida')
return lista_datas
print(get_last_business_day('SEMANAL'))
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment