Skip to content

Date missing in datetime accessor #4983

Closed
@observingClouds

Description

@observingClouds

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions