pydata / pydata/xarray

Extra dimension on first argument passed into apply_ufunc

Open
#2,714 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-documentation
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

Here's my code:

import numpy as np
import xarray as xr

da = xr.DataArray(np.random.rand(1000, 100))
da = da.rename({'dim_0': 'rows_a'})
db = xr.DataArray(np.random.rand(1000, 100))
db = db.rename({'dim_0': 'rows_b'})

def print_shape(a):
    print(a.shape)
    return np.zeros(shape=(a.shape[0]))

def print_two_shapes(a, b):
    print(a.shape)
    print(b.shape)
    return np.zeros(shape=(a.shape[0], b.shape[0]))

If I print_shape and print_shapes with apply_ufunc I am surprised by the results:

xr.apply_ufunc(
    print_shape,
    da,
    input_core_dims=[['dim_1']]
)

(1000, 100)
<xarray.DataArray (rows_a: 1000)>
array([0., 0., 0., ..., 0., 0., 0.])
Coordinates:
  * rows_a   (rows_a) int64 0 1 2 3 4 5 6 7 ... 992 993 994 995 996 997 998 999

vs

xr.apply_ufunc(
    print_two_shapes,
    da, db,
    input_core_dims=[['dim_1'], ['dim_1']]
)

(1000, 1, 100)
(1000, 100)

<xarray.DataArray (rows_a: 1000, rows_b: 1000)>
array([[0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       ...,
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.]])
Coordinates:
  * rows_a   (rows_a) int64 0 1 2 3 4 5 6 7 ... 992 993 994 995 996 997 998 999
  * rows_b   (rows_b) int64 0 1 2 3 4 5 6 7 ... 992 993 994 995 996 997 998 999

My array da has changed shape from (1000, 100) to (1000, 1, 100) when a second argument was added to my call to apply_ufunc.

Maybe this is documented, but I missed it. If it is documented I'd be glad to be pointed to it and I'll see if I can come up with a suggestion of how to highlight this better in the documentation as it really threw me.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the two apply_ufunc calls using the Python, NumPy, and xarray example in the issue, focusing on input_core_dims and the differing argument counts. Read the apply_ufunc documentation and clarify why the first argument receives different shapes; done means the documented behavior explains both printed results and how to avoid the surprise.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data, documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.