Skip to content

Commit 9ee0f01

Browse files
Expose use_cftime option in open_zarr #2886 (#3229)
* Expose use_cftime option in open_zarr #2886 * Add test for open_zarr w/ use_cftime * Formatting only * Add entry in `whats-new.rst` * Remove space Co-authored-by: Anderson Banihirwe <[email protected]>
1 parent 2acd0fc commit 9ee0f01

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

doc/whats-new.rst

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ New Features
6262
now accept a ``dim_order`` parameter allowing to specify the resulting dataframe's
6363
dimensions order (:issue:`4331`, :pull:`4333`).
6464
By `Thomas Zilio <https://github.com/thomas-z>`_.
65+
- Expose ``use_cftime`` option in :py:func:`~xarray.open_zarr` (:issue:`2886`, :pull:`3229`)
66+
By `Samnan Rahee <https://github.com/Geektrovert>`_ and `Anderson Banihirwe <https://github.com/andersy005>`_.
6567

6668

6769
Bug fixes

xarray/backends/zarr.py

+12
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ def open_zarr(
510510
overwrite_encoded_chunks=False,
511511
chunk_store=None,
512512
decode_timedelta=None,
513+
use_cftime=None,
513514
**kwargs,
514515
):
515516
"""Load and decode a dataset from a Zarr store.
@@ -576,6 +577,16 @@ def open_zarr(
576577
{'days', 'hours', 'minutes', 'seconds', 'milliseconds', 'microseconds'}
577578
into timedelta objects. If False, leave them encoded as numbers.
578579
If None (default), assume the same value of decode_time.
580+
use_cftime: bool, optional
581+
Only relevant if encoded dates come from a standard calendar
582+
(e.g. "gregorian", "proleptic_gregorian", "standard", or not
583+
specified). If None (default), attempt to decode times to
584+
``np.datetime64[ns]`` objects; if this is not possible, decode times to
585+
``cftime.datetime`` objects. If True, always decode times to
586+
``cftime.datetime`` objects, regardless of whether or not they can be
587+
represented using ``np.datetime64[ns]`` objects. If False, always
588+
decode times to ``np.datetime64[ns]`` objects; if this is not possible
589+
raise an error.
579590
580591
Returns
581592
-------
@@ -637,6 +648,7 @@ def maybe_decode_store(store, lock=False):
637648
decode_coords=decode_coords,
638649
drop_variables=drop_variables,
639650
decode_timedelta=decode_timedelta,
651+
use_cftime=use_cftime,
640652
)
641653

642654
# TODO: this is where we would apply caching

xarray/tests/test_backends.py

+10
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,16 @@ def test_chunk_encoding_with_partial_dask_chunks(self):
20252025
) as ds1:
20262026
assert_equal(ds1, original)
20272027

2028+
@requires_cftime
2029+
def test_open_zarr_use_cftime(self):
2030+
ds = create_test_data()
2031+
with self.create_zarr_target() as store_target:
2032+
ds.to_zarr(store_target, consolidated=True)
2033+
ds_a = xr.open_zarr(store_target, consolidated=True)
2034+
assert_identical(ds, ds_a)
2035+
ds_b = xr.open_zarr(store_target, consolidated=True, use_cftime=True)
2036+
assert xr.coding.times.contains_cftime_datetimes(ds_b.time)
2037+
20282038

20292039
@requires_zarr
20302040
class TestZarrDictStore(ZarrBase):

0 commit comments

Comments
 (0)