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

Make the date parser more flexible #238

Open
malmans2 opened this issue Oct 17, 2023 · 0 comments
Open

Make the date parser more flexible #238

malmans2 opened this issue Oct 17, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@malmans2
Copy link
Contributor

malmans2 commented Oct 17, 2023

Is your feature request related to a problem? Please describe.

It would be nice to make the date parser more flexible. For example, it would be useful to submit requests of monthly data.
The snippet below shows a possible implementation that uses the strings /to/ and /by/ as MARS.

cc: @EddyCMWF as I've noticed you just opened #237

Describe the solution you'd like

import pandas as pd
import pytest


def date_parser(fmt="%Y-%m-%d", **request):
    date = request.pop("date", None)
    if date is None:
        return request

    if isinstance(date, str):
        date = date.lower()
        for string in (" ", "to", "by"):
            date = date.replace(string, "")
        if "//" not in date:
            dates = pd.to_datetime(date.split("/"))
        else:
            date_split = iter(date.split("//"))
            start = next(date_split)
            end = next(date_split)
            freq = next(date_split, "1D")
            if freq.isdigit():
                freq += "D"
            dates = pd.date_range(start=start, end=end, freq=freq, inclusive="both")
    else:
        dates = pd.to_datetime(date)
    return request | {"date": dates.strftime(fmt).tolist()}


@pytest.mark.parametrize(
    "date,expected",
    (
        (
            "2000/to/2000-12/by/1MS",
            {
                "date": [
                    "2000-01-01",
                    "2000-02-01",
                    "2000-03-01",
                    "2000-04-01",
                    "2000-05-01",
                    "2000-06-01",
                    "2000-07-01",
                    "2000-08-01",
                    "2000-09-01",
                    "2000-10-01",
                    "2000-11-01",
                    "2000-12-01",
                ]
            },
        ),
        (
            ["2000-01", "2000-02"],
            {"date": ["2000-01-01", "2000-02-01"]},
        ),
        # Old features
        (
            "2000-01-01",
            {"date": ["2000-01-01"]},
        ),
        (
            "2000-01-01/2000-01-02",
            {"date": ["2000-01-01", "2000-01-02"]},
        ),
        (
            "2000-01-01/to/2000-01-02",
            {"date": ["2000-01-01", "2000-01-02"]},
        ),
        (
            "2000-01-01/to/2000-01-03/by/2",
            {"date": ["2000-01-01", "2000-01-03"]},
        ),
    ),
)
def test_parse_date(date, expected):
    assert date_parser(date=date) == expected

Describe alternatives you've considered

No response

Additional context

No response

Organisation

B-Open / CADS-EQC

@malmans2 malmans2 added the enhancement New feature or request label Oct 17, 2023
@sandorkertesz sandorkertesz self-assigned this Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants