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

Provide a neat way to get date strings from arrays with date units. #4508

Open
pp-mo opened this issue Jan 17, 2022 · 4 comments
Open

Provide a neat way to get date strings from arrays with date units. #4508

pp-mo opened this issue Jan 17, 2022 · 4 comments

Comments

@pp-mo
Copy link
Member

pp-mo commented Jan 17, 2022

✨ Feature Request

Following [this comment] #4499 (comment) (and preceding ones).

If you know what you're doing this isn't actually that hard.
E.G. coord.units.num2date(coord.bounds).astype('str')
But it isn't so easy to work that out.

How about Coord.date_strings() ?
We can make it cheekily default to the Coord's own points, like Connectivity.indices_by_src

@pp-mo pp-mo changed the title Provide a neat way to get date strings from data arrays with date units. Provide a neat way to get date strings from arrays with date units. Jan 18, 2022
@rcomer
Copy link
Member

rcomer commented Dec 13, 2022

I wanted this again today. I think the implementation is easy, and the hard bit is deciding what the interface should be. So marking this for discussion.

@vsherratt
Copy link
Contributor

vsherratt commented Dec 14, 2022

One option which seemed favourable in peloton discussions: adding a __format__ method to Coord, such that format(tcoord, "%Y-%m-%d") could be used to create a string with all points formatted with the given format string. This is also used by f-strings so f"{tcoord:%Y-%m-%d}" would be equivalent.

Actual implementation might look something like

def __format__(self, fmt):
    return str([format(point, fmt) for point in self.units.num2date(self.points)])

Just needs a bit more thought into points vs bounds (always both? extra symbol in the fmt string to choose one or the other?)

@larsbarring
Copy link
Contributor

As one immediately upvoting this feature I am really looking forward to this, and I would very much favour the possibility to choose only points, only bounds, or both. Many thanks /L

@vsherratt vsherratt removed their assignment Mar 14, 2023
@rcomer
Copy link
Member

rcomer commented Jan 17, 2024

I wanted this again today so I rolled my own

def print_coord_times(coord, points=True, bounds=True):
    """
    Given a time type coordinate (e.g. 'time' or 'forecast_reference_time'),
    print string representations of the points/bounds.

    Set *points* or *bounds* to False to skip printing those.
    """
    if not coord.units.is_time_reference():
        raise ValueError(f"{coord.name()} is not a time type coordinate")

    if points:
        print("Points:")
        print(coord.units.num2date(coord.points).astype('str'))

    if bounds:
        print("Bounds:")
        print(coord.units.num2date(coord.bounds).astype('str'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

5 participants