numpy手册(3)-Datetimes and Timedeltas

浏览: 2480

这里说下numpy中日期、时间相关的使用。

主要参考:https://docs.scipy.org/doc/numpy-dev/reference/arrays.datetime.html

1. 基本使用

在numpy中,我们很方便的讲字符串转换成日期类型

import numpy as np

np.datetime64('2017-08-06')
Out[3]: numpy.datetime64('2017-08-06')

np.datetime64('2017-08')
Out[4]: numpy.datetime64('2017-08')

#我们可以通过参数,强制将数据格式化为我们想要的粒度
np.datetime64('2017-08' , 'D')
Out[5]: numpy.datetime64('2017-08-01')

np.datetime64('2017-08' , 'Y')
Out[6]: numpy.datetime64('2017')

a = np.array(['2017-07-01','2017-07-15','2017-08-01'] , dtype=np.datetime64)

a
Out[10]: array(['2017-07-01', '2017-07-15', '2017-08-01'], dtype='datetime64[D]')

#我们也可以使用arange函数初始化数组
b = np.arange('2017-08-01','2017-09-01',dtype=np.datetime64)

b
Out[12]:
array(['2017-08-01', '2017-08-02', '2017-08-03', ..., '2017-08-29',
'2017-08-30', '2017-08-31'], dtype='datetime64[D]')

2.日期计算

    在numpy中,我们可以进行简单的日期计算

#2个日期相减,会得到相差的天数
np.datetime64('2017-08-03') - np.datetime64('2017-07-15')
Out[27]: numpy.timedelta64(19,'D')

#这里日期可以直接减去对应的天数
np.datetime64('2017-08-03') - np.timedelta64(20,'D')
Out[28]: numpy.datetime64('2017-07-14')

#这里粒度要一样,一个是D,不可以和M相减
np.datetime64('2017-08-03') - np.timedelta64(1,'M')
Traceback (most recent call last):

File "<ipython-input-29-61beff16fb05>", line 1, in <module>
np.datetime64('2017-08-03') - np.timedelta64(1,'M')

TypeError: Cannot get a common metadata divisor for NumPy datetime metadata [D] and [M] because they have incompatible nonlinear base time units


np.datetime64('2017-08') - np.timedelta64(1,'M')
Out[30]: numpy.datetime64('2017-07')

3.工作日判断

    numpy中提供了一个一些工作日判断的函数,比如,通常周一到周五是工作日

numpy.busday_offset(dates, offsets, roll='raise', weekmask='1111100', holidays=None, busdaycal=None, out=None)

First adjusts the date to fall on a valid day according to the roll rule, then applies offsets to the given dates counted in valid days.
#2017-08-01是周二
#2017-08-01的下一个工作日是2017-08-02
np.busday_offset('2017-08-01',1)
Out[32]: numpy.datetime64('2017-08-02')

#2017-08-01的下2个工作日是2017-08-03
np.busday_offset('2017-08-01',2)
Out[33]: numpy.datetime64('2017-08-03')

这时候,如果传入的日期是周末,就会报错了

#2017-08-05是周六
np.busday_offset('2017-08-05',2)
Traceback (most recent call last):

File "<ipython-input-34-9f767204127b>", line 1, in <module>
np.busday_offset('2017-08-05',2)

ValueError: Non-business day date in busday_offset

当然,我们可以通过参数来避免错误

roll : {‘raise’, ‘nat’, ‘forward’, ‘following’, ‘backward’, ‘preceding’, ‘modifiedfollowing’, ‘modifiedpreceding’}, optional



How to treat dates that do not fall on a valid day. The default is ‘raise’.

‘raise’ means to raise an exception for an invalid day.
‘nat’ means to return a NaT (not-a-time) for an invalid day.
‘forward’ and ‘following’ mean to take the first valid day later in time.
‘backward’ and ‘preceding’ mean to take the first valid day earlier in time.
‘modifiedfollowing’ means to take the first valid day later in time unless it is across a Month boundary, in which case to take the first valid day earlier in time.
‘modifiedpreceding’ means to take the first valid day earlier in time unless it is across a Month boundary, in which case to take the first valid day later in time.


常用的可能是这个forward和backward

一个是向前取第一个有效的工作日,一个是向后取第一个有效的工作日

np.busday_offset('2017-08-05',2,roll='forward')
Out[35]: numpy.datetime64('2017-08-09')

np.busday_offset('2017-08-05',2,roll='backward')
Out[36]: numpy.datetime64('2017-08-08')

np.busday_offset('2017-08-05',0,roll='forward')
Out[37]: numpy.datetime64('2017-08-07')

np.busday_offset('2017-08-05',0,roll='backward')
Out[38]: numpy.datetime64('2017-08-04')
#判断是否为工作日
np.is_busday()

np.is_busday(np.datetime64('2017-08-05'))
Out[39]: False

np.is_busday(np.datetime64('2017-08-01'))
Out[40]: True


#判断时间段内,工作日天数
np.busday_count()

np.busday_count(np.datetime64('2017-08-01'),np.datetime64('2017-08-06'))
Out[41]: 4

np.busday_count(np.datetime64('2017-08-06'),np.datetime64('2017-08-01'))
Out[42]: -4

推荐 0
本文由 liutangwow 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册