Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion doc/user-guide/io.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ ds = xr.Dataset(
},
)

ds.to_netcdf(nc_filename)
ds.to_netcdf(nc_filename, unlimited_dims=["y"])
```

By default, the file is saved as netCDF4 (assuming netCDF4-Python is
Expand Down Expand Up @@ -510,6 +510,19 @@ ds_disk["y"].encoding
ds_disk.encoding
```

One key piece of information you can find in the encoding is whether a
dimension is unlimited. Unlimited dimensions can grow in size, and are
commonly used for a dimension like `time` that is appended to over
time:

```{code-cell}
ds_disk.encoding["unlimited_dims"]
```

This information can also be set explicitly when writing a file with
{py:meth}`Dataset.to_netcdf` using the `unlimited_dims` keyword
argument.

Note that all operations that manipulate variables other than indexing
will remove encoding information.

Expand Down
13 changes: 13 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ Deprecations
Bug Fixes
~~~~~~~~~

- Fixed ``DataArray.str.replace`` replacing every occurrence instead of none when
``n=0``. ``re.sub`` treats ``count=0`` as "replace all", so the regex code path
collapsed ``n=0`` onto ``n=-1``, while the ``regex=False`` path already handled
``n=0`` correctly (:pull:`11545`).
By `Alexander Kropiunig <https://github.com/Kropiunig>`_.
- ``min`` and ``max`` of object arrays with ``skipna=False`` now return ``NaN``
for slices containing missing values, instead of a result that depended on
the position of the missing value (:issue:`11501`, :pull:`#11627`).
Expand Down Expand Up @@ -172,6 +177,11 @@ Documentation
- Migrated from nbsphinx/jupyter-execute to myst-nb (:issue:`7924`, :pull:`11456`).
By `Nick Hodgskin <https://github.com/VeckoTheGecko>`_.

- Added an example to the netCDF section of the IO user guide showing how to
check which dimensions are unlimited via ``Dataset.encoding``
(:issue:`7517`, :pull:`11618`).
By `Anirban Mandal <https://github.com/CoderAnirban71>`_.


Internal Changes
~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -428,6 +438,9 @@ Bug Fixes
By `Emmanuel Ferdman <https://github.com/emmanuel-ferdman>`_.
- :func:`combine_by_coords` no longer returns an empty dataset when a generator is passed as ``data_objects`` (:issue:`10114`, :pull:`11265`).
By `Amartya Anand <https://github.com/SurfyPenguin>`_.
- Warn when tuple-style ``DataArray`` coordinates are renamed by explicitly
provided dimension names (:issue:`11234`, :pull:`11292`).
By `Asish Kumar <https://github.com/officialasishkumar>`_.
- Preserve non-grouped coordinates in fallback ``groupby`` reductions when grouping
by a non-leading dimension reorders the underlying variable dimensions
(:issue:`11188`, :pull:`11290`).
Expand Down
6 changes: 4 additions & 2 deletions xarray/core/accessor_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -1944,8 +1944,10 @@ def replace(

if regex:
pat = self._re_compile(pat=pat, flags=flags, case=case)
func = lambda x, ipat, irepl, i_n: ipat.sub(
repl=irepl, string=x, count=max(i_n, 0)
# ``re.sub`` interprets ``count=0`` as "replace every occurrence",
# so ``n=0`` has to be special-cased to mean "replace nothing".
func = lambda x, ipat, irepl, i_n: (
x if i_n == 0 else ipat.sub(repl=irepl, string=x, count=max(i_n, 0))
)
else:
pat = self._stringify(pat)
Expand Down
15 changes: 15 additions & 0 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ def _infer_coords_and_dims(
if not hashable(d):
raise TypeError(f"Dimension {d} is not hashable")

if coords is not None and not utils.is_dict_like(coords):
if any(
isinstance(coord, tuple)
and len(coord) >= 2
and hashable(coord[0])
and coord[0] != dim
for dim, coord in zip(dims_tuple, coords, strict=True)
):
utils.emit_user_level_warning(
"Coordinate names in tuple-style coords are ignored when `dims` "
"are provided. Use a mapping for `coords` if you need named "
"coordinates.",
UserWarning,
)

new_coords: Mapping[Hashable, Any]

if isinstance(coords, Coordinates):
Expand Down
22 changes: 22 additions & 0 deletions xarray/tests/test_accessor_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,28 @@ def test_replace(dtype) -> None:
assert_equal(result, expected)


def test_replace_n_zero(dtype) -> None:
# ``n=0`` means "make no replacements", for regex and literal patterns alike
values = xr.DataArray(["fooBAD__barBAD"], dims=["x"]).astype(dtype)

result = values.str.replace("BAD[_]*", "", n=0)
assert result.dtype == values.dtype
assert_equal(result, values)

result = values.str.replace("BAD", "", n=0, regex=False)
assert result.dtype == values.dtype
assert_equal(result, values)

# ``n`` is broadcast, so a single zero must not spill over to its neighbours
n = xr.DataArray([0, 1, -1], dims=["y"])
result = values.str.replace("BAD[_]*", "", n=n)
expected = xr.DataArray(
[["fooBAD__barBAD", "foobarBAD", "foobar"]], dims=["x", "y"]
).astype(dtype)
assert result.dtype == expected.dtype
assert_equal(result, expected)


def test_replace_callable() -> None:
values = xr.DataArray(["fooBAD__barBAD"])

Expand Down
32 changes: 32 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,38 @@ def test_constructor(self) -> None:
expected = DataArray([1, 2, 3], coords=[("x", [0, 1, 2])])
assert_identical(expected, actual)

def test_constructor_tuple_coords_warn_when_dims_override_names(self) -> None:
data = np.random.random((2, 3))
coords = [("a", [0, 1]), ("b", [-1, -2, -3])]

with pytest.warns(
UserWarning,
match="Coordinate names in tuple-style coords are ignored",
):
actual = DataArray(data, coords=coords, dims=["x", "y"])

expected = Dataset(
{None: (["x", "y"], data)},
coords={"x": [0, 1], "y": [-1, -2, -3]},
)[None]
assert_identical(expected, actual)

def test_constructor_tuple_coords_no_warning_when_names_match_dims(self) -> None:
data = np.random.random((2, 3))

with assert_no_warnings():
actual = DataArray(
data,
coords=[("x", [0, 1]), ("y", [-1, -2, -3])],
dims=["x", "y"],
)

expected = Dataset(
{None: (["x", "y"], data)},
coords={"x": [0, 1], "y": [-1, -2, -3]},
)[None]
assert_identical(expected, actual)

def test_constructor_invalid(self) -> None:
data = np.random.randn(3, 2)

Expand Down
Loading