OverflowError:将matlab日期数转换为python日期时间时,日期值超出范围



我正在尝试使用以下函数将matlab日期编号格式的Panda数据帧列转换为python日期时间格式:

def datenum_to_datetime(datenum):
"""
Convert Matlab datenum into Python datetime.
:param datenum: Date in datenum format
:return:        Datetime object corresponding to datenum.
"""
days = datenum % 1
date = datetime.fromordinal(int(datenum)) 
+ timedelta(days=days) 
- timedelta(days=366)

return date.year

值的样本

0         693726
1         693726
2         693726
3         693726
4         693726
...  
460718    726831
460719    726831
460720    726831
460721    726831
460722    726831
Name: dob, Length: 460723, dtype: int32

但当我尝试应用该函数时,会出现以下错误。

df['dob'] = df['dob'].apply(datenum_to_datetime)
OverflowError                             Traceback (most recent call last)
<ipython-input-67-cc4fb607b497> in <module>()
----> 1 df['dob'] = df['dob'].apply(datenum_to_datetime)
2 df.head()
1 frames
pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()
<ipython-input-66-bab915eae5ff> in datenum_to_datetime(datenum)
6     """
7     days = datenum % 1
----> 8     date = datetime.fromordinal(int(datenum))            + timedelta(days=2)            - timedelta(days=366)
9 
10     return date.year
OverflowError: date value out of range

在转换Matlab';s的datenum格式转换为Python,但此函数工作时没有出现错误

def datenum_to_datetime(datenum):
"""
Convert Matlab datenum into Python datetime.
:param datenum: Date in datenum format
:return:        Datetime object corresponding to datenum.
"""
origin = np.datetime64('0000-01-01', 'D') - np.timedelta64(1, 'D')
date = datenum * np.timedelta64(1, 'D') + origin
year = date.astype('datetime64[Y]').astype(int) + 1970

return year

相关内容

  • 没有找到相关文章

最新更新