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 support for group-by financial years. #1257

Merged
merged 5 commits into from May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions datacube/virtual/transformations.py
Expand Up @@ -6,6 +6,7 @@

import numpy
import xarray
import pandas as pd

from datacube.utils.masking import make_mask as make_mask_prim
from datacube.utils.masking import mask_invalid_data as mask_invalid_data_prim
Expand Down Expand Up @@ -356,6 +357,16 @@ def result(output_var, output_desc):
def year(time):
return time.astype('datetime64[Y]')

def fiscal_year(time):
""""
This function will support group-by financial years
"""
df = pd.Series(time.values)
years = df.apply(lambda x: np.datetime64(str(x.to_period('Q-JUN').qyear))).values
ds = xr.DataArray(years, name='time', attrs=time.attrs, coords=time.coords, dims=time.dims)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me too.

What happens if instead of the two lines defining and modifying ds you try to do it in one go?

ds = xr.DataArray(years, name='time', attrs=time.attrs, coords=years, dims=time.dims)

I think that should work!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The coordinates should be a dictionary so something like below will work but I'm not 100% sure if the coords can contain other variables than time which the following code will exclude:

ds = xr.DataArray(years, name='time', attrs=time.attrs, coords=({"time": years}), dims=time.dims)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about writing a test - that way we know for sure if it works the way we expect. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the existing time grouping transforms aren't covered by tests either, but they are trivial compared to this one.

ds = ds.assign_coords({"time": years})
return ds


def month(time):
return time.astype('datetime64[M]')
Expand Down
2 changes: 1 addition & 1 deletion docs/about/whats_new.rst
Expand Up @@ -7,7 +7,7 @@ What's New

v1.8.next
=========

- Added support for group-by financial years to virtual products. (:pull:`1257`)
- Remove reference to `rasterio.path`. (:pull:`1255`)
- Cleaner separation of postgis and postgres drivers, and suppress SQLAlchemy cache warnings. (:pull:`1254`)
- Prevent Shapely deprecation warning. (:pull:`1253`)
Expand Down