在 Numpy 中測試陣列值以判斷是否為 NaT(非時間)
若要測試陣列是否為 NaT,請在 Python Numpy 中使用 numpy.isnat() 方法。該條件廣播整個輸入值。在條件為 True 的位置,out 陣列將設定為 ufunc 結果。在其他位置,out 陣列將保留其原始值。請注意,如果透過預設 out=None 建立了一個未初始化的 out 陣列,那麼在條件為 False 的位置中,out 陣列將保持未初始化。
out 是儲存結果的位置。如果提供了 out,則它必須具有輸入值廣播的形狀。如果未提供或為 None,則返回一個新建的陣列。元組(僅可能作為關鍵字引數)的長度必須等於輸出的數量。
步驟
首先,匯入所需庫 −
import numpy as np
使用一些 nat 和 date 值建立陣列 −
arr = np.array(["2021-12-22", "NaT", "NAT", "nAt", '2021-12'], dtype="datetime64[ns]")
顯示陣列 −
print("Array...
", arr)獲取陣列的型別 −
print("
Our Array type...
", arr.dtype)
獲取陣列的維度 −
print("
Our Array Dimensions...
",arr.ndim)獲取陣列中的元素數量 −
print("
Number of elements...
", arr.size)
若要在 PHP Numpy 中測試陣列是否為 NaT,請使用 numpy.isnat() 方法 −
print("
Test array for NaT...
",np.isnat(arr))示例
import numpy as np
# Create an array with some nat and date values
arr = np.array(["2021-12-22", "NaT", "NAT", "nAt", '2021-12'], dtype="datetime64[ns]")
# Display the array
print("Array...
", arr)
# Get the type of the array
print("
Our Array type...
", arr.dtype)
# Get the dimensions of the Array
print("
Our Array Dimensions...
",arr.ndim)
# Get the number of elements in the Array
print("
Number of elements...
", arr.size)
# To test array for NaT, use the numpy.isnat() method in Python Numpy
print("
Test array for NaT...
",np.isnat(arr))輸出
Array... ['2021-12-22T00:00:00.000000000' 'NaT' 'NaT' 'NaT' '2021-12-01T00:00:00.000000000'] Our Array type... datetime64[ns] Our Array Dimensions... 1 Number of elements... 5 Test array for NaT... [False True True True False]
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP