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

Date missing in datetime accessor #4983

Closed
observingClouds opened this issue Mar 2, 2021 · 4 comments · Fixed by #4994
Closed

Date missing in datetime accessor #4983

observingClouds opened this issue Mar 2, 2021 · 4 comments · Fixed by #4994

Comments

@observingClouds
Copy link
Contributor

observingClouds commented Mar 2, 2021

What happened:
I wonder if there is a reason, why there is no date attribute in the datetime accessor.

What you expected to happen:
As the time attribute is supported I would expect the same for the date attribute

Minimal Complete Verifiable Example:

import xarray as xr
import pandas as pd
time_coord = pd.date_range("2020-01-01","2020-01-03", freq="12H")
da = xr.DataArray([1,2,3,4,5], dims=["time"], coords={'time': time_coord})

print(da.time.dt.time)
#<xarray.DataArray 'time' (time: 5)>
#array([datetime.time(0, 0), datetime.time(12, 0), datetime.time(0, 0),
#       datetime.time(12, 0), datetime.time(0, 0)], dtype=object)
#Coordinates:
#  * time     (time) datetime64[ns] 2020-01-01 2020-01-01T12:00:00 ... 2020-01-03

print(da.time.dt.date)
#---------------------------------------------------------------------------
#AttributeError                            Traceback (most recent call last)
#<ipython-input-42-13741f407661> in <module>
#----> 1 da.time.dt.date
#AttributeError: 'DatetimeAccessor' object has no attribute 'date'

Suggestion:
A simple addition of

date = Properties._tslib_field_accessor(
        "date", "Date corresponding to datetimes", object
    )

in core/accessor_dt.py should do the trick. Happy to do a PR.

Anything else we need to know?:

Environment:

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.8.6 | packaged by conda-forge | (default, Oct 7 2020, 19:08:05)
[GCC 7.5.0]
python-bits: 64
OS: Linux
OS-release: 2.6.32-754.33.1.el6.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: 1.10.6
libnetcdf: 4.7.4

xarray: 0.17.0
pandas: 1.2.1
numpy: 1.20.0
scipy: 1.6.0
netCDF4: 1.5.5.1
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: 2.6.1
cftime: 1.4.1
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2021.02.0
distributed: 2021.02.0
matplotlib: 3.3.4
cartopy: 0.18.0
seaborn: 0.11.1
numbagg: None
pint: 0.16.1
setuptools: 49.6.0.post20210108
pip: 21.0.1
conda: None
pytest: None
IPython: 7.20.0
sphinx: None

@mathause
Copy link
Collaborator

mathause commented Mar 2, 2021

Sounds reasonable. Or maybe that's not supported in cftime? cc @spencerkclark

@spencerkclark
Copy link
Member

Yes, I agree, this seems reasonable. It's true that there is no object equivalent to datetime.date in cftime, but I do not think that is necessarily a requirement. We can raise an error in that instance.

I'll admit I personally have not found a need for functionality like this -- I typically prefer to use something like da.time.dt.floor("D"), since it retains the np.datetime64 or cftime type -- but if folks would find it useful I think it makes sense to add (this has come up on StackOverflow before too).

@observingClouds
Copy link
Contributor Author

observingClouds commented Mar 4, 2021

I didn't thought of using da.time.dt.floor("D"). This is indeed great to know, but as there seems to be more folks who are expecting da.time.dt.date to work, so I'd still like to see this implemented.

The time attribute that is already implemented has the same issue that it does not exists in cftime:

import numpy as np
import pandas as pd
import xarray as xr

attrs = {"units": "hours since 3000-01-01"}
ds = xr.Dataset({"time": ("time", [0, 1, 2, 3], attrs)})
xr.decode_cf(ds).time.dt.time
# AttributeError: 'CFTimeIndex' object has no attribute 'time'

I implemented the date attribute in PR #4994. The usage of date and CFTimeIndex raises an explicit AttributeError now and mentions the usage of floor("D").

@spencerkclark
Copy link
Member

For sure, thanks for getting started on a PR! I like the more informative AttributeError you added; I agree the existing error for the time attribute is difficult to follow. Let me know when it is ready for review.

Regarding da.time.dt.floor("D"), we could also consider adding a normalize method (e.g. in pandas) which is shorthand for the same thing.

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 a pull request may close this issue.

3 participants