zoukankan      html  css  js  c++  java
  • Numpy:字符串转为数值型、日期型;日期型转为数值型

    一、字符串转为浮点型

    print("---------------转换数据类型---------------------")
    vector = numpy.array(["1", "2", "3"])
    print (vector.dtype)
    print (vector)
    
    vector = vector.astype(float)   # 字符串转为浮点型
    print (vector.dtype)
    print (vector)

    结果图:

    二、字符串转为日期型、日期型转为整数型

    print("========日期型数据类型转换=============")
    f = np.array(["2018","2019-01-01","2019-02-01","2019-01-02 08:08:08"])
    print(f)
    # 将f数组的元素从字符串改为日期类型
    Y = f.astype("M8[Y]")
    M = f.astype("M8[M]")
    D = f.astype("M8[D]")
    h = f.astype("M8[h]")
    m = f.astype("M8[m]")
    s = f.astype("M8[s]")
    print(Y)
    print(M)
    print(D)
    print(h)
    print(m)
    print(s)
    
    print("========将日期类型转为数值类型==========")
    # 日期类型转为数值型,计算出来的数值是从1970年开始至我们要算的日期的间隔
    YI = Y.astype("int32")
    MI = M.astype("int32")
    DI = D.astype("int32")
    hI = h.astype("int32")
    mI = m.astype("int32")
    sI = s.astype("int32")
    print(YI)
    print(MI)
    print(DI)
    print(hI)
    print(mI)
    print(sI)
    print(DI[2]-DI[1])

    结果图:

    补充知识了解:

    数据类型的简写字符码:

  • 相关阅读:
    【07】关于相等 Equals
    【06】拆箱、装箱
    【05】CTS、CLS、CLR
    判断属性存在于原型而非对象的方法
    Javascript打印网页局部的实现方案
    Jquery获取DOM绑定事件
    Bug 级别定义标准
    JavaScript中的数据类型
    <script>元素在XHTML中的用法
    CSS深入理解学习笔记之float
  • 原文地址:https://www.cnblogs.com/wodexk/p/10308535.html
Copyright © 2011-2022 走看看