25

I would like to extract the values of the coordinate variables.

For example I create a DataArray as:

import xarray as xr
import numpy as np
import pandas as pd
years_arr=range(1982,1986)
time = pd.date_range('14/1/' + str(years_arr[0]) + ' 12:00:00', periods=len(years_arr), freq=pd.DateOffset(years=1))
lon = range(20,24)
lat = range(10,14)
arr1 = xr.DataArray(data, coords=[time, lat, lon], dims=['time', 'latitude', 'longitude'])

I now would like to output the lon values from arr1. I'm asking as arr1 going into a function so I may not have the lon values available.

3 Answers 3

32

arr1.coords['lon'] gives you longitude as a xarray.DataArray, and arr1.coords['lon'].values gives you the values as a numpy array.

Sign up to request clarification or add additional context in comments.

Comments

6

Another possible solution is:

time, lat, lon = arr1.indexes.values()

The result is a Float64Index for your lat/lon coordinates.

Comments

0

Yet other possible syntax are:

lon = arr1.lon

or

lon = arr1['lon']

apart from the already mentioned

lon = arr1.coords['lon']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.