本文介绍了numpy,TypeError:无法从dtype('< M8 [us]')强制转换为dtype('< M8 [D]')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出直到特定日期的工作日数,并出现以下错误:

I'm trying to find out number of business days until a specific date and get the following error:

import numpy as np
import pandas_market_calendars as mcal
from datetime import datetime
import pandas as pd

nyse = mcal.get_calendar('NYSE')
holidays = nyse.holidays()
holidays = list(holidays.holidays) # NYSE Holidays

today = datetime.now()
expiration = datetime(2019,2,13,0,0)

days_to_expiration = np.busday_count(today,expiration,holidays=holidays)
print(days_to_expiration)


In [6]: days_to_expiration = np.busday_count(today,expiration,holidays=holidays)
Traceback (most recent call last):

  File "<ipython-input-6-559c16b20339>", line 1, in <module>
    days_to_expiration = np.busday_count(today,expiration,holidays=holidays)

TypeError: Iterator operand 0 dtype could not be cast from dtype('<M8[us]') to dtype('<M8[D]') according to the rule 'safe'

有什么想法吗?

推荐答案

您应该将日期传递给busday_count而不是日期时间:

You should pass dates to busday_count rather than datetimes:

In [11]: today = datetime.now()
    ...: expiration = datetime(2019,2,13,0,0)
    ...:
    ...: days_to_expiration = np.busday_count(today.date(),expiration.date(),holidays=holidays)
    ...: print(days_to_expiration)
11

这篇关于numpy,TypeError:无法从dtype('&lt; M8 [us]')强制转换为dtype('&lt; M8 [D]')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 18:58