numpy 时间日期的应用以及数据类型及数组创建

@Numpy 数据类型及数组创建

一、时间日期和时间增量的应用
eg:2020-07-10 周五

#从字符串创建 datetime64 类型时,默认情况下,numpy 会根据字符串自动选择对应的单位。
import numpy as np
a = np.datetime64('2020-03-01')
print(a,a.dtype)

#从字符串创建 datetime64 类型时,可以强制指定使用的单位。
import numpy as np
a=np.datetime64('2020-03','D')
print(a,a.dtype)

#从字符串创建 datetime64 数组时,如果单位不统一,则一律转化成其中最小的单位
import numpy as np
a=np.array(['2020-03','2020-03-08','2020-03-08 20:00'],dtype='datetime64')
print(a,a.dtype)

#使用 arange() 创建 datetime64 数组,用于生成日期范围。
import numpy as np
a = np.arange('2020-08-01', '2020-08-10', dtype=np.datetime64)
print(a)
print(a.dtype)
a = np.arange('2020-08-01 20:00', '2020-08-10', dtype=np.datetime64)
print(a)

#timedelta64 表示两个 datetime64 之间的差。timedelta64 也是带单位的,并且和相减运算中的两个 datetime64 中的较小的单位保持一致。
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)
print(b,b.dtype)
print(c,c.dtype)
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)
print(b,b.dtype)

#生成 timedelta64时,要注意年('Y')和月('M')这两个单位无法和其它单位进行运算(一年有几天?一个月有几个小时?这些都是不
#确定的)。
import numpy as np
a=np.timedelta64(1,'Y')
b=np.timedelta64(a,'M')
print(a)
print(b)
c=np.timedelta64(1,'h')
d=np.timedelta64(c,'m')
print(c)
print(d)
print(np.timedelta64(a,'D'))# TypeError: Cannot cast NumPy timedelta64 scalar from metadata [Y] to [D] according to the rule 'same_kind'
print(np.timedelta(b,'D'))# TypeError: Cannot cast NumPy timedelta64 scalar from metadata [Y] to [D] according to the rule 'same_kind'

#timedelta64 的运算。
import numpy as np
a=np.timedelta64(1,'Y')
b=np.timedelta64(6,'M')
c=np.timedelta64(1,'W')
d=np.timedelta64(1,'D')
e=np.timedelta64(10,'D')
print(a)
print(b)
print(a+b)
print(a-b)
print(2*a)
print(a/b)
print(c/d)
print(c%e)

#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)
dt2 = dt64.astype(datetime.datetime)
print(dt2, type(dt2))

#将指定的偏移量应用于工作日,单位天('D')。计算下一个工作日,如果当前日期为非工作日,默认报错。可以指定 forward 或
#backward 规则来避免报错。(一个是向前取第一个有效的工作日,一个是向后取第一个有效的工作日)
import numpy as np
a=np.busday_offset('2020-07-10',offsets=1)
print(a)
#a=np.busday_offset('2020-07-11',offsets=1)
#print(a)
a = np.busday_offset('2020-07-11', offsets=0, roll='forward')
b = np.busday_offset('2020-07-11', offsets=0, roll='backward')
print(a)
print(b)
a = np.busday_offset('2020-07-11', offsets=3, roll='forward')
b = np.busday_offset('2020-07-11', offsets=-1, roll='backward')
print(a)
print(b)

#返回指定日期是否是工作日。
import numpy as np
a=np.is_busday('2020-07-10')
b=np.is_busday('2020-07-11')
print(a)
print(b)

#统计一个 datetime64[D] 数组中的工作日天数。
import numpy as np
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)
print(b)


import numpy as np
a = np.busday_offset('2020-07-11', offsets=1, roll='forward')
b = np.busday_offset('2020-07-11', offsets=-1, roll='backward')
print(a)
print(b)

#自定义周掩码值,即指定一周中哪些星期是工作日。
import numpy as np
np.is_busday('2020-07-10',weekmask=[1,1,1,1,1,0,0])
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)
print(b)

#返回两个日期之间的工作日数量。
import numpy as np
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)
print(b)

二、依据现有数据来创建ndarray

#(a)通过array()函数进行创建。
#创建一维数组
import numpy as np
a=np.array([0,1,2,3,4])
b=np.array((0,1,2,3,4))
print(a,type(a))
print(b,type(b))
#创建二维数组
c = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
print(c, type(c))
#创建三维数组
d=np.array([[(1.5,2,3),(4,5,6)],
            [(3,2,1),(4,5,6)]])
print(d,type(d))



#(b)通过asarray()函数进行创建
#array() 和 asarray() 都可以将结构数据转化为 ndarray
import numpy as np
x=[[1,1,1],[1,1,1],[1,1,1]]
y=np.array(x)
z=np.asarray(x)
x[1][2]=2
print(x,type(x))
print(y,type(y))
print(z,type(z))


#array() 和 asarray() 的区别。( array() 和 asarray() 主要区别就是当数据源是ndarray 时, array() 仍然会 copy 出一个副
#本,占用新的内存,但不改变 dtype 时 asarray() 不会。)
import numpy as np
x=np.array([[1,1,1],[1,1,1],[1,1,1]])
y=np.array(x)
z=np.asarray(x)
w=np.asarray(x,dtype=np.int)
x[1][2]=2
print(x,type(x),x.dtype)
print(y,type(y),y.dtype)
print(z,type(z),z.dtype)
print(w,type(w),w.dtype)

#更改为较大的dtype时,其大小必须是array的最后一个axis的总大小(以字节为单位)的除数
import numpy as np
x = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])#int 32
print(x, x.dtype)
x.dtype=np.int16
print(x)

# (c)通过fromfunction()函数进行创建
#给函数绘图的时候可能会用到 fromfunction() ,该函数可从函数中创建数组。
#def fromfunction(function, shape, **kwargs):
#通过在每个坐标上执行一个函数来构造数组
import numpy as np
def f(x,y):
    return 10*x+y
x=np.fromfunction(f,(5,4),dtype=int)
print(x)
x=np.fromfunction(lambda i,j:i == j,(3,3),dtype=int)
print(x)
x=np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int)
print(x)

插入链接与图片

链接: [link]

  1. https://www.jianshu.com/p/336cd77d9914 2. https://www.cnblogs.com/gl1573/p/10549547.html#h2datetime64 3. https://www.numpy.org.cn/reference/arrays/datetime.html#%E6%97%A5%E6%9C%9F%E6%97%B6%E9%97%B4%E5%8D%95%E4%BD%8D

参考:datawhale 十月组队学习numpy文档 未完待续…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值