将日期时间数组转换为字符串数组,在 Python 中传递小时日期时间单位
要将日期时间数组转换为字符串数组,请使用Python Numpy 中的方法。该方法返回一个与输入数组形状相同的字符串数组。第一个参数是要格式化的 UTC 时间戳数组。“units”参数设置日期时间单位来改变精度。我们已经通过了小时单位numpy.datetime_as_string()
脚步
首先,导入所需的库 -
import numpy as np
创建一个日期时间数组。'M' 类型指定日期时间 -
arr = np.arange('2022-02-20T02:10', 6*60, 60, dtype='M8[m]')
显示我们的数组 -
print("Array...\n",arr)
获取数据类型: -
print("\nArray datatype...\n",arr.dtype)
获取数组的维度 -
print("\nArray Dimensions...\n",arr.ndim)
获取阵列的形状 -
print("\nOur Array Shape...\n",arr.shape)
获取数组的元素数量 -
print("\nNumber of elements in the Array...\n",arr.size)
要将日期时间数组转换为字符串数组,请使用Python Numpy 中的方法 -numpy.datetime_as_string()
print("\nResult...\n",np.datetime_as_string(arr, unit ='h'))
示例
import numpy as np输出结果# Create an array of datetime
# The 'M' type specifies datetime
arr = np.arange('2022-02-20T02:10', 6*60, 60, dtype='M8[m]')
# Displaying our array
print("Array...\n",arr)
# Get the datatype
print("\nArray datatype...\n",arr.dtype)
# Get the dimensions of the Array
print("\nArray Dimensions...\n",arr.ndim)
# Get the shape of the Array
print("\nOur Array Shape...\n",arr.shape)
# Get the number of elements of the Array
print("\nNumber of elements in the Array...\n",arr.size)
# To convert an array of datetimes into an array of strings, use the numpy.datetime_as_string() method in Python Numpy
# The method returns an array of strings the same shape as the input array
# The first parameter is the array of UTC timestamps to format
# The "units" parameter sets the datetime unit to change the precision
# We have passed the hours unit
print("\nResult...\n",np.datetime_as_string(arr, unit ='h'))
Array...['2022-02-20T02:10' '2022-02-20T03:10' '2022-02-20T04:10'
'2022-02-20T05:10' '2022-02-20T06:10' '2022-02-20T07:10']
Array datatype...
datetime64[m]
Array Dimensions...
1
Our Array Shape...
(6,)
Number of elements in the Array...
6
Result...
['2022-02-20T02' '2022-02-20T03' '2022-02-20T04' '2022-02-20T05'
'2022-02-20T06' '2022-02-20T07']
以上是 将日期时间数组转换为字符串数组,在 Python 中传递小时日期时间单位 的全部内容, 来源链接: utcz.com/z/297204.html