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

xr$interp problem with translation from posXct to np.datetime64 #2

Open
jmarkloew opened this issue Sep 10, 2021 · 2 comments
Open

Comments

@jmarkloew
Copy link
Member

I tried to interpolate a xr.dataarray in R with this code:

polRatio <- ds$VV/ds$VH
polRatio$attrs$units= "VV/VH"

load_sq= seq.Date(from = as.Date("01/10/2019", format= "%m/%d/%Y"), to= as.Date("12/31/2019", format= "%m/%d/%Y"), by= "6 day")
intp_sq= seq.POSIXt(from = as.POSIXct("01/10/2019", format= "%m/%d/%Y"), to= as.POSIXct("12/31/2019", format= "%m/%d/%Y"), by= "day")

intp_sq= intp_sq[-which(intp_sq %in% load_sq)]

CR_intp= polRatio$interp(time= intp_sq[1:length(intp_sq)])

But it gave me the following error:

Error in py_call_impl(callable, dots$args, dots$keywords): KeyError: numpy.datetime64('NaT')

Detailed traceback:
  File "/home/datacube/anaconda3/envs/jupyterhub/lib/python3.9/site-packages/xarray/core/dataarray.py", line 1725, in interp
    ds = self._to_temp_dataset().interp(
  File "/home/datacube/anaconda3/envs/jupyterhub/lib/python3.9/site-packages/xarray/core/dataset.py", line 3131, in interp
    obj, newidx = missing._localize(obj, {k: v})
  File "/home/datacube/anaconda3/envs/jupyterhub/lib/python3.9/site-packages/xarray/core/missing.py", line 559, in _localize
    imin = index.get_loc(minval, method="nearest")
  File "/home/datacube/anaconda3/envs/jupyterhub/lib/python3.9/site-packages/pandas/core/indexes/datetimes.py", line 705, in get_loc
    raise KeyError(orig_key) from err

Traceback:

1. polRatio$interp(time = intp_sq[1:length(intp_sq)])
2. py_call_impl(callable, dots$args, dots$keywords)

I also checked the type of the timestamp object of the xr.dataarray I was trying to interpolate and it is posxct
I am definitely no expert but could it point to a problem with the translation from posixct to np.datetime64?

Thanks and best regards

@16EAGLE
Copy link
Member

16EAGLE commented Sep 14, 2021

Without having investigated your example further, I noticed that intp_sq is of length 0. I guess that is unintended, right?

When running intp_sq[-which(intp_sq %in% load_sq)], intp_sq becomes POSIXct of length 0. Could you try changing your example and see whether the interpolation error persists? If yes I will look into the date/time conversion.

@jmarkloew
Copy link
Member Author

There was a mixup between Date and POSIX formats. If I use a POSIXt sequence for int_p and load_sq xr$interp works fine. However, if a Date sequence is given in int_p the error says it needs a datetime object. It would be nice if an exception is added that catches the Date format or at least an error message which points to the required POSIX format. dc_load works fine with Date format so it would be more comfortable if it is some how handled. I also added the original error message that occurrs when using Date instead of POSIX format:

Error in py_call_impl(callable, dots$args, dots$keywords): TypeError: When interpolating over a datetime-like coordinate, the coordinates to interpolate to must be either datetime strings or datetimes. Instead got
<xarray.IndexVariable 'time' (time: 296)>
array([datetime.date(2019, 1, 11), datetime.date(2019, 1, 12),
       datetime.date(2019, 1, 13), ..., datetime.date(2019, 12, 28),
       datetime.date(2019, 12, 29), datetime.date(2019, 12, 31)], dtype=object)
Attributes:
    units:    seconds since 1970-01-01 00:00:00

Detailed traceback:
  File "/home/datacube/anaconda3/envs/jupyterhub/lib/python3.9/site-packages/xarray/core/dataarray.py", line 1725, in interp
    ds = self._to_temp_dataset().interp(
  File "/home/datacube/anaconda3/envs/jupyterhub/lib/python3.9/site-packages/xarray/core/dataset.py", line 3123, in interp
    validated_indexers = {
  File "/home/datacube/anaconda3/envs/jupyterhub/lib/python3.9/site-packages/xarray/core/dataset.py", line 3124, in <dictcomp>
    k: _validate_interp_indexer(maybe_variable(obj, k), v)
  File "/home/datacube/anaconda3/envs/jupyterhub/lib/python3.9/site-packages/xarray/core/dataset.py", line 3114, in _validate_interp_indexer
    raise TypeError(

Traceback:

1. polRatio$interp(time = intp_sq[1:length(intp_sq)])

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

No branches or pull requests

2 participants