Skip to content

Commit

Permalink
Add date attribute to datetime accessor (pydata#4994)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
observingClouds authored Mar 16, 2021
1 parent 76d6d75 commit daea5df
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/api-hidden.rst
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@
core.accessor_dt.DatetimeAccessor.floor
core.accessor_dt.DatetimeAccessor.round
core.accessor_dt.DatetimeAccessor.strftime
core.accessor_dt.DatetimeAccessor.date
core.accessor_dt.DatetimeAccessor.day
core.accessor_dt.DatetimeAccessor.dayofweek
core.accessor_dt.DatetimeAccessor.dayofyear
Expand Down
1 change: 1 addition & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ Datetimelike properties
DataArray.dt.daysinmonth
DataArray.dt.season
DataArray.dt.time
DataArray.dt.date
DataArray.dt.is_month_start
DataArray.dt.is_month_end
DataArray.dt.is_quarter_end
Expand Down
3 changes: 2 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ New Features
grant from the `Chan Zuckerberg Initiative <https://chanzuckerberg.com>`_ and
developed by `B-Open <https://www.bopen.eu>`_.
By `Aureliana Barghini <https://github.com/aurghs>`_ and `Alessandro Amici <https://github.com/alexamici>`_.
- :py:attr:`~core.accessor_dt.DatetimeAccessor.date` added (:issue:`4983`, :pull:`4994`).
By `Hauke Schulz <https://github.com/observingClouds>`_.
- Implement ``__getitem__`` for both :py:class:`~core.groupby.DatasetGroupBy` and
:py:class:`~core.groupby.DataArrayGroupBy`, inspired by pandas'
:py:meth:`~pandas.core.groupby.GroupBy.get_group`.
By `Deepak Cherian <https://github.com/dcherian>`_.


Breaking changes
~~~~~~~~~~~~~~~~
- :py:func:`open_dataset` and :py:func:`open_dataarray` now accept only the first argument
Expand Down
8 changes: 8 additions & 0 deletions xarray/core/accessor_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def _access_through_cftimeindex(values, name):
if name == "season":
months = values_as_cftimeindex.month
field_values = _season_from_months(months)
elif name == "date":
raise AttributeError(
"'CFTimeIndex' object has no attribute `date`. Consider using the floor method instead, for instance: `.time.dt.floor('D')`."
)
else:
field_values = getattr(values_as_cftimeindex, name)
return field_values.reshape(values.shape)
Expand Down Expand Up @@ -415,6 +419,10 @@ def weekofyear(self):
"time", "Timestamps corresponding to datetimes", object
)

date = Properties._tslib_field_accessor(
"date", "Date corresponding to datetimes", object
)

is_month_start = Properties._tslib_field_accessor(
"is_month_start",
"Indicates whether the date is the first day of the month.",
Expand Down
14 changes: 14 additions & 0 deletions xarray/tests/test_accessor_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def setup(self):
"weekday",
"dayofyear",
"quarter",
"date",
"time",
"is_month_start",
"is_month_end",
"is_quarter_start",
Expand Down Expand Up @@ -144,6 +146,8 @@ def test_not_datetime_type(self):
"weekday",
"dayofyear",
"quarter",
"date",
"time",
"is_month_start",
"is_month_end",
"is_quarter_start",
Expand Down Expand Up @@ -430,6 +434,16 @@ def test_isocalendar_cftime(data):
data.time.dt.isocalendar()


@requires_cftime
def test_date_cftime(data):

with raises_regex(
AttributeError,
r"'CFTimeIndex' object has no attribute `date`. Consider using the floor method instead, for instance: `.time.dt.floor\('D'\)`.",
):
data.time.dt.date()


@requires_cftime
@pytest.mark.filterwarnings("ignore::RuntimeWarning")
def test_cftime_strftime_access(data):
Expand Down

0 comments on commit daea5df

Please sign in to comment.