Stream: xarray

Topic: apply_ufunc


view this post on Zulip Thomas Martin (Nov 03 2022 at 17:09):

Hi y'all!

Trying to make a few functions that will work for processing data with apply_ufunc. The dataset is the air_temp example dataset.

from sklearn.preprocessing import MinMaxScaler


def minmax(x):
    scaler = MinMaxScaler(
        feature_range=(-1,1))

    min_max_scaled = scaler.fit_transform(x.reshape(-1,1))
    return min_max_scaled

next cell is here:

min_maxd = xr.apply_ufunc(minmax, ds,
        input_core_dims=[['time']],
        vectorize=True)

The output is a valueError:

ValueError: Expected 2D array, got 1D array instead:
array=[241.2     242.09999 242.29999 ... 243.48999 245.79    245.09   ].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

view this post on Zulip Deepak Cherian (Nov 03 2022 at 17:39):

input_core_dims=[['time']],
        vectorize=True

This means,: "provide my function with 1D arrays (only time) by looping over all other dimensions". Sounds like you want the inverse where all dimensions but time are core dimensions, and then vectorize=True will loop over time.


Last updated: May 16 2025 at 17:14 UTC