Skip to content

Ensure decoding as datetime64[ns] #59

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
Mar 12, 2014
Merged

Ensure decoding as datetime64[ns] #59

merged 2 commits into from
Mar 12, 2014

Conversation

shoyer
Copy link
Member

@shoyer shoyer commented Mar 10, 2014

Pandas seems to have trouble constructing multi-indices when it's given
datetime64 arrays which don't have ns precision. The current version of
decode_cf_datetime will give datetime arrays with the default precision, which
is us. Hence, when coupled with the dtype restoring wrapper from PR #54, the
to_series() and to_dataframe() methods were broken when using decoded
datetimes.

@shoyer shoyer added the bug label Mar 10, 2014
shoyer added 2 commits March 10, 2014 18:26
Pandas seems to have trouble constructing multi-indices when it's given
datetime64 arrays which don't have ns precision. The current version of
decode_cf_datetime will give datetime arrays with the default precision, which
is us. Hence, when coupled with the dtype restoring wrapper from PR #54, the
`to_series()` and `to_dataframe()` methods were broken when using decoded
datetimes.
@akleeman
Copy link
Contributor

I'll go ahead and merge this in to fix the bug ... but I wonder if there is any way we can avoid using np.datetime64 objects. They seem under-developed / broken.

akleeman added a commit that referenced this pull request Mar 12, 2014
Ensure decoding as datetime64[ns]
@akleeman akleeman merged commit 4cbe328 into master Mar 12, 2014
@shoyer shoyer deleted the datetimens branch March 12, 2014 16:56
@shoyer
Copy link
Member Author

shoyer commented Mar 12, 2014

So I think the unfortunate reality is that np.datetime64 is still our best option. Otherwise we are left with dates as Python objects, which is painfully slow.

@akleeman
Copy link
Contributor

Yeah, does seem that way. Unfortunately that makes comparison with datetime objects a bit awkward. Perhaps we should include a to_datetime() the way pandas does? Certainly, it would be convenient to be able to do comparisons such as:

ds['time'].data > datetime.datetime(2014, 01, 01)

This is definitely not high priority though.

@shoyer
Copy link
Member Author

shoyer commented Mar 13, 2014

If you're sure that 'time' is stored as pandas.DatetimeIndex (e.g., you use a standard calendar and all dates within roughly 1700-2300), you can do those comparisons with ds['time'].index, which even lets you compare to strings: ds['time'].index > '2014-01-01'. But I agree it would be nice to have a general to_datetime method like pandas. If we already wrote this functionality (and it was somewhat tricky), we might as well expose it to users.

keewis pushed a commit to keewis/xarray that referenced this pull request Jan 17, 2024
* think I've fixed the bug

* used feature from python 3.9

* test but doesn't yet work properly

* only check subtree, not down to root

* add parent info to repr

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

Successfully merging this pull request may close these issues.

2 participants