XArray
Functions to augment XArray DataArrays and Datasets with additional functionality.
clone_using(da, np_arr, use_coords=True, use_attrs=False, new_name=None)
Given a NumPy array, return an XArray DataArray
which contains the same
dimension names and (optionally) coordinates and other properties as the
supplied DataArray
.
This is similar to xr.DataArray.copy()
with more specificity for
the type of cloning you would like to perform - the different properties
that you desire to mirror in the new DataArray
.
If the coordinates from the source DataArray
are not desired, the shape
of the source and new NumPy arrays don't need to match.
The number of dimensions do, however.
Examples:
Making a new DataArray
from a previous one, keeping the
dimension names but dropping the coordinates (the input NumPy array
is of a different size):
>>> import xarray as xr
>>> import janitor.xarray
>>> da = xr.DataArray(
... np.zeros((512, 1024)), dims=["ax_1", "ax_2"],
... coords=dict(ax_1=np.linspace(0, 1, 512),
... ax_2=np.logspace(-2, 2, 1024)),
... name="original",
... )
>>> new_da = da.clone_using(
... np.ones((4, 6)), new_name='new_and_improved', use_coords=False,
... )
>>> new_da
<xarray.DataArray 'new_and_improved' (ax_1: 4, ax_2: 6)> Size: 192B
array([[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]])
Dimensions without coordinates: ax_1, ax_2
Parameters:
Name | Type | Description | Default |
---|---|---|---|
da |
DataArray
|
The |
required |
np_arr |
array
|
The NumPy array which will be wrapped in a new |
required |
use_coords |
bool
|
If |
True
|
use_attrs |
bool
|
If |
False
|
new_name |
str
|
If set, use as the new name of the returned |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If number of dimensions in |
ValueError
|
If shape of |
Returns:
Type | Description |
---|---|
DataArray
|
A |
Source code in janitor/xarray/functions.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
convert_datetime_to_number(da_or_ds, time_units, dim='time')
Convert the coordinates of a datetime axis to a human-readable float representation.
Examples:
Convert a DataArray
's time dimension coordinates from
minutes to seconds:
>>> import numpy as np
>>> import xarray as xr
>>> import janitor.xarray
>>> timepoints = 5
>>> da = xr.DataArray(
... np.array([2, 8, 0, 1, 7, 7]),
... dims="time",
... coords=dict(time=np.arange(6) * np.timedelta64(1, "m"))
... )
>>> da_minutes = da.convert_datetime_to_number("s", dim="time")
>>> da_minutes
<xarray.DataArray (time: 6)> Size: 48B
array([2, 8, 0, 1, 7, 7])
Coordinates:
* time (time) float64 48B 0.0 60.0 120.0 180.0 240.0 300.0
Parameters:
Name | Type | Description | Default |
---|---|---|---|
da_or_ds |
Union[DataArray, Dataset]
|
XArray object. |
required |
time_units |
str
|
Numpy timedelta string specification for the unit you would like to convert the coordinates to. |
required |
dim |
str
|
The time dimension whose coordinates are datetime objects. |
'time'
|
Returns:
Type | Description |
---|---|
Union[DataArray, Dataset]
|
The original XArray object with the time dimension reassigned. |
Source code in janitor/xarray/functions.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
|