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

Handling time in string format #90

Closed
SarahAlidoost opened this issue May 31, 2021 · 4 comments
Closed

Handling time in string format #90

SarahAlidoost opened this issue May 31, 2021 · 4 comments

Comments

@SarahAlidoost
Copy link
Contributor

SarahAlidoost commented May 31, 2021

In ewatercycle, the start/end time is given as a string in the ISO format (which uses the Grogerian calendar), and in UTC time. A date string ISO format is represented differently by different libraries:

from datetime import datetime, timezone
datetime.fromisoformat('2000-01-01T00:00:00+00:00')
#out: datetime.datetime(2000, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)
datetime(2000,1, 1, tzinfo=timezone.utc).isoformat()
#out: '2000-01-01T00:00:00+00:00'
import numpy as np
np.datetime64('2000-01-01T00:00:00.0')
#out: numpy.datetime64('2000-01-01T00:00:00.000')
np.datetime_as_string(np.array(np.datetime64('2000-01-01T00:00:00.0')), timezone='UTC')
#out: '2000-01-01T00:00:00.000Z'
import pandas as pd
pd.date_range(start='1/1/2000', periods=1, tz='UTC')
#out: DatetimeIndex(['2000-01-01 00:00:00+00:00'], dtype='datetime64[ns, UTC]', freq='D')
import cftime
cftime.datetime(2000,1, 1)
#out: cftime.datetime(2000, 1, 1, 0, 0, 0, 0)
cftime.datetime(2000,1, 1).isoformat()
#out: '2000-01-01T00:00:00'
  • xarray with np.datetime64
import numpy as np
import xarray as xr
da = xr.DataArray(
        [0.1],
        coords=[[np.datetime64('2000-01-01')]],
        dims=['time'],
        )
da.coords['time'][0]
#out: array('2000-01-01T00:00:00.000000000', dtype='datetime64[ns]')
np.datetime_as_string(da.coords['time'][0], timezone='UTC')
#out: '2000-01-01T00:00:00.000000000Z'
  • xarray with datetime
from datetime import datetime, timezone
import xarray as xr
da = xr.DataArray(
        [0.1],
        coords=[[datetime(2000, 1, 1, tzinfo=timezone.utc)]],
        dims=['time'],
        )
da.coords['time'][0]
#out: array('2000-01-01T00:00:00.000000000', dtype='datetime64[ns]')
np.datetime_as_string(da.coords['time'][0], timezone='UTC')
#out: '2000-01-01T00:00:00.000000000Z'
@SarahAlidoost
Copy link
Contributor Author

I suggest using the numpy iso format i.e. '2000-01-01T00:00:00Z' and library dateutil to parse it as:

from dateutil.parser import parse
parse('2000-01-01T00:00:00Z')
#out: datetime.datetime(2000, 1, 1, 0, 0, tzinfo=tzutc())

and to convert the datetime object back to the iso format:

parse('2000-01-01T00:00:00Z').strftime("%Y-%m-%dT%H:%M:%SZ")
#out: '2000-01-01T00:00:00Z'

@Peter9192
Copy link
Collaborator

Nice overview! So the internal representation of dateutil is 'normal' datetime.datetime objects?

@SarahAlidoost
Copy link
Contributor Author

Nice overview! So the internal representation of dateutil is 'normal' datetime.datetime objects?

It seems so. It is mentioned in the documentation that "the dateutil module provides powerful extensions to the standard datetime module".

@Peter9192
Copy link
Collaborator

Addressed in #137

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

No branches or pull requests

3 participants