From 6470695844d3baf9f8607152fffa89419725a5a9 Mon Sep 17 00:00:00 2001 From: Hauke Schulz <43613877+observingClouds@users.noreply.github.com> Date: Thu, 4 Mar 2021 11:32:12 +0100 Subject: [PATCH 1/7] add date accessor to accessor_dt.py (GH4983) --- xarray/core/accessor_dt.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/xarray/core/accessor_dt.py b/xarray/core/accessor_dt.py index 561d5d30a79..eee2711ad0b 100644 --- a/xarray/core/accessor_dt.py +++ b/xarray/core/accessor_dt.py @@ -415,6 +415,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.", From ffbc5f02a59aa412b88164bbf82418afa5dca3b4 Mon Sep 17 00:00:00 2001 From: Hauke Schulz <43613877+observingClouds@users.noreply.github.com> Date: Thu, 4 Mar 2021 16:41:31 +0100 Subject: [PATCH 2/7] raise error when using date with CFTimeIndex --- xarray/core/accessor_dt.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/xarray/core/accessor_dt.py b/xarray/core/accessor_dt.py index eee2711ad0b..f6e888c97ef 100644 --- a/xarray/core/accessor_dt.py +++ b/xarray/core/accessor_dt.py @@ -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( + "Attribute `date` is not available for CFTimeIndex. Consider using `floor('D')` instead." + ) else: field_values = getattr(values_as_cftimeindex, name) return field_values.reshape(values.shape) From b107288973362c0a843e1b36789927349fb91866 Mon Sep 17 00:00:00 2001 From: Hauke Schulz <43613877+observingClouds@users.noreply.github.com> Date: Mon, 15 Mar 2021 02:22:25 +0100 Subject: [PATCH 3/7] Add tests --- xarray/core/accessor_dt.py | 2 +- xarray/tests/test_accessor_dt.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/xarray/core/accessor_dt.py b/xarray/core/accessor_dt.py index f6e888c97ef..079faa86001 100644 --- a/xarray/core/accessor_dt.py +++ b/xarray/core/accessor_dt.py @@ -33,7 +33,7 @@ def _access_through_cftimeindex(values, name): field_values = _season_from_months(months) elif name == "date": raise AttributeError( - "Attribute `date` is not available for CFTimeIndex. Consider using `floor('D')` instead." + "'CFTimeIndex' object has no attribute `date`. Consider using `floor('D')` instead." ) else: field_values = getattr(values_as_cftimeindex, name) diff --git a/xarray/tests/test_accessor_dt.py b/xarray/tests/test_accessor_dt.py index 984bfc763bc..18519b16d74 100644 --- a/xarray/tests/test_accessor_dt.py +++ b/xarray/tests/test_accessor_dt.py @@ -59,6 +59,8 @@ def setup(self): "weekday", "dayofyear", "quarter", + "date", + "time", "is_month_start", "is_month_end", "is_quarter_start", @@ -144,6 +146,8 @@ def test_not_datetime_type(self): "weekday", "dayofyear", "quarter", + "date", + "time", "is_month_start", "is_month_end", "is_quarter_start", @@ -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 `floor\('D'\)` instead.", + ): + data.time.dt.date() + + @requires_cftime @pytest.mark.filterwarnings("ignore::RuntimeWarning") def test_cftime_strftime_access(data): From 46b1f0dff4607acdc6c80350c65318415559fc53 Mon Sep 17 00:00:00 2001 From: Hauke Schulz <43613877+observingClouds@users.noreply.github.com> Date: Mon, 15 Mar 2021 03:22:10 +0100 Subject: [PATCH 4/7] Mention changes in whats-new.rst --- doc/whats-new.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 7c3d57f4fe8..926b0242790 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -22,7 +22,8 @@ v0.17.1 (unreleased) New Features ~~~~~~~~~~~~ - +- :py:attr:`~core.accessor_dt.DatetimeAccessor.date` added (:issue:`4983`, :pull:`4994`). + By `Hauke Schulz `_. Breaking changes ~~~~~~~~~~~~~~~~ From 71a4221f90d65280349781a8a88c3a3968a6acb9 Mon Sep 17 00:00:00 2001 From: Hauke Schulz <43613877+observingClouds@users.noreply.github.com> Date: Mon, 15 Mar 2021 13:56:50 +0100 Subject: [PATCH 5/7] Add DatetimeAccessor.date to api-hidden.rst --- doc/api-hidden.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/api-hidden.rst b/doc/api-hidden.rst index 14d79039a3a..8f583216723 100644 --- a/doc/api-hidden.rst +++ b/doc/api-hidden.rst @@ -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 From fc7d202ed2785807f8c931a5ae9d9ebd0f8c6425 Mon Sep 17 00:00:00 2001 From: Hauke Schulz <43613877+observingClouds@users.noreply.github.com> Date: Mon, 15 Mar 2021 15:06:43 +0100 Subject: [PATCH 6/7] Add attribute to api.rst --- doc/api.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/api.rst b/doc/api.rst index 9add7a96109..8b85c9ffea5 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -487,6 +487,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 From b3dbb343be870404031707df4c4e52249196cd4e Mon Sep 17 00:00:00 2001 From: Hauke Schulz <43613877+observingClouds@users.noreply.github.com> Date: Mon, 15 Mar 2021 23:07:32 +0100 Subject: [PATCH 7/7] Change AttributeError message --- xarray/core/accessor_dt.py | 2 +- xarray/tests/test_accessor_dt.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/core/accessor_dt.py b/xarray/core/accessor_dt.py index 079faa86001..1d4ef755fa0 100644 --- a/xarray/core/accessor_dt.py +++ b/xarray/core/accessor_dt.py @@ -33,7 +33,7 @@ def _access_through_cftimeindex(values, name): field_values = _season_from_months(months) elif name == "date": raise AttributeError( - "'CFTimeIndex' object has no attribute `date`. Consider using `floor('D')` instead." + "'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) diff --git a/xarray/tests/test_accessor_dt.py b/xarray/tests/test_accessor_dt.py index 18519b16d74..adfa2bed33b 100644 --- a/xarray/tests/test_accessor_dt.py +++ b/xarray/tests/test_accessor_dt.py @@ -439,7 +439,7 @@ def test_date_cftime(data): with raises_regex( AttributeError, - r"'CFTimeIndex' object has no attribute `date`. Consider using `floor\('D'\)` instead.", + r"'CFTimeIndex' object has no attribute `date`. Consider using the floor method instead, for instance: `.time.dt.floor\('D'\)`.", ): data.time.dt.date()