How to get n working day of the month?

Asked

Viewed 635 times

0

I’m creating a data model for Machine Learning to get a transaction amount forecast per year/month/day/hour.

But I am needing to map the 5th and 10th working day of the month so that my algorithm predicts that are days of great movement and so the numbers are closer to real values.

I have already searched some libraries and found a name Workalendar, where I spend a date and what working day I need and it returns me the date, below follows an example of use.

from workalendar.america import Brazil
cal = Brazil()
print(cal.add_working_days(date(2019, 12, 1), 5))
...
2019-12-06

The problem is that I’m having trouble moving on to my variables 0 or 1 to validate that the working day is actually what I need. I tried to make a loop and scan the entire dataframe by passing the date and working day, however I could not find a way to inform that the returned value is valid or not. :(

Has anyone ever had this problem managed to solve this problem with some mathematical formula or something like that?

Note: I took a look at the library code to see if I could find something, but I didn’t quite understand how they calculate the working day n.

  • 1

    Just remembering that to count only working days you will have to consider the holidays, which can be national, state or municipal.

  • As for the holidays, I’m mapping them manually and merging to select whether a date is a business day or not. For example, if I could get the 5th working day but it was a state holiday the working day would be the next.

  • Put what you tried

  • I was making a "dumb" comparison, rsrs. I managed to solve my problem using the workalendar lib! ;)

1 answer

0


I managed to do, even if it looks like a certain gambiarra! kkk

for i in range(len(dados)):
if(dados['data'][i] == pd.Timestamp(cal.add_working_days(date(anos[i], meses[i], 1), 5))):
    dados['dia5'][i] = 1
if(dados['data'][i] == pd.Timestamp(cal.add_working_days(date(anos[i], meses[i], 1), 10))):
    dados['dia10'][i] = 1

Basically I requested a comparison of all Dataframe data to return a valid date with the one I expected. But I needed to convert the return of the datetime for pandas. Timestamp.

I was doing it backwards and it wasn’t working very well because of the time embedded in the timestamp, when converting the two don’t stay equal in fact.

If anyone has any smarter solution, please report in the post.

Browser other questions tagged

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