【问题标题】:changing int band values to names将 int 带值更改为名称
【发布时间】:2022-01-22 03:05:18
【问题描述】:

我有一个 xarray.DataArray 的内容,geotiffs_da,它是通过从几个 geoTIFF 文件中读取数据而形成的。部分xarray内容如下:

geotiffs_da_renamed 数组([[[[1065., 1041., 1036., ..., 1069., 1025., 1028.], ... [4342., 4192., 4022., ..., 3468., 3458., 3469.]]]], dtype=float32) 坐标:

  • band(波段)int32 1 2 3 4 5 6 7 8 9
  • 纬度(纬度) float64 13.8 13.8 13.8 13.8 ... 13.79 13.79 13.79
  • longitude(经度) float64 -15.79 -15.79 -15.79 ... -15.78 -15.78 -15.78
  • 时间(时间)datetime64[ns] 2021-07-18 2021-07-23 ... 2021-11-20

原来的 xarray 用 'x' 和 'y' 代替经度和纬度,我可以使用来更改它们

geotiffs_da = geotiffs_da.rename ({ 'x': 'longitude', 'y': 'latitude' })

如何更改 'band' 的内容,使其显示蓝色、绿色、红色...,而不是 1、2、3、...?

【问题讨论】:

    标签: python python-xarray


    【解决方案1】:

    assign_coords() 会这样做。文档是here

    这是一个例子:

    import xarray as xr
    import pandas as pd
    
    #============================
    # create sample dataset (ds)
    #============================
    # create dataFrame
    df = pd.DataFrame({'x':[1,2,3,4,5,6,7],
                       'y':[10,20,30,40,50,60,70]}).set_index('x')
    
    # convert to dataset
    ds = df.to_xarray()
    
    #============================
    # assign new x coordinate
    # to list of colors
    #============================
    ds = ds.assign_coords(x=['red','orange','yellow','green','blue','indigo','violet'])
    

    ds.assign_coords(x=...) 将坐标 x 分配给颜色列表。由于 Xarray 总是返回一个对象并且没有像 Pandas 那样的inplace=True 选项,因此我们必须替换原始数据集 (ds) 或将其分配给其他对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-17
      • 1970-01-01
      • 2016-04-10
      • 2022-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-14
      相关资源
      最近更新 更多