날짜 시간 배열을 문자열 배열로 변환하려면 Python Numpy에서 numpy.datetime_as_string() 메서드를 사용하십시오. 이 메서드는 입력 배열과 같은 모양의 문자열 배열을 반환합니다. 첫 번째 매개변수는 형식화할 UTC 타임스탬프의 배열입니다. "units" 매개변수는 날짜/시간 단위를 설정하여 정밀도를 변경합니다. 시간 단위를 초과했습니다.
단계
먼저 필요한 라이브러리를 가져옵니다 -
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']