简体   繁体   English

xarray-返回满足某些条件的数据,而不必使用for循环

[英]xarray - Return data that meets certain criteria without having to use for loop

I am working with multi-dimensional data da_criteria_1or0_hourly . 我正在使用多维数据da_criteria_1or0_hourly

The data (variable) is either 1 or 0. 数据(变量)为1或0。

print(da_criteria_1or0_hourly)

Output: 输出:

<xarray.DataArray (time: 8760, latitude: 106, longitude: 193)>
dask.array<shape=(8760, 106, 193), dtype=int32, chunksize=(744, 106, 193)>
Coordinates:
  * latitude   (latitude) float32 -39.2 -39.149525 ... -33.950478 -33.9
  * longitude  (longitude) float32 140.8 140.84792 140.89584 ... 149.95209 150.0
  * time       (time) datetime64[ns] 2017-01-01 ... 2017-12-31T23:00:00

The data is as massive as 179212080. 数据量高达179212080。

I am unsure which method on xarray I should use to get a new xarray object that only return data which is 1 ( 0 can be assigned NaN or dropped). 我不确定应该使用哪种方法在xarray上获取新的xarray对象,该对象仅返回数据为1 (可以为NaN分配0或将其丢弃)。

I was trying to print out data for each coordinate using sel using FOR LOOP, but this was extremely slow. 我试图使用FOR LOOP使用sel打印每个坐标的数据,但这非常慢。 It could take forever. 这可能会永远。

for time_elem in da_criteria_1or0_hourly.coords['time'].values:
    for lat_elem in da_criteria_1or0_hourly.coords['latitude'].values:
        for lon_elem in da_criteria_1or0_hourly.coords['longitude'].values:
            val = da_criteria_1or0_hourly.sel(time=time_elem,latitude=lat_elem,longitude=lon_elem).values
            if (val == 1):
                print(time_elem, lat_elem, lon_elem, val)

Is there any better way to do it? 有什么更好的方法吗?

If I understood your problem correctly xarray.Dataset.where should be the way to go: 如果我正确理解了您的问题, xarray.Dataset.where应该使用xarray.Dataset.where

da_criteria_1or0_hourly.where(da_criteria_1or0_hourly == 1)

This will return a new xarray where every entry that was not 1 is now NaN. 这将返回一个新的xarray,其中每个不为1的条目现在都是NaN。

Time comparison for a 50x50x50 xarray: 50x50x50 xarray的时间比较:

for loops: ~56.9456s for循环:〜56.9456s

where : ~00.0020s where :〜00.0020s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用plt.text仅绘制符合特定条件的数据 - Using plt.text to only plot data that meets a certain criteria Python/Pandas:当一列数据满足一定条件时如何处理 - Python/Pandas:How to process a column of data when it meets certain criteria Pandas:如何循环遍历长字符串的每个字符,如果字符满足特定类型和数值条件则执行加法 - Pandas: How to loop through each character of a long string, perform addition if the character meets certain type and numerical criteria 编写代码以检查密码是否符合特定条件 - Writing code to check if a password meets certain criteria 在 Python 中验证数字序列是否满足某些条件 - Validate That A Sequence of Numbers Meets Certain Criteria in Python 如果满足特定条件,是否可以将值添加到列表中? - Is it possible to add a value into a list if it meets certain criteria? 检查 pandas 列中的语法是否符合某些条件 - check if syntax in pandas column meets certain criteria 在对象满足特定条件后发送电子邮件 - Send an email once object meets certain criteria 仅当内容符合特定条件时才将表单保存到数据库 - How to save form to database only if content meets certain criteria Python - 构建子列表,满足来自大量组合的特定条件 - Python - building sublist that meets certain criteria from huge number of combinations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM