1

I want to calculate the number of days between the Current date and the 1st day of the current month. If the date is the 1st of the month itself then my variable (let's call it X) should be assigned 1 else it calculates the difference.

I've tried using the busday_count function available in Python as shown below but it gives me the following error

np.busday_count((datetime.date(now.year,now.month,1)), datetime.datetime.now())

error

TypeError Traceback (most recent call last) in ----> 1 np.busday_count((datetime.date(now.year,now.month,1)), now)

TypeError: Iterator operand 1 dtype could not be cast from dtype('

Tony
  • 9,672
  • 3
  • 47
  • 75

2 Answers2

2

You can go about it like this, to get number of business days since beginning of month including today:

np.busday_count(np.datetime64('today', 'M'), np.datetime64('today', 'D')) + 1
zipa
  • 27,316
  • 6
  • 40
  • 58
0

zipa's answer will give you just the business days.

if you want all the days.

using datetime library.

from datetime import date
today = date.today()
firstOfMonth = datetime(today.year, today.month, 1)
numOfDays = today - firstOfMonth
print (numOfDays.days + 1)

using Numpy

from datetime import date
import numpy as np
today = date.today()
firstOfMonth = datetime(today.year, today.month, 1)
days = np.datetime64(today) - np.datetime64(firstOfMonth) + 1
Yatish Kadam
  • 454
  • 2
  • 11