# 创建数据的类型
class dtype(object):
def __init__(self,obj,align=False,copy=False):
pass
class iinfo(object):
def __init__(self,int_type):
pass
def min(self):
pass
def max(self):
pass
import numpy as np
a=np.dtype('b1')
print(a.type)
print(a.itemsize)
ii16=np.iinfo(np.int16)
print(ii16.min,ii16.max)
class finfo(object):
def __init__(self,dtype):
pass
ff16=np.finfo(np.float16)
print(ff16.bits)
print(ff16.min)
print(ff16.max)
print(ff16.eps)
a=np.datetime64('2020-03-01')
print(a.dtype)
a=np.array(['2020-03', '2020-03-08', '2020-03-08 20:00'],dtype='datetime64')
a=np.arange('2020-08-01','2020-08-10',dtype=np.datetime64)
# datetime64 和 timedelta64 运算
import numpy as np
a = np.datetime64('2020-03-08') - np.datetime64('2020-03-07')
b = np.datetime64('2020-03-08') - np.datetime64('202-03-07 08:00')
c = np.datetime64('2020-03-08') - np.datetime64('2020-03-07 23:00', 'D')
print(a, a.dtype) # 1 days timedelta64[D]
print(b, b.dtype) # 956178240 minutes timedelta64[m]
print(c, c.dtype) # 1 days timedelta64[D]
a = np.datetime64('2020-03') + np.timedelta64(20, 'D')
b = np.datetime64('2020-06-15 00:00') + np.timedelta64(12, 'h')
print(a, a.dtype) # 2020-03-21 datetime64[D]
print(b, b.dtype) # 2020-06-15T12:00 datetime64[m]
# numpy.datetime64 与 datetime.datetime 相互转换
import numpy as np
import datetime
dt = datetime.datetime(year=2020, month=6, day=1, hour=20, minute=5, second=30)
dt64 = np.datetime64(dt, 's')
print(dt64, dt64.dtype)
# 2020-06-01T20:05:30 datetime64[s]
dt2 = dt64.astype(datetime.datetime)
print(dt2, type(dt2))
# 2020-06-01 20:05:30 <class 'datetime.datetime'>
# 计算上下工作日
a = np.busday_offset('2020-07-11', offsets=0, roll='forward')
b = np.busday_offset('2020-07-11', offsets=0, roll='backward')
print(a) # 2020-07-13
print(b) # 2020-07-10
a = np.busday_offset('2020-07-11', offsets=1, roll='forward')
b = np.busday_offset('2020-07-11', offsets=1, roll='backward')
# 可以指定偏移量为 0 来获取当前日期向前或向后最近的工作日,当然,如果当前日期本身就是工作日,则直接返回当前日期
print(a) # 2020-07-14
print(b) # 2020-07-13
# 判断是否工作日
import numpy as np
# 2020-07-10 星期五
a = np.is_busday('2020-07-10')
b = np.is_busday('2020-07-11')
print(a) # True
print(b) # False
# 统计一个 datetime64[D] 数组中的工作日天数
import numpy as np
# 2020-07-10 星期五
begindates = np.datetime64('2020-07-10')
enddates = np.datetime64('2020-07-20')
a = np.arange(begindates, enddates, dtype='datetime64')
b = np.count_nonzero(np.is_busday(a))
print(a)
# ['2020-07-10' '2020-07-11' '2020-07-12' '2020-07-13' '2020-07-14'
# '2020-07-15' '2020-07-16' '2020-07-17' '2020-07-18' '2020-07-19']
print(b) # 6
# 自定义周掩码值,即指定一周中哪些星期是工作日。
import numpy as np
# 2020-07-10 星期五
a = np.is_busday('2020-07-10', weekmask=[1, 1, 1, 1, 1, 0, 0])
b = np.is_busday('2020-07-10', weekmask=[1, 1, 1, 1, 0, 0, 1])
print(a) # True
print(b) # False
# numpy.busday_count(begindates, enddates, weekmask='1111100', holidays=[], busdaycal=None, out=None)Counts the number of valid days between begindates and enddates, not including the day of enddates.
import numpy as np
# 2020-07-10 星期五
begindates = np.datetime64('2020-07-10')
enddates = np.datetime64('2020-07-20')
a = np.busday_count(begindates, enddates)
b = np.busday_count(enddates, begindates)
print(a) # 6
print(b) # -6
参考图文