如何将自定义对象分配给 xarray 数据值?
How can I assign a custom object to xarray data values?
我已经成功地使用 xarray 创建了一个 DataArray:
df_invoice_features = xr.DataArray(data=None,
dims={"y", "x"},
coords={"y": unique_invoices, "x": cols})
我创建了一个自定义 class 并将 xarray 的一个值分配给此 class 的实例:
class MyArray:
def __init__(self, s):
self.arr = np.array((s))
def set(self, idx, val):
self.arr[idx] = val
def get(self):
return self.arr
df_invoice_features.loc['basket_value_brand', invoice_id] = MyArray(len_b)
再次创建成功:
但是当我想更新这个class实例的数组时:
df_invoice_features.loc['basket_value_brand', invoice_id].set(0, 10)
它returns这个错误:
AttributeError: 'DataArray' object has no attribute 'set'
如何在 xarray 数据值中使用数组、字典或我的自定义对象?
所以 df_invoice_features.loc['basket_value_brand', invoice_id]
实际上并不 return MyArray(len_b)
。相反,它 return 是 xarray
DataArray;特别是坐标 ['basket_value_brand', invoice_id]
处完整 DataArray
的子集。这不仅包括该位置 (MyArray(len_b)
) 的值,还包括存储在该 DataArray
位置的所有其他信息;即,您的坐标、元数据等
如果您想访问该位置的实际 值 ,则必须使用 .values
;即,
df_invoice_features.loc['basket_value_brand', invoice_id].values
这应该可以找到您正在寻找的 MyArray(len_b)
。但是,我不完全清楚您想用那个 class 做什么。如果您尝试在该位置更改 DataArray 的值,查看 xarray 文档的 this 位可能特别有用。
我已经成功地使用 xarray 创建了一个 DataArray:
df_invoice_features = xr.DataArray(data=None,
dims={"y", "x"},
coords={"y": unique_invoices, "x": cols})
我创建了一个自定义 class 并将 xarray 的一个值分配给此 class 的实例:
class MyArray:
def __init__(self, s):
self.arr = np.array((s))
def set(self, idx, val):
self.arr[idx] = val
def get(self):
return self.arr
df_invoice_features.loc['basket_value_brand', invoice_id] = MyArray(len_b)
再次创建成功:
但是当我想更新这个class实例的数组时:
df_invoice_features.loc['basket_value_brand', invoice_id].set(0, 10)
它returns这个错误:
AttributeError: 'DataArray' object has no attribute 'set'
如何在 xarray 数据值中使用数组、字典或我的自定义对象?
所以 df_invoice_features.loc['basket_value_brand', invoice_id]
实际上并不 return MyArray(len_b)
。相反,它 return 是 xarray
DataArray;特别是坐标 ['basket_value_brand', invoice_id]
处完整 DataArray
的子集。这不仅包括该位置 (MyArray(len_b)
) 的值,还包括存储在该 DataArray
位置的所有其他信息;即,您的坐标、元数据等
如果您想访问该位置的实际 值 ,则必须使用 .values
;即,
df_invoice_features.loc['basket_value_brand', invoice_id].values
这应该可以找到您正在寻找的 MyArray(len_b)
。但是,我不完全清楚您想用那个 class 做什么。如果您尝试在该位置更改 DataArray 的值,查看 xarray 文档的 this 位可能特别有用。