Skip to content

assume UTC, and drop timezone #27

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

Merged
merged 2 commits into from
Apr 22, 2021
Merged

assume UTC, and drop timezone #27

merged 2 commits into from
Apr 22, 2021

Conversation

scottyhq
Copy link
Contributor

simple fix for #2

in brief, currently xarray does not like timezone information that can be present in (pandas.DatetimeIndex(['2019-12-30 00:00:01+00:00'], dtype='datetime64[ns, UTC]', freq=None)). As a result nicely formated datetimes are converted to nanosecond integers, causing confusion for users.

STAC datetimes should all be UTC. This additional line ensures the UTC timezone info is present (due to a pandas bug with infer_datetime_format=True), and then drops the timezone info for xarray.

A few examples of what this does:

# want to ensure these work
print(pd.to_datetime('2018-10-27T23:30:00Z').tz_convert('UTC').tz_localize(None))
print(pd.to_datetime('2018-10-27T23:30:00.123Z').tz_convert('UTC').tz_localize(None))

# valid ISO8601, but unclear if found in wild STACs
print(pd.to_datetime('2018-10-28T01:30:00+02:00').tz_convert('UTC').tz_localize(None))

# an invalid ISO8601 string (no Z) -> probably ok to hit errors in this case
pd.to_datetime('2018-10-28 01:30:00.1').tz_convert('UTC').tz_localize(None)
2018-10-27 23:30:00
2018-10-27 23:30:00.123000
2018-10-27 23:30:00
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-3e456a4822cb> in <module>
      7 
      8 # an invalid ISO8601 string (no Z) -> probably ok to hit errors in this case
----> 9 pd.to_datetime('2018-10-28 01:30:00.1').tz_convert('UTC').tz_localize(None)

pandas/_libs/tslibs/timestamps.pyx in pandas._libs.tslibs.timestamps.Timestamp.tz_convert()

TypeError: Cannot convert tz-naive Timestamp, use tz_localize to localize

Copy link
Owner

@gjoseph92 gjoseph92 left a comment

Choose a reason for hiding this comment

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

Awesome, thanks for the fix @scottyhq!

Would you mind also adding a note about this in the stackstac.stack docstring? Maybe something in the returns section about the time coordinate always being converted to UTC?

Copy link
Owner

@gjoseph92 gjoseph92 left a comment

Choose a reason for hiding this comment

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

Excellent! Thank you!

@gjoseph92 gjoseph92 merged commit 5ece164 into gjoseph92:main Apr 22, 2021
gjoseph92 added a commit that referenced this pull request Apr 23, 2021
With #27, the `tz_convert` would fail when we tripped over pandas-dev/pandas#41047, since the DatetimeIndex would be tz-naive. Now, we assume tz-naive datetimes are already in UTC.

Addresses #33 (comment)
gjoseph92 added a commit that referenced this pull request Apr 23, 2021
With #27, the `tz_convert` would fail when we tripped over pandas-dev/pandas#41047, since the DatetimeIndex would be tz-naive. Now, we assume tz-naive datetimes are already in UTC.

Addresses #33 (comment)
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

Successfully merging this pull request may close these issues.

2 participants