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

Enable subtracting a scalar cftime.datetime object from a CFTimeIndex #2671

Closed
spencerkclark opened this issue Jan 14, 2019 · 0 comments
Closed

Comments

@spencerkclark
Copy link
Member

Code Sample, a copy-pastable example if possible

In [1]: import xarray

In [2]: times = xarray.cftime_range('2000', periods=3)

In [3]: times - times[0]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-97cbca76a8af> in <module>
----> 1 times - times[0]

~/xarray-dev/xarray/xarray/coding/cftimeindex.py in __sub__(self, other)
    417             return CFTimeIndex(np.array(self) - other.to_pytimedelta())
    418         else:
--> 419             return CFTimeIndex(np.array(self) - other)
    420
    421     def _add_delta(self, deltas):

~/xarray-dev/xarray/xarray/coding/cftimeindex.py in __new__(cls, data, name)
    238         result = object.__new__(cls)
    239         result._data = np.array(data, dtype='O')
--> 240         assert_all_valid_date_type(result._data)
    241         result.name = name
    242         return result

~/xarray-dev/xarray/xarray/coding/cftimeindex.py in assert_all_valid_date_type(data)
    194             raise TypeError(
    195                 'CFTimeIndex requires cftime.datetime '
--> 196                 'objects. Got object of {}.'.format(date_type))
    197         if not all(isinstance(value, date_type) for value in data):
    198             raise TypeError(

TypeError: CFTimeIndex requires cftime.datetime objects. Got object of <class 'datetime.timedelta'>.

Problem description

This should result in a pandas.TimedeltaIndex, as is the case for a pandas.DatetimeIndex:

In [4]: import pandas

In [5]: times = pandas.date_range('2000', periods=3)

In [6]: times - times[0]
Out[6]: TimedeltaIndex(['0 days', '1 days', '2 days'], dtype='timedelta64[ns]', freq=None)

Expected Output

In [1]: import xarray

In [2]: times = xarray.cftime_range('2000', periods=3)

In [3]: times - times[0]
Out[3]: TimedeltaIndex(['0 days', '1 days', '2 days'], dtype='timedelta64[ns]', freq=None)

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.6 | packaged by conda-forge | (default, Jul 26 2018, 09:55:02)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]
python-bits: 64
OS: Darwin
OS-release: 18.2.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: 1.10.4
libnetcdf: 4.6.2

xarray: 0.10.9+127.ga7129d1
pandas: 0.24.0.dev0+1332.g5d134ec
numpy: 1.15.4
scipy: 1.1.0
netCDF4: 1.4.2
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.0.3.4
PseudonetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
cyordereddict: None
dask: 1.0.0
distributed: 1.25.1
matplotlib: 3.0.2
cartopy: None
seaborn: 0.9.0
setuptools: 40.6.3
pip: 18.1
conda: None
pytest: 3.10.1
IPython: 7.2.0
sphinx: None

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

No branches or pull requests

1 participant