在作为参数传递给函数时获取 numpy 结构化数组列的名称
Grabbing the name of a numpy structured array column when passed as a parameter to a function
当尝试使用 numpy.array(结构化的 numpy 数组)时,我知道我可以通过像数组 ["col"] 这样的操作拉出一列。据我了解,这是因为 numpy.dtype.names 和结构化数组的性质。但是,当将所述数组传递给函数时,使用 numpy.dtype.name 时我没有得到列名,我得到类似 "strxxx" 的东西。如果有帮助的话,这个特定的数组是使用 numpy.genfromtxt() 和一个 csv 文件创建的。例如下面的代码
def empty_check(param):
for ind in param:
# Ignore future warning for comparison just for this instance
with warnings.catch_warnings():
warnings.simplefilter(action='ignore', category=FutureWarning)
if ind == '':
print("Please fill out required data in", param.dtype.name, "column")
结果:
Please fill out required data in str544 column
有人知道为什么出现 str544 而不是列名吗?
设置
Python 3.7.0
麻木 1.15.4
IDE: PyCharm 2018.3
你在这里犯了一个小错误。您假设 dtype.name
和 dtype.names
return 是同一件事。他们没有。
name
A bit-width name for this data-type.
names
Ordered list of field names, or None if there are no fields.
所以您看到的是字段数据类型的位宽名称。但是,如果您调用 dtype.names
,您将被 returned None
,因为您传递的 单个字段 没有任何字段 return.
据我所知,没有一种方法可以在不访问包含字段的结构化数组的情况下推断字段的名称。您很可能必须将字段名称作为参数传递给 empty_check
函数。
当尝试使用 numpy.array(结构化的 numpy 数组)时,我知道我可以通过像数组 ["col"] 这样的操作拉出一列。据我了解,这是因为 numpy.dtype.names 和结构化数组的性质。但是,当将所述数组传递给函数时,使用 numpy.dtype.name 时我没有得到列名,我得到类似 "strxxx" 的东西。如果有帮助的话,这个特定的数组是使用 numpy.genfromtxt() 和一个 csv 文件创建的。例如下面的代码
def empty_check(param):
for ind in param:
# Ignore future warning for comparison just for this instance
with warnings.catch_warnings():
warnings.simplefilter(action='ignore', category=FutureWarning)
if ind == '':
print("Please fill out required data in", param.dtype.name, "column")
结果:
Please fill out required data in str544 column
有人知道为什么出现 str544 而不是列名吗?
设置 Python 3.7.0 麻木 1.15.4 IDE: PyCharm 2018.3
你在这里犯了一个小错误。您假设 dtype.name
和 dtype.names
return 是同一件事。他们没有。
name
A bit-width name for this data-type.
names
Ordered list of field names, or None if there are no fields.
所以您看到的是字段数据类型的位宽名称。但是,如果您调用 dtype.names
,您将被 returned None
,因为您传递的 单个字段 没有任何字段 return.
据我所知,没有一种方法可以在不访问包含字段的结构化数组的情况下推断字段的名称。您很可能必须将字段名称作为参数传递给 empty_check
函数。