-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Initialize empty or full DataArray #3159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 22 commits
4f6311e
1aa3ff3
db2cb28
4cecfe9
a538dab
4c45b7b
b0f2d4e
2fa6e48
c481560
28b7336
59a632f
8c165fd
f337550
9a57dc5
70786c1
3f95767
8c97a46
dec7622
8c5aaf3
347ec33
e3127a3
38858c6
4be0607
9bb3530
68ff54a
7407757
4e95cc3
4df28db
8ea1c47
95c88ab
2c0c634
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,6 +158,30 @@ def _infer_coords_and_dims( | |
return new_coords, dims | ||
|
||
|
||
def _check_data_shape(data, coords, dims): | ||
if data is dtypes.NA: | ||
data = np.nan | ||
if ( | ||
coords is not None | ||
and utils.is_scalar(data, include_0d=False) | ||
and not isinstance(data, indexing.ExplicitlyIndexed) | ||
): | ||
if utils.is_dict_like(coords): | ||
if dims is None: | ||
return data | ||
else: | ||
data_shp = tuple( | ||
griverat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
as_compatible_data(coords[k]).size if k in coords.keys() else 1 | ||
griverat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for k in dims | ||
) | ||
elif any(isinstance(coord, tuple) for coord in coords): | ||
data_shp = tuple(as_compatible_data(coord).size for d, coord in coords) | ||
else: | ||
data_shp = tuple(as_compatible_data(coord).size for coord in coords) | ||
data = np.full(data_shp, data) | ||
return data | ||
|
||
|
||
class _LocIndexer: | ||
def __init__(self, data_array: "DataArray"): | ||
self.data_array = data_array | ||
|
@@ -234,7 +258,7 @@ class DataArray(AbstractArray, DataWithCoords): | |
|
||
def __init__( | ||
self, | ||
data: Any, | ||
data: Any = dtypes.NA, | ||
griverat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
coords: Union[Sequence[Tuple], Mapping[Hashable, Any], None] = None, | ||
dims: Union[Hashable, Sequence[Hashable], None] = None, | ||
name: Hashable = None, | ||
|
@@ -323,6 +347,7 @@ def __init__( | |
if encoding is None: | ||
encoding = getattr(data, "encoding", None) | ||
|
||
data = _check_data_shape(data, coords, dims) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should move this logic above For example:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand correctly, the second example you provided shouldn't work since Regarding the |
||
data = as_compatible_data(data) | ||
coords, dims = _infer_coords_and_dims(data.shape, coords, dims) | ||
variable = Variable(dims, data, attrs, encoding, fastpath=True) | ||
|
Uh oh!
There was an error while loading. Please reload this page.