task1

it2023-11-12  65

第一章 常量 import numpy as np

numpy.nan

表示空值: nan = NAN =NaN

print(np.nan==np.NAN) False print(np.nan==np.NaN) False print(np.nan != np.NAN) True

numpy.isnan()

test element_wise for nan and return result as a boolean array

x = np.array([1,2,3,np.nan,10]) print(x) y = np.isnan(x) print(y) z = np.count_nonzero(y) print(z) [ 1. 2. 3. nan 10.] [False False False True False] 1

numpy.inf

表示正无穷大Inf = inf = infty = Infinity = PINF

numpy.pi

表示圆周率 pi = 3.1415926

numpy.e

表示自然常数 e = 2.7182…

数据类型

常见数据类型

创建数据类型

class dtype(object): def __init__(self,obj,align=False,copy=False): pass import numpy as np a = np.dtype("b1") print(a.type) print(a.itemsize) a = np.dtype("i1") print(a.type) print(a.itemsize) a = np.dtype("i2") print(a.type) print(a.itemsize) a = np.dtype("i4") print(a.type) print(a.itemsize) a = np.dtype("i8") print(a.type) print(a.itemsize) a = np.dtype("u1") print(a.type) print(a.itemsize) a = np.dtype("u2") print(a.type) print(a.itemsize) a = np.dtype("u4") print(a.type) print(a.itemsize) a = np.dtype("u8") print(a.type) print(a.itemsize) a = np.dtype("f2") print(a.type) print(a.itemsize) a = np.dtype("f4") print(a.type) print(a.itemsize) a = np.dtype("f8") print(a.type) print(a.itemsize) a = np.dtype("S") print(a.type) print(a.itemsize) a = np.dtype("S3") print(a.type) print(a.itemsize) a = np.dtype("U3") print(a.type) print(a.itemsize) <class 'numpy.bool_'> 1 <class 'numpy.int8'> 1 <class 'numpy.int16'> 2 <class 'numpy.int32'> 4 <class 'numpy.int64'> 8 <class 'numpy.uint8'> 1 <class 'numpy.uint16'> 2 <class 'numpy.uint32'> 4 <class 'numpy.uint64'> 8 <class 'numpy.float16'> 2 <class 'numpy.float32'> 4 <class 'numpy.float64'> 8 <class 'numpy.bytes_'> 0 <class 'numpy.bytes_'> 3 <class 'numpy.str_'> 12 class iinfo(object): def __init__(self,int_type): pass def min(self): pass def max(self): pass ii16 = np.iinfo(np.int16) print(ii16.min) print(ii16.max) -32768 32767 ii32 = np.iinfo(np.int32) print(ii32.min) print(ii32.max) -2147483648 2147483647 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) 16 -65500.0 65500.0 0.000977 ff32 = np.finfo(np.float32) print(ff32.bits) print(ff32.min) print(ff32.max) print(ff32.eps) 32 -3.4028235e+38 3.4028235e+38 1.1920929e-07

时间日期和时间增量

datetime64基础

a = np.datetime64("2020-10-20") print(a,a.dtype) 2020-10-20 datetime64[D] #从字符串创建datetime64类型,默认情况下,numpy会根据字符串自动选择对应的单位 a = np.datetime64("2020-10") print(a,a.dtype) a = np.datetime64("2020-10-20 20:20:20") print(a,a.dtype) a = np.datetime64("2020-10-20 20:20") print(a,a.dtype) a = np.datetime64("2020-10-20 20") 2020-10 datetime64[M] 2020-10-20T20:20:20 datetime64[s] 2020-10-20T20:20 datetime64[m] a = np.datetime64("2020/10/20") --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-41-9002d3188054> in <module> ----> 1 a = np.datetime64("2020/10/20") ValueError: Error parsing datetime string "2020/10/20" at position 4 a = np.datetime64("2020 10 20") --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-42-44fee002feaf> in <module> ----> 1 a = np.datetime64("2020 10 20") ValueError: Error parsing datetime string "2020 10 20" at position 4 #从字符串创建datetime64类型,可以强制指定使用的单位。 a = np.datetime64("2020-10","D") print(a,a.dtype) 2020-10-01 datetime64[D] a = np.datetime64("2020-10-20","Y") print(a,a.dtype) print(np.datetime64("2020-03") == np.datetime64("2020-03-01")) print(np.datetime64("2020-03") == np.datetime64("2020-03-02")) 2020 datetime64[Y] True False #从字符串创建datetime64数组时,如果单位不统一,则一律转换成其中最小的单位 a = np.array(["2020-10","2020-10-20","2020-10-20 20:00","2020"],dtype="datetime64") print(a,a.dtype) ['2020-10-01T00:00' '2020-10-20T00:00' '2020-10-20T20:00' '2020-01-01T00:00'] datetime64[m] #利用arange()创建datetime64数组,用于生成日期范围 a = np.arange("2020-10-01","2020-10-20",dtype=np.datetime64) print(a,a.dtype) ['2020-10-01' '2020-10-02' '2020-10-03' '2020-10-04' '2020-10-05' '2020-10-06' '2020-10-07' '2020-10-08' '2020-10-09' '2020-10-10' '2020-10-11' '2020-10-12' '2020-10-13' '2020-10-14' '2020-10-15' '2020-10-16' '2020-10-17' '2020-10-18' '2020-10-19'] datetime64[D] #datetime64 和 timedelta64 运算 a = np.datetime64("2020-10-20") - np.datetime64("2020-10-01") b = np.datetime64("2020-10-20") - np.datetime64("2020-10-01 10:00") c = np.datetime64("2020-10-20") - np.datetime64("2020-10-01 10:00","D") print(a,a.dtype) print(b,b.dtype) print(c,c.dtype) 19 days timedelta64[D] 26760 minutes timedelta64[m] 19 days timedelta64[D] a = np.datetime64("2020-10") + np.timedelta64(20,"D") b = np.datetime64("2020-10-20 11:28") + np.timedelta64(10,"h") print(a,a.dtype) print(b,b.dtype) 2020-10-21 datetime64[D] 2020-10-20T21:28 datetime64[m]

生成timedelta64时,要注意年(“Y”)和月(“M”)这两个单位无法和其他单位进行运算(一年有几天?一个月有几个小时?这些都是不确定的)

a = np.timedelta64(1,"Y") b = np.timedelta64(a,"M") print(a) print(b) 1 years 12 months c = np.timedelta64(1,"h") d = np.timedelta64(c,"m") print(c) print(d) 1 hours 60 minutes print(np.timedelta64(a,"D")) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-62-eee3145511d1> in <module> ----> 1 print(np.timedelta64(a,"D")) TypeError: Cannot cast NumPy timedelta64 scalar from metadata [Y] to [D] according to the rule 'same_kind' print(np.timedelta64(b,"D")) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-63-f6258b0667a2> in <module> ----> 1 print(np.timedelta64(b,"D")) TypeError: Cannot cast NumPy timedelta64 scalar from metadata [M] to [D] according to the rule 'same_kind' #timedelta64的运算 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(c) print(d) print(a+b) print(a-b) 1 years 6 months 1 weeks 1 days 18 months 6 months print(2*b) print(a/b) 12 months 2.0 print(c/d) 7.0 print(c%e) 7 days #numpy.datetime64 与 datetime.datetime 相互转化 import datetime df = datetime.datetime(year=2020,month=6,day=1,hour=20,minute=5,second=30) df datetime.datetime(2020, 6, 1, 20, 5, 30) df64 = np.datetime64(df,"s") print(df,df64,df64.dtype) 2020-06-01 20:05:30 2020-06-01T20:05:30 datetime64[s] df2 = df64.astype(datetime.datetime) print(df2,type(df2)) 2020-06-01 20:05:30 <class 'datetime.datetime'>

将指定的偏移量应用于工作日,单位天(‘D’)。计算下一个工作日,如果当前日期为非工作日,默认报错。可以指定 forward 或 backward 规则来避免报错。(一个是向前取第一个有效的工作日,一个是向后取第一个有效的工作日)

a = np.busday_offset("2020-10-20",offsets=1) print(a) a = np.busday_offset("2020-10-18",offsets=1) print(a) 2020-10-21 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-11-9402e24e50cc> in <module> 1 a = np.busday_offset("2020-10-20",offsets=1) 2 print(a) ----> 3 a = np.busday_offset("2020-10-18",offsets=1) 4 print(a) 5 a = np.busday_offset("2020-10-18",offsets=0,roll="forward") ValueError: Non-business day date in busday_offset a = np.busday_offset("2020-10-18",offsets=0,roll="forward") b = np.busday_offset("2020-10-18",offsets=0,roll="backward") print(a) print(b) 2020-10-19 2020-10-16 a = np.busday_offset("2020-10-18",offsets=1,roll="forward") b = np.busday_offset("2020-10-18",offsets=1,roll="backward") print(a) print(b) 2020-10-20 2020-10-19 a = np.is_busday("2020-10-20") print(a) b = np.is_busday("2020-10-18") print(b) True False begindates = np.datetime64("2020-10-20") enddates = np.datetime64("2020-12-31") a = np.arange(begindates,enddates,dtype="datetime64") b = np.count_nonzero(np.is_busday(a)) print(a) print(b) ['2020-10-20' '2020-10-21' '2020-10-22' '2020-10-23' '2020-10-24' '2020-10-25' '2020-10-26' '2020-10-27' '2020-10-28' '2020-10-29' '2020-10-30' '2020-10-31' '2020-11-01' '2020-11-02' '2020-11-03' '2020-11-04' '2020-11-05' '2020-11-06' '2020-11-07' '2020-11-08' '2020-11-09' '2020-11-10' '2020-11-11' '2020-11-12' '2020-11-13' '2020-11-14' '2020-11-15' '2020-11-16' '2020-11-17' '2020-11-18' '2020-11-19' '2020-11-20' '2020-11-21' '2020-11-22' '2020-11-23' '2020-11-24' '2020-11-25' '2020-11-26' '2020-11-27' '2020-11-28' '2020-11-29' '2020-11-30' '2020-12-01' '2020-12-02' '2020-12-03' '2020-12-04' '2020-12-05' '2020-12-06' '2020-12-07' '2020-12-08' '2020-12-09' '2020-12-10' '2020-12-11' '2020-12-12' '2020-12-13' '2020-12-14' '2020-12-15' '2020-12-16' '2020-12-17' '2020-12-18' '2020-12-19' '2020-12-20' '2020-12-21' '2020-12-22' '2020-12-23' '2020-12-24' '2020-12-25' '2020-12-26' '2020-12-27' '2020-12-28' '2020-12-29' '2020-12-30'] 52 np.is_busday(a) array([ True, True, True, True, False, False, True, True, True, True, True, False, False, True, True, True, True, True, False, False, True, True, True, True, True, False, False, True, True, True, True, True, False, False, True, True, True, True, True, False, False, True, True, True, True, True, False, False, True, True, True, True, True, False, False, True, True, True, True, True, False, False, True, True, True, True, True, False, False, True, True, True]) #自定义周期掩码值,即指定一周中哪些星期是工作日 a = np.is_busday("2020-10-20",weekmask=[1,1,1,1,1,0,0]) b = np.is_busday("2020-10-20",weekmask=[1,0,1,1,1,1,0]) print(a) print(b) True False #返回两个日期之间的工作日数量 begindates = np.datetime64("2020-10-20") enddates = np.datetime64("2020-10-31") a = np.busday_count(begindates,enddates) b = np.busday_count(enddates,begindates) print(a) print(b) 9 -9

数组的创建

1.依据现有数据来创建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)) [0 1 2 3 4] <class 'numpy.ndarray'> [0 1 2 3 4] <class 'numpy.ndarray'> #创建二维数组 c = np.array([[1,2,3,4,5,6], [7,8,9,10,11,12]]) print(c,type(c)) [[ 1 2 3 4 5 6] [ 7 8 9 10 11 12]] <class 'numpy.ndarray'> x = [[1,2,3,4,5,6],[7,8,9,10,11,12]] y = np.array(x) z = np.asarray(x) print(y,type(y)) print(z,type(z)) print(x,type(x)) [[ 1 2 3 4 5 6] [ 7 8 9 10 11 12]] <class 'numpy.ndarray'> [[ 1 2 3 4 5 6] [ 7 8 9 10 11 12]] <class 'numpy.ndarray'> [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]] <class 'list'>
最新回复(0)