Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add date attribute to datetime accessor #4994

Merged
merged 10 commits into from
Mar 16, 2021
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