Skip to content

Timedelta dt accessor does not work #3609

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

Closed
Tillsten opened this issue Dec 10, 2019 · 4 comments · Fixed by #3612
Closed

Timedelta dt accessor does not work #3609

Tillsten opened this issue Dec 10, 2019 · 4 comments · Fixed by #3612

Comments

@Tillsten
Copy link

Tillsten commented Dec 10, 2019

MCVE Code Sample

# Your code here
import xarray as xr
import datetime as dt

now = dt.datetime.now()
dt_array = xr.DataArray([now + dt.timedelta(seconds=i) for i in range(10)])

dt_array.dt.second #works

start_t = dt_array - dt_array[0]
start_t.dt.second #fails

Expected Output

Last line should give me the time timedelta in seconds. Instead it raises an exception. Same for hour and minute.

Output of xr.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)] python-bits: 64 OS: Windows OS-release: 10 machine: AMD64 processor: Intel64 Family 6 Model 158 Stepping 10, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None libhdf5: None libnetcdf: None

xarray: 0.14.0
pandas: 0.25.3
numpy: 1.17.4
scipy: 1.2.1
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.1.1
cartopy: None
seaborn: None
numbagg: None
setuptools: 42.0.2.post20191203
pip: 19.1.1
conda: None
pytest: 5.0.0
IPython: 7.7.0
sphinx: 2.2.1

@andersy005
Copy link
Member

andersy005 commented Dec 11, 2019

  1. Per Pandas documentation, the following are the defined attributes for Timedelta:
asm8 Return a numpy timedelta64 array scalar view.
components Return a components namedtuple-like.
days Number of days.
delta Return the timedelta in nanoseconds (ns), for internal compatibility.
microseconds Number of microseconds (>= 0 and less than 1 second).
nanoseconds Return the number of nanoseconds (n), where 0 <= n < 1 microsecond.
resolution Return a string representing the lowest timedelta resolution.
resolution_string Return a string representing the lowest timedelta resolution.
seconds Number of seconds (>= 0 and less than 1 day).

and since xarray coerces the dataarray to a Pandas Series prior accessing the requested attribute, calling start_t.dt.second fails.

  1. Since seconds is a defined attribute for pandas.TimeDelta, I expect start_t.dt.seconds to work. However, it fails:
In [33]: start_t.dt.seconds
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-33-e1e0943720f8> in <module>
----> 1 start_t.dt.seconds

AttributeError: 'DatetimeAccessor' object has no attribute 'seconds'

This is likely a bug in xarray.

  1. Pandas appears to be establishing differences between DatetimeProperties and TimedeltaProperties:
In [16]: start_t_series.dt
Out[16]: <pandas.core.indexes.accessors.TimedeltaProperties object at 0x7f2afafd45c0>

In [17]: dt_array_series.dt
Out[17]: <pandas.core.indexes.accessors.DatetimeProperties object at 0x7f2bf7ccd320>

Xarray, on the other hand, seems to be treating them the same:

In [18]: dt_array.dt
Out[18]: <xarray.core.accessor_dt.DatetimeAccessor at 0x7f2afb6e8710>

In [19]: start_t.dt
Out[19]: <xarray.core.accessor_dt.DatetimeAccessor at 0x7f2afb6a20f0>
  • Should xarray establish differences between DatetimeProperties and TimedeltaProperties??

@dcherian
Copy link
Contributor

This is more unimplemented feature than a bug. .dt is really a DatetimeAccessor.

The Timedelta functionality hasn't been implemented e.g.

second = _tslib_field_accessor("second", "The seconds of the datetime", np.int64)

@andersy005
Copy link
Member

@dcherian, Thank you for chiming in! Are there any other issues with discussions about implementation for Timedelta functionality? I am happy to take a stab at implementing this.

@dcherian
Copy link
Contributor

I think this would be a welcome feature. It seems like there should be a lot of shared code between the two accessors.

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