Skip to content

Commit be1f4a0

Browse files
authored
Time accessor (#64)
* added _get_axis_coord_alt for groupby(T.month) * rename _alt to _time_accessor * Update cf_xarray/tests/test_accessor.py * ran black * flake8 * Actually use mapper :) * add _get_axis_coord_time_accessor to group * rm "groupby_time_accessor" from test line 156 * Add whats-new * Update example notebook * fix whats-new entry
1 parent 698408a commit be1f4a0

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-12
lines changed

cf_xarray/accessor.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,36 @@ def _get_axis_coord_single(var: Union[DataArray, Dataset], key: str,) -> List[st
182182
return results
183183

184184

185+
def _get_axis_coord_time_accessor(var, key):
186+
"""
187+
Helper method for when our key name is of the nature "T.month" and we want to
188+
isolate the "T" for coordinate mapping
189+
190+
Parameters
191+
----------
192+
var: DataArray, Dataset
193+
DataArray belonging to the coordinate to be checked
194+
key: str, ["X", "Y", "Z", "T", "longitude", "latitude", "vertical", "time"]
195+
key to check for.
196+
197+
Returns
198+
-------
199+
List[str], Variable name(s) in parent xarray object that matches axis or coordinate `key` appended by the frequency extension (e.g. ".month")
200+
201+
Notes
202+
-----
203+
Returns an empty list if there is no frequency extension specified.
204+
"""
205+
if "." in key:
206+
key, ext = key.split(".", 1)
207+
208+
results = _get_axis_coord_single(var, key)
209+
return [v + "." + ext for v in results]
210+
211+
else:
212+
return []
213+
214+
185215
def _get_axis_coord(var: Union[DataArray, Dataset], key: str,) -> List[str]:
186216
"""
187217
Translate from axis or coord name to variable name
@@ -308,7 +338,7 @@ def _get_measure(da: Union[DataArray, Dataset], key: str) -> List[str]:
308338
"dims_or_levels": (_get_axis_coord,), # reset_index
309339
"window": (_get_axis_coord,), # rolling_exp
310340
"coord": (_get_axis_coord_single,), # differentiate, integrate
311-
"group": (_get_axis_coord_single,),
341+
"group": (_get_axis_coord_single, _get_axis_coord_time_accessor),
312342
"indexer": (_get_axis_coord_single,), # resample
313343
"variables": (_get_axis_coord,), # sortby
314344
"weights": (_get_measure_variable,), # type: ignore

cf_xarray/tests/test_accessor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ def test_rename_like():
126126
("resample", {"time": "M"}, {"T": "M"}),
127127
("rolling", {"lat": 5}, {"Y": 5}),
128128
("groupby", {"group": "time"}, {"group": "T"}),
129+
("groupby", {"group": "time.month"}, {"group": "T.month"}),
129130
("groupby_bins", {"group": "lat", "bins": 5}, {"group": "latitude", "bins": 5}),
130131
pytest.param(
131132
"coarsen",

doc/examples/introduction.ipynb

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"execution_count": null,
1515
"metadata": {
1616
"ExecuteTime": {
17-
"end_time": "2020-07-24T20:12:00.609657Z",
18-
"start_time": "2020-07-24T20:11:59.698753Z"
17+
"end_time": "2020-07-27T23:20:40.515653Z",
18+
"start_time": "2020-07-27T23:20:39.578412Z"
1919
}
2020
},
2121
"outputs": [],
@@ -37,8 +37,8 @@
3737
"execution_count": null,
3838
"metadata": {
3939
"ExecuteTime": {
40-
"end_time": "2020-07-24T20:12:00.805176Z",
41-
"start_time": "2020-07-24T20:12:00.718615Z"
40+
"end_time": "2020-07-27T23:20:41.803595Z",
41+
"start_time": "2020-07-27T23:20:41.713269Z"
4242
}
4343
},
4444
"outputs": [],
@@ -718,18 +718,25 @@
718718
"ds.cf.resample(T=\"D\").mean()"
719719
]
720720
},
721+
{
722+
"cell_type": "markdown",
723+
"metadata": {},
724+
"source": [
725+
"`cf_xarray` also understands the \"datetime accessor\" syntax for groupby\n"
726+
]
727+
},
721728
{
722729
"cell_type": "code",
723730
"execution_count": null,
724731
"metadata": {
725732
"ExecuteTime": {
726-
"end_time": "2020-06-30T23:25:33.106698Z",
727-
"start_time": "2020-06-30T23:25:33.037746Z"
733+
"end_time": "2020-07-27T23:21:24.092199Z",
734+
"start_time": "2020-07-27T23:21:24.025943Z"
728735
}
729736
},
730737
"outputs": [],
731738
"source": [
732-
"ds.cf.groupby(\"time.month\").mean(\"longitude\")"
739+
"ds.cf.groupby(\"T.month\").mean(\"longitude\")"
733740
]
734741
},
735742
{
@@ -780,13 +787,13 @@
780787
"execution_count": null,
781788
"metadata": {
782789
"ExecuteTime": {
783-
"end_time": "2020-06-30T23:25:33.212324Z",
784-
"start_time": "2020-06-30T23:25:33.164373Z"
790+
"end_time": "2020-07-27T23:20:59.326974Z",
791+
"start_time": "2020-07-27T23:20:59.261728Z"
785792
}
786793
},
787794
"outputs": [],
788795
"source": [
789-
"ds.cf.groupby(\"time.month\").mean([\"lat\", \"X\"])"
796+
"ds.cf.groupby(\"T.month\").mean([\"lat\", \"X\"])"
790797
]
791798
},
792799
{
@@ -853,7 +860,7 @@
853860
"name": "python",
854861
"nbconvert_exporter": "python",
855862
"pygments_lexer": "ipython3",
856-
"version": "3.7.6"
863+
"version": "3.7.8"
857864
},
858865
"toc": {
859866
"base_numbering": 1,

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ What's New
44
v0.1.6 (unreleased)
55
===================
66

7+
- Remap datetime accessor syntax for groupby. E.g. ``.cf.groupby("T.month")`` → ``.cf.groupby("ocean_time.month")``.
8+
(:pr:`64`, :issue:`6`). `Julia Kent`_.
79
- Added ``.cf.rename_like`` to rename matching variables. Only coordinate variables
810
i.e. those that match the criteria for ``("latitude", "longitude", "vertical", "time")``
911
are renamed for now. (:pr:`55`) `Deepak Cherian`_.
@@ -32,3 +34,4 @@ v0.1.3
3234
- Support expanding key to multiple dimension names.
3335

3436
.. _`Deepak Cherian`: https://github.com/dcherian
37+
.. _`Julia Kent`: https://github.com/jukent

0 commit comments

Comments
 (0)