Cách lưu trữ tọa độ trong Python

Xarray cung cấp các quy trình lập chỉ mục cực kỳ linh hoạt, kết hợp các tính năng tốt nhất của NumPy và pandas để chọn dữ liệu

Cách cơ bản nhất để truy cập các phần tử của đối tượng

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
4 là sử dụng cú pháp
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
5 của Python, chẳng hạn như
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
6, trong đó
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
7 và
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
8 đều là số nguyên. Vì các đối tượng xarray có thể lưu trữ các tọa độ tương ứng với từng chiều của một mảng, nên cũng có thể lập chỉ mục dựa trên nhãn tương tự như
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
9. Trong lập chỉ mục dựa trên nhãn, vị trí phần tử
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
7 được tra cứu tự động từ các giá trị tọa độ

Kích thước của các đối tượng xarray có tên, vì vậy bạn cũng có thể tra cứu kích thước theo tên, thay vì nhớ thứ tự vị trí của chúng

Tổng quan nhanh#

Tổng cộng, xarray hỗ trợ bốn loại lập chỉ mục khác nhau, như được mô tả bên dưới và được tóm tắt trong bảng này

tra cứu thứ nguyên

tra cứu chỉ mục

cú pháp

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
4

cú pháp

In [16]: da.reindex[x=[1.1, 1.5], method="nearest", tolerance=0.2]
Out[16]: 

array[[ 2., nan]]
Coordinates:
  * x        [x] float64 1.1 1.5
2

vị trí

Theo số nguyên

In [16]: da.reindex[x=[1.1, 1.5], method="nearest", tolerance=0.2]
Out[16]: 

array[[ 2., nan]]
Coordinates:
  * x        [x] float64 1.1 1.5
3

Không có sẵn

vị trí

Theo nhãn

In [16]: da.reindex[x=[1.1, 1.5], method="nearest", tolerance=0.2]
Out[16]: 

array[[ 2., nan]]
Coordinates:
  * x        [x] float64 1.1 1.5
4

Không có sẵn

Bằng tên

Theo số nguyên

In [16]: da.reindex[x=[1.1, 1.5], method="nearest", tolerance=0.2]
Out[16]: 

array[[ 2., nan]]
Coordinates:
  * x        [x] float64 1.1 1.5
5 hoặc
In [16]: da.reindex[x=[1.1, 1.5], method="nearest", tolerance=0.2]
Out[16]: 

array[[ 2., nan]]
Coordinates:
  * x        [x] float64 1.1 1.5
6

In [16]: da.reindex[x=[1.1, 1.5], method="nearest", tolerance=0.2]
Out[16]: 

array[[ 2., nan]]
Coordinates:
  * x        [x] float64 1.1 1.5
7 hoặc
In [16]: da.reindex[x=[1.1, 1.5], method="nearest", tolerance=0.2]
Out[16]: 

array[[ 2., nan]]
Coordinates:
  * x        [x] float64 1.1 1.5
8

Bằng tên

Theo nhãn

In [16]: da.reindex[x=[1.1, 1.5], method="nearest", tolerance=0.2]
Out[16]: 

array[[ 2., nan]]
Coordinates:
  * x        [x] float64 1.1 1.5
9 hoặc
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
60

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
61 hoặc
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
62

Có thể lập chỉ mục nâng cao hơn cho tất cả các phương thức bằng cách cung cấp đối tượng

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
4 làm chỉ mục. Xem Lập chỉ mục theo vectơ để biết chi tiết.

Lập chỉ mục vị trí#

Lập chỉ mục một

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
4 trực tiếp hoạt động [hầu hết] giống như đối với các mảng có nhiều mảng, ngoại trừ đối tượng được trả về luôn là một DataArray khác

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
1

Các thuộc tính được duy trì trong tất cả các hoạt động lập chỉ mục

Cảnh báo

Lập chỉ mục theo vị trí khác với NumPy khi lập chỉ mục với nhiều mảng như

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
65, như được mô tả trong Lập chỉ mục theo vectơ .

Xarray cũng hỗ trợ lập chỉ mục dựa trên nhãn, giống như gấu trúc. Bởi vì chúng tôi sử dụng một

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
66 dưới mui xe, lập chỉ mục dựa trên nhãn rất nhanh. Để lập chỉ mục dựa trên nhãn, hãy sử dụng thuộc tính
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
67

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
5

Trong ví dụ này, phần được chọn là một phần con của mảng trong phạm vi '2000-01-01'. '2000-01-02' dọc theo thời gian tọa độ đầu tiên và với giá trị 'IA' từ không gian tọa độ thứ hai

Bạn có thể thực hiện bất kỳ thao tác lập chỉ mục nhãn nào được hỗ trợ bởi pandas, bao gồm lập chỉ mục với từng phần, lát và danh sách/mảng nhãn, cũng như lập chỉ mục với mảng boolean. Giống như gấu trúc, lập chỉ mục dựa trên nhãn trong xarray bao gồm cả giới hạn bắt đầu và giới hạn dừng

Đặt giá trị với lập chỉ mục dựa trên nhãn cũng được hỗ trợ

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
6

Lập chỉ mục với tên thứ nguyên #

Với tên thứ nguyên, chúng ta không phải dựa vào thứ tự thứ nguyên và có thể sử dụng chúng một cách rõ ràng để cắt dữ liệu. Có hai cách để làm điều này

  1. Sử dụng các phương pháp thuận tiện

    In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]
    
    In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
    Out[13]: 
    
    array[[2, 3]]
    Coordinates:
      * x        [x] int64 1 2
    
    In [14]: da.sel[x=0.1, method="backfill"]
    Out[14]: 
    
    array[2]
    Coordinates:
        x        int64 1
    
    In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
    Out[15]: 
    
    array[[1, 2, 2, 3, 3]]
    Coordinates:
      * x        [x] float64 0.5 1.0 1.5 2.0 2.5
    
    68 và
    In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]
    
    In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
    Out[13]: 
    
    array[[2, 3]]
    Coordinates:
      * x        [x] int64 1 2
    
    In [14]: da.sel[x=0.1, method="backfill"]
    Out[14]: 
    
    array[2]
    Coordinates:
        x        int64 1
    
    In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
    Out[15]: 
    
    array[[1, 2, 2, 3, 3]]
    Coordinates:
      * x        [x] float64 0.5 1.0 1.5 2.0 2.5
    
    69

    In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]
    
    In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
    Out[13]: 
    
    array[[2, 3]]
    Coordinates:
      * x        [x] int64 1 2
    
    In [14]: da.sel[x=0.1, method="backfill"]
    Out[14]: 
    
    array[2]
    Coordinates:
        x        int64 1
    
    In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
    Out[15]: 
    
    array[[1, 2, 2, 3, 3]]
    Coordinates:
      * x        [x] float64 0.5 1.0 1.5 2.0 2.5
    
    9

  2. Sử dụng từ điển làm đối số cho lập chỉ mục mảng dựa trên vị trí hoặc nhãn

    In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]
    
    In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
    Out[13]: 
    
    array[[2, 3]]
    Coordinates:
      * x        [x] int64 1 2
    
    In [14]: da.sel[x=0.1, method="backfill"]
    Out[14]: 
    
    array[2]
    Coordinates:
        x        int64 1
    
    In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
    Out[15]: 
    
    array[[1, 2, 2, 3, 3]]
    Coordinates:
      * x        [x] float64 0.5 1.0 1.5 2.0 2.5
    
    0

Các đối số cho các phương thức này có thể là bất kỳ đối tượng nào có thể lập chỉ mục mảng dọc theo thứ nguyên được cung cấp bởi từ khóa, e. g. , nhãn cho một giá trị riêng lẻ, đối tượng Python

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
10 hoặc mảng 1 chiều

Ghi chú

Chúng tôi rất muốn có thể lập chỉ mục với các tên thứ nguyên được gắn nhãn bên trong dấu ngoặc, nhưng thật không may, Python chưa hỗ trợ lập chỉ mục với các đối số từ khóa như

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
11

Tra cứu hàng xóm gần nhất #

Các phương pháp lựa chọn dựa trên nhãn

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
68,
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
13 và
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
14 đều hỗ trợ đối số từ khóa
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
15 và
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
16. Tham số phương thức cho phép tra cứu hàng xóm gần nhất [không chính xác] bằng cách sử dụng các phương thức
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
17,
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
18 hoặc
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
19

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5

Dung sai giới hạn khoảng cách tối đa cho các kết quả khớp hợp lệ với tra cứu không chính xác

________số 8

Tham số phương thức chưa được hỗ trợ nếu bất kỳ đối số nào của

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
30 là một đối tượng
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
10

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
6

Tuy nhiên, bạn không cần sử dụng

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
15 để thực hiện việc cắt lát không chính xác. Cắt đã trả về tất cả các giá trị bên trong phạm vi [bao gồm], miễn là các nhãn chỉ mục tăng đều

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
1

Các trục lập chỉ mục với các nhãn đơn điệu giảm dần cũng hoạt động, miễn là các đối số

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
10 hoặc
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
34 cũng đang giảm dần

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
3

Ghi chú

Nếu bạn muốn nội suy dọc theo tọa độ thay vì tra cứu các hàng xóm gần nhất, hãy sử dụng

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
35 và
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
36. Xem nội suy để biết chi tiết.

Lập chỉ mục tập dữ liệu #

Chúng ta cũng có thể sử dụng các phương thức này để lập chỉ mục đồng thời tất cả các biến trong tập dữ liệu, trả về tập dữ liệu mới

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
50

Lập chỉ mục vị trí trên tập dữ liệu không được hỗ trợ vì thứ tự thứ nguyên trong tập dữ liệu hơi mơ hồ [có thể khác nhau giữa các mảng khác nhau]. Tuy nhiên, bạn có thể lập chỉ mục bình thường với tên thứ nguyên

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
51

Thả nhãn và kích thước #

Phương thức

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
37 trả về một đối tượng mới với các nhãn chỉ mục được liệt kê dọc theo thứ nguyên bị loại bỏ

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
52

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
38 là cả một phương pháp
In [16]: da.reindex[x=[1.1, 1.5], method="nearest", tolerance=0.2]
Out[16]: 

array[[ 2., nan]]
Coordinates:
  * x        [x] float64 1.1 1.5
2 và
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
4

Sử dụng

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
501 để xóa kích thước đầy đủ từ Tập dữ liệu. Bất kỳ biến nào có các thứ nguyên này cũng bị loại bỏ

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
53

Tạo mặt nạ với
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
2#

Các phương thức lập chỉ mục trên các đối tượng xarray thường trả về một tập hợp con của dữ liệu gốc. Tuy nhiên, đôi khi sẽ hữu ích khi chọn một đối tượng có cùng hình dạng với dữ liệu gốc, nhưng có một số thành phần bị che đi. Để thực hiện loại lựa chọn này trong xarray, hãy sử dụng

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
503

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
54

Điều này đặc biệt hữu ích cho việc lập chỉ mục rời rạc của dữ liệu đa chiều, e. g. , để áp dụng mặt nạ 2D cho hình ảnh. Lưu ý rằng

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
2 tuân theo tất cả các quy tắc căn chỉnh và phát sóng xarray thông thường cho các hoạt động nhị phân [e. g. ,
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
505] giữa đối tượng được lập chỉ mục và điều kiện, như được mô tả trong Tính toán .

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
55

Theo mặc định,

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
2 duy trì kích thước ban đầu của dữ liệu. Đối với các trường hợp kích thước dữ liệu đã chọn nhỏ hơn nhiều so với dữ liệu gốc, hãy sử dụng tùy chọn
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
507 clip phối hợp các phần tử được che hoàn toàn

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
56

Chọn giá trị với
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
3#

Để kiểm tra xem các phần tử của một đối tượng xarray có chứa một đối tượng duy nhất hay không, bạn có thể so sánh với toán tử đẳng thức

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
509 [e. g. ,
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
510]. Để kiểm tra nhiều giá trị, hãy sử dụng
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
511

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
57

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
511 hoạt động đặc biệt tốt với
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
503 để hỗ trợ lập chỉ mục theo mảng chưa phải là nhãn của mảng

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
58

Tuy nhiên, một số thận trọng là theo thứ tự. khi được thực hiện nhiều lần, kiểu lập chỉ mục này chậm hơn đáng kể so với sử dụng

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
68

Lập chỉ mục Vectorized #

Giống như numpy và pandas, xarray hỗ trợ lập chỉ mục nhiều phần tử mảng cùng một lúc theo cách vector hóa

Nếu bạn chỉ cung cấp số nguyên, lát hoặc mảng không được gắn nhãn [mảng không có tên thứ nguyên, chẳng hạn như

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
515,
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
516, nhưng không phải là
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
517 hoặc
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
518] thì việc lập chỉ mục có thể được hiểu là trực giao. Mỗi thành phần bộ chỉ mục chọn độc lập dọc theo kích thước tương ứng, tương tự như cách hoạt động của chỉ mục vectơ trong Fortran hoặc MATLAB hoặc sau khi sử dụng trình trợ giúp
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
519

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
59

Để linh hoạt hơn, bạn có thể cung cấp các đối tượng

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
517 làm chỉ mục. Các kích thước trên các mảng kết quả được đưa ra bởi sự kết hợp có thứ tự của các kích thước của bộ chỉ mục

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
60

Các lát hoặc chuỗi/mảng không có kích thước được đặt tên được xử lý như thể chúng có cùng kích thước được lập chỉ mục dọc theo

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
61

Hơn nữa, bạn có thể sử dụng

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
517 đa chiều làm bộ chỉ mục, trong đó kích thước mảng kết quả cũng được xác định bởi kích thước của bộ chỉ mục

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
62

Tương tự như cách hoạt động của tính năng lập chỉ mục nâng cao của NumPy, tính năng lập chỉ mục được vector hóa cho xarray dựa trên quy tắc phát sóng của chúng tôi. Xem Quy tắc lập chỉ mục để biết thông số kỹ thuật đầy đủ.

Lập chỉ mục vectorized cũng hoạt động với

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
522,
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
67 và
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
524

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
63

Các phương pháp này cũng có thể được áp dụng cho các đối tượng

In [16]: da.reindex[x=[1.1, 1.5], method="nearest", tolerance=0.2]
Out[16]: 

array[[ 2., nan]]
Coordinates:
  * x        [x] float64 1.1 1.5
2

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
64

Lập chỉ mục vectorized có thể được sử dụng để trích xuất thông tin từ các ô lưới quan tâm gần nhất, ví dụ: các ô lưới mô hình khí hậu gần nhất cho một bộ sưu tập vĩ độ và kinh độ của trạm thời tiết được chỉ định

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
65

Mẹo

Nếu bạn đang tải dữ liệu của mình từ đĩa một cách lười biếng, thì không phải mọi hình thức lập chỉ mục vector hóa đều được hỗ trợ [hoặc nếu được hỗ trợ, có thể không được hỗ trợ hiệu quả]. Bạn có thể thấy hiệu suất tăng lên bằng cách tải dữ liệu của mình vào bộ nhớ trước, e. g. , với

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
526

Ghi chú

Nếu một bộ chỉ mục là một

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
517, thì tọa độ của nó không được xung đột với phần con đã chọn của mảng đích [ngoại trừ các thứ nguyên được lập chỉ mục rõ ràng với
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
34/
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
529]. Nếu không,
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
530 sẽ được nâng lên

Gán giá trị với lập chỉ mục #

Để chọn và gán giá trị cho một phần của

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
517, bạn có thể sử dụng lập chỉ mục với
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
34

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
66

hoặc

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
503

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
67

Lập chỉ mục vectorized cũng có thể được sử dụng để gán giá trị cho đối tượng xarray

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
68

Giống như

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
534, việc gán giá trị đôi khi hoạt động khác với những gì người ta có thể mong đợi

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
69

Trường hợp phần tử thứ 0 sẽ bị trừ 1 lần duy nhất. Điều này là do

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
535 được gọi ba lần, thay vì
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
536. Xem Gán giá trị cho các mảng được lập chỉ mục để biết chi tiết

Ghi chú

Mảng Dask không hỗ trợ gán giá trị [xem Tính toán song song với Dask để biết chi tiết].

Ghi chú

Các tọa độ trong cả mảng bên trái và bên phải không được xung đột với nhau. Nếu không,

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
530 sẽ được nâng lên

Cảnh báo

Không cố gán giá trị khi sử dụng bất kỳ phương pháp lập chỉ mục nào

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
522 hoặc
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
524

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
90

Thay vào đó, các giá trị có thể được chỉ định bằng cách sử dụng chỉ mục dựa trên từ điển

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
91

Gán các giá trị với lập chỉ mục chuỗi bằng cách sử dụng

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
529 hoặc
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
541 không thành công

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
92

Bạn cũng có thể gán giá trị cho tất cả các biến của một

In [16]: da.reindex[x=[1.1, 1.5], method="nearest", tolerance=0.2]
Out[16]: 

array[[ 2., nan]]
Coordinates:
  * x        [x] float64 1.1 1.5
2 cùng một lúc

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
93

Kích thước có thể khác nhau giữa các biến trong tập dữ liệu, nhưng tất cả các biến cần phải có ít nhất kích thước được chỉ định trong từ điển bộ chỉ mục. Các giá trị mới phải là vô hướng,

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
4 hoặc chính
In [16]: da.reindex[x=[1.1, 1.5], method="nearest", tolerance=0.2]
Out[16]: 

array[[ 2., nan]]
Coordinates:
  * x        [x] float64 1.1 1.5
2 chứa tất cả các biến cũng xuất hiện trong tập dữ liệu được sửa đổi

Lập chỉ mục nâng cao hơn#

Việc sử dụng các đối tượng

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
517 làm chỉ mục cho phép lập chỉ mục rất linh hoạt. Sau đây là một ví dụ về việc lập chỉ mục theo chiều kim đồng hồ

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
94

trong đó ba phần tử tại

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
546 được chọn và ánh xạ dọc theo một chiều mới
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
547

Nếu bạn muốn thêm một tọa độ vào thứ nguyên mới

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
547, bạn có thể cung cấp một
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
4 với một tọa độ,

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
95

Tương tự, lập chỉ mục theo điểm dựa trên nhãn cũng có thể thực hiện được bằng phương pháp

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
529

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
96

Căn chỉnh và lập chỉ mục lại#

Xarray's

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
551,
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
552 và
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
553 áp đặt một
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
4 hoặc
In [16]: da.reindex[x=[1.1, 1.5], method="nearest", tolerance=0.2]
Out[16]: 

array[[ 2., nan]]
Coordinates:
  * x        [x] float64 1.1 1.5
2 lên một bộ tọa độ mới tương ứng với các kích thước. Các giá trị ban đầu được tập hợp con cho các nhãn chỉ mục vẫn được tìm thấy trong các nhãn mới và các giá trị tương ứng với các nhãn mới không được tìm thấy trong đối tượng ban đầu được điền bằng NaN

Các hoạt động Xarray kết hợp nhiều đối tượng thường tự động sắp xếp các đối số của chúng để chia sẻ cùng một chỉ mục. Tuy nhiên, căn chỉnh thủ công có thể hữu ích để kiểm soát tốt hơn và tăng hiệu suất

Để lập chỉ mục lại một thứ nguyên cụ thể, hãy sử dụng

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
13

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
97

Phương pháp

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
14 là một phím tắt hữu ích. Để chứng minh, chúng tôi sẽ tạo một tập hợp con DataArray với các giá trị mới

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
98

Lập chỉ mục lại

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
558 với
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
559 chọn ra hai giá trị đầu tiên dọc theo mỗi thứ nguyên

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
99

Thao tác ngược lại yêu cầu chúng tôi lập chỉ mục lại thành hình dạng lớn hơn, vì vậy chúng tôi điền vào các giá trị còn thiếu bằng NaN

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
00

Hàm

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
560 cho phép chúng tôi thực hiện các phép nối cơ sở dữ liệu linh hoạt hơn như
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
561,
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
562,
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
563 và
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
564

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
01

Cả

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
552 và
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
553 đều hoạt động hoán đổi cho nhau giữa các đối tượng
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
4 và
In [16]: da.reindex[x=[1.1, 1.5], method="nearest", tolerance=0.2]
Out[16]: 

array[[ 2., nan]]
Coordinates:
  * x        [x] float64 1.1 1.5
2 và với bất kỳ số lượng tên kích thước phù hợp nào

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
02

Thiếu nhãn tọa độ#

Nhãn tọa độ cho từng thứ nguyên là tùy chọn [kể từ xarray v0. 9]. Lập chỉ mục dựa trên nhãn với

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
529 và
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
34 sử dụng lập chỉ mục dựa trên số nguyên, vị trí tiêu chuẩn làm dự phòng cho các thứ nguyên không có nhãn tọa độ

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
03

Căn chỉnh giữa các đối tượng xarray trong đó một hoặc cả hai không có nhãn tọa độ chỉ thành công nếu tất cả các kích thước cùng tên có cùng độ dài. Mặt khác, nó làm tăng một lỗi thông tin

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
04

Chỉ mục cơ bản#

Xarray sử dụng

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
66 nội bộ để thực hiện các thao tác lập chỉ mục. Nếu bạn cần truy cập các chỉ mục cơ bản, chúng có sẵn thông qua thuộc tính
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
572

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
05

Sử dụng

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
573 để lấy chỉ mục cho một thứ nguyên, quay trở lại mặc định là
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
574 nếu nó không có nhãn tọa độ

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
06

Bản sao so với. Lượt xem#

Việc lập chỉ mục mảng có trả về dạng xem hay bản sao của dữ liệu cơ bản hay không tùy thuộc vào bản chất của nhãn

Để lập chỉ mục vị trí [số nguyên], xarray tuân theo các quy tắc giống như NumPy

  • Lập chỉ mục vị trí chỉ với số nguyên và lát trả về chế độ xem

  • Lập chỉ mục vị trí với mảng hoặc danh sách trả về một bản sao

Các quy tắc lập chỉ mục dựa trên nhãn phức tạp hơn

  • Lập chỉ mục dựa trên nhãn chỉ với các lát trả về một chế độ xem

  • Lập chỉ mục dựa trên nhãn với các mảng trả về một bản sao

  • Lập chỉ mục dựa trên nhãn với các đại lượng vô hướng trả về một dạng xem hoặc một bản sao, tùy thuộc vào việc bộ chỉ mục vị trí tương ứng có thể được biểu diễn dưới dạng một số nguyên hay một đối tượng lát. Các quy tắc chính xác được xác định bởi gấu trúc

Cho dù dữ liệu là bản sao hay chế độ xem thì xarray có thể dự đoán được nhiều hơn so với pandas, do đó, không giống như pandas, xarray không tạo cảnh báo SettingWithCopy. Tuy nhiên, bạn vẫn nên tránh chuyển nhượng với chỉ mục chuỗi

Lập chỉ mục đa cấp#

Cũng giống như gấu trúc, có thể lập chỉ mục nâng cao trên các chỉ mục đa cấp với

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
67 và
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
524. Bạn có thể cắt một đa chỉ mục bằng cách cung cấp nhiều chỉ mục, tôi. e. , một bộ lát cắt, nhãn, danh sách nhãn hoặc bất kỳ bộ chọn nào được gấu trúc cho phép

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
07

Bạn cũng có thể chọn nhiều phần tử bằng cách cung cấp danh sách nhãn hoặc bộ dữ liệu hoặc một phần của bộ dữ liệu

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
08

Ngoài ra, xarray hỗ trợ từ điển

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
09

Để thuận tiện,

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
524 cũng chấp nhận trực tiếp các mức đa chỉ mục làm đối số từ khóa

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
0

Lưu ý rằng khi sử dụng

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
524, không thể kết hợp bộ chỉ mục thứ nguyên với bộ chỉ mục cấp độ cho thứ nguyên đó [e. g. ,
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
579 sẽ tăng
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
580]

Giống như gấu trúc, xarray xử lý lựa chọn một phần trên đa chỉ mục [giảm cấp độ]. Như được hiển thị bên dưới, nó cũng đổi tên thứ nguyên/tọa độ khi đa chỉ mục được giảm thành một chỉ mục duy nhất

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
1

Không giống như gấu trúc, xarray không đoán liệu bạn có cung cấp chỉ mục hoặc kích thước hay không khi sử dụng

In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
67 trong một số trường hợp không rõ ràng. Ví dụ: đối với
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
582 và
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
583, xarray luôn diễn giải ['one', 'two'] và ['a', 0] lần lượt là tên và nhãn của chiều thứ nhất và thứ hai. Bạn phải chỉ định tất cả các kích thước hoặc sử dụng dấu chấm lửng trong công cụ xác định
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
67, e. g. trong ví dụ trên,
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
585 hoặc
In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]

In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
Out[13]: 

array[[2, 3]]
Coordinates:
  * x        [x] int64 1 2

In [14]: da.sel[x=0.1, method="backfill"]
Out[14]: 

array[2]
Coordinates:
    x        int64 1

In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
Out[15]: 

array[[1, 2, 2, 3, 3]]
Coordinates:
  * x        [x] float64 0.5 1.0 1.5 2.0 2.5
586

Quy tắc lập chỉ mục#

Ở đây chúng tôi mô tả các quy tắc đầy đủ mà xarray sử dụng để lập chỉ mục vector hóa. Lưu ý rằng điều này là dành cho mục đích giải thích. vì mục đích hiệu quả và để hỗ trợ các chương trình phụ trợ khác nhau, việc triển khai thực tế sẽ khác

  1. [Chỉ dành cho lập chỉ mục dựa trên nhãn. ] Tra cứu các chỉ số vị trí dọc theo mỗi chiều từ

    In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]
    
    In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
    Out[13]: 
    
    array[[2, 3]]
    Coordinates:
      * x        [x] int64 1 2
    
    In [14]: da.sel[x=0.1, method="backfill"]
    Out[14]: 
    
    array[2]
    Coordinates:
        x        int64 1
    
    In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
    Out[15]: 
    
    array[[1, 2, 2, 3, 3]]
    Coordinates:
      * x        [x] float64 0.5 1.0 1.5 2.0 2.5
    
    66 tương ứng

  2. Một đối tượng lát cắt đầy đủ

    In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]
    
    In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
    Out[13]: 
    
    array[[2, 3]]
    Coordinates:
      * x        [x] int64 1 2
    
    In [14]: da.sel[x=0.1, method="backfill"]
    Out[14]: 
    
    array[2]
    Coordinates:
        x        int64 1
    
    In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
    Out[15]: 
    
    array[[1, 2, 2, 3, 3]]
    Coordinates:
      * x        [x] float64 0.5 1.0 1.5 2.0 2.5
    
    588 được chèn cho mỗi thứ nguyên mà không cần bộ chỉ mục

  3. Các đối tượng

    In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]
    
    In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
    Out[13]: 
    
    array[[2, 3]]
    Coordinates:
      * x        [x] int64 1 2
    
    In [14]: da.sel[x=0.1, method="backfill"]
    Out[14]: 
    
    array[2]
    Coordinates:
        x        int64 1
    
    In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
    Out[15]: 
    
    array[[1, 2, 2, 3, 3]]
    Coordinates:
      * x        [x] float64 0.5 1.0 1.5 2.0 2.5
    
    10 được chuyển đổi thành mảng, được đưa ra bởi
    In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]
    
    In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
    Out[13]: 
    
    array[[2, 3]]
    Coordinates:
      * x        [x] int64 1 2
    
    In [14]: da.sel[x=0.1, method="backfill"]
    Out[14]: 
    
    array[2]
    Coordinates:
        x        int64 1
    
    In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
    Out[15]: 
    
    array[[1, 2, 2, 3, 3]]
    Coordinates:
      * x        [x] float64 0.5 1.0 1.5 2.0 2.5
    
    590

  4. Giả sử tên thứ nguyên cho bộ chỉ mục mảng không có thứ nguyên, chẳng hạn như

    In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]
    
    In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
    Out[13]: 
    
    array[[2, 3]]
    Coordinates:
      * x        [x] int64 1 2
    
    In [14]: da.sel[x=0.1, method="backfill"]
    Out[14]: 
    
    array[2]
    Coordinates:
        x        int64 1
    
    In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
    Out[15]: 
    
    array[[1, 2, 2, 3, 3]]
    Coordinates:
      * x        [x] float64 0.5 1.0 1.5 2.0 2.5
    
    515 và
    In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]
    
    In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
    Out[13]: 
    
    array[[2, 3]]
    Coordinates:
      * x        [x] int64 1 2
    
    In [14]: da.sel[x=0.1, method="backfill"]
    Out[14]: 
    
    array[2]
    Coordinates:
        x        int64 1
    
    In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
    Out[15]: 
    
    array[[1, 2, 2, 3, 3]]
    Coordinates:
      * x        [x] float64 0.5 1.0 1.5 2.0 2.5
    
    516, từ các thứ nguyên sẽ được lập chỉ mục dọc theo. Ví dụ,
    In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]
    
    In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
    Out[13]: 
    
    array[[2, 3]]
    Coordinates:
      * x        [x] int64 1 2
    
    In [14]: da.sel[x=0.1, method="backfill"]
    Out[14]: 
    
    array[2]
    Coordinates:
        x        int64 1
    
    In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
    Out[15]: 
    
    array[[1, 2, 2, 3, 3]]
    Coordinates:
      * x        [x] float64 0.5 1.0 1.5 2.0 2.5
    
    593 được hiểu là
    In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]
    
    In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
    Out[13]: 
    
    array[[2, 3]]
    Coordinates:
      * x        [x] int64 1 2
    
    In [14]: da.sel[x=0.1, method="backfill"]
    Out[14]: 
    
    array[2]
    Coordinates:
        x        int64 1
    
    In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
    Out[15]: 
    
    array[[1, 2, 2, 3, 3]]
    Coordinates:
      * x        [x] float64 0.5 1.0 1.5 2.0 2.5
    
    594

  5. Đối với mỗi biến trong một

    In [16]: da.reindex[x=[1.1, 1.5], method="nearest", tolerance=0.2]
    Out[16]: 
    
    array[[ 2., nan]]
    Coordinates:
      * x        [x] float64 1.1 1.5
    
    2 hoặc
    In [12]: da = xr.DataArray[[1, 2, 3], [["x", [0, 1, 2]]]]
    
    In [13]: da.sel[x=[1.1, 1.9], method="nearest"]
    Out[13]: 
    
    array[[2, 3]]
    Coordinates:
      * x        [x] int64 1 2
    
    In [14]: da.sel[x=0.1, method="backfill"]
    Out[14]: 
    
    array[2]
    Coordinates:
        x        int64 1
    
    In [15]: da.reindex[x=[0.5, 1, 1.5, 2, 2.5], method="pad"]
    Out[15]: 
    
    array[[1, 2, 2, 3, 3]]
    Coordinates:
      * x        [x] float64 0.5 1.0 1.5 2.0 2.5
    
    4 [mảng và tọa độ của nó]

    1. Phát tất cả các bộ lập chỉ mục có liên quan dựa trên tên thứ nguyên của chúng [xem Phát theo tên thứ nguyên để biết đầy đủ chi tiết].

    2. Lập chỉ mục mảng cơ sở bởi các bộ chỉ mục quảng bá, sử dụng các quy tắc lập chỉ mục nâng cao của NumPy

  6. Nếu bất kỳ DataArray lập chỉ mục nào có tọa độ và không tồn tại tọa độ nào có cùng tên, hãy đính kèm chúng vào đối tượng được lập chỉ mục

Chủ Đề