Skip to content

Commit daea5df

Browse files
Add date attribute to datetime accessor (#4994)
* add date accessor to accessor_dt.py (GH4983) * raise error when using date with CFTimeIndex * Add tests * Mention changes in whats-new.rst * Add DatetimeAccessor.date to api-hidden.rst * Add attribute to api.rst * Change AttributeError message
1 parent 76d6d75 commit daea5df

File tree

5 files changed

+26
-1
lines changed

5 files changed

+26
-1
lines changed

doc/api-hidden.rst

+1
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@
287287
core.accessor_dt.DatetimeAccessor.floor
288288
core.accessor_dt.DatetimeAccessor.round
289289
core.accessor_dt.DatetimeAccessor.strftime
290+
core.accessor_dt.DatetimeAccessor.date
290291
core.accessor_dt.DatetimeAccessor.day
291292
core.accessor_dt.DatetimeAccessor.dayofweek
292293
core.accessor_dt.DatetimeAccessor.dayofyear

doc/api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ Datetimelike properties
507507
DataArray.dt.daysinmonth
508508
DataArray.dt.season
509509
DataArray.dt.time
510+
DataArray.dt.date
510511
DataArray.dt.is_month_start
511512
DataArray.dt.is_month_end
512513
DataArray.dt.is_quarter_end

doc/whats-new.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ New Features
5151
grant from the `Chan Zuckerberg Initiative <https://chanzuckerberg.com>`_ and
5252
developed by `B-Open <https://www.bopen.eu>`_.
5353
By `Aureliana Barghini <https://github.com/aurghs>`_ and `Alessandro Amici <https://github.com/alexamici>`_.
54+
- :py:attr:`~core.accessor_dt.DatetimeAccessor.date` added (:issue:`4983`, :pull:`4994`).
55+
By `Hauke Schulz <https://github.com/observingClouds>`_.
5456
- Implement ``__getitem__`` for both :py:class:`~core.groupby.DatasetGroupBy` and
5557
:py:class:`~core.groupby.DataArrayGroupBy`, inspired by pandas'
5658
:py:meth:`~pandas.core.groupby.GroupBy.get_group`.
5759
By `Deepak Cherian <https://github.com/dcherian>`_.
5860

59-
6061
Breaking changes
6162
~~~~~~~~~~~~~~~~
6263
- :py:func:`open_dataset` and :py:func:`open_dataarray` now accept only the first argument

xarray/core/accessor_dt.py

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def _access_through_cftimeindex(values, name):
3131
if name == "season":
3232
months = values_as_cftimeindex.month
3333
field_values = _season_from_months(months)
34+
elif name == "date":
35+
raise AttributeError(
36+
"'CFTimeIndex' object has no attribute `date`. Consider using the floor method instead, for instance: `.time.dt.floor('D')`."
37+
)
3438
else:
3539
field_values = getattr(values_as_cftimeindex, name)
3640
return field_values.reshape(values.shape)
@@ -415,6 +419,10 @@ def weekofyear(self):
415419
"time", "Timestamps corresponding to datetimes", object
416420
)
417421

422+
date = Properties._tslib_field_accessor(
423+
"date", "Date corresponding to datetimes", object
424+
)
425+
418426
is_month_start = Properties._tslib_field_accessor(
419427
"is_month_start",
420428
"Indicates whether the date is the first day of the month.",

xarray/tests/test_accessor_dt.py

+14
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def setup(self):
5959
"weekday",
6060
"dayofyear",
6161
"quarter",
62+
"date",
63+
"time",
6264
"is_month_start",
6365
"is_month_end",
6466
"is_quarter_start",
@@ -144,6 +146,8 @@ def test_not_datetime_type(self):
144146
"weekday",
145147
"dayofyear",
146148
"quarter",
149+
"date",
150+
"time",
147151
"is_month_start",
148152
"is_month_end",
149153
"is_quarter_start",
@@ -430,6 +434,16 @@ def test_isocalendar_cftime(data):
430434
data.time.dt.isocalendar()
431435

432436

437+
@requires_cftime
438+
def test_date_cftime(data):
439+
440+
with raises_regex(
441+
AttributeError,
442+
r"'CFTimeIndex' object has no attribute `date`. Consider using the floor method instead, for instance: `.time.dt.floor\('D'\)`.",
443+
):
444+
data.time.dt.date()
445+
446+
433447
@requires_cftime
434448
@pytest.mark.filterwarnings("ignore::RuntimeWarning")
435449
def test_cftime_strftime_access(data):

0 commit comments

Comments
 (0)