Skip to content

recommended method to update variable values #6575

Answered by keewis
JessicaS11 asked this question in Q&A
Discussion options

You must be logged in to vote

you should be able to use copy(data=...):

In [9]: ds["time"] = ds["time"].copy(data=[np.datetime64(ds["time"].data[0].astype(str)[:-1])])
   ...: ds
Out[9]: 
<xarray.Dataset>
Dimensions:      (x: 2, y: 2, time_idx: 1)
Coordinates:
    lon          (x, y) float64 -99.83 -99.32 -99.79 -99.23
    lat          (x, y) float64 42.25 42.21 42.63 42.59
  * time_idx     (time_idx) int64 15
Dimensions without coordinates: x, y
Data variables:
    temperature  (x, y, time_idx) float64 26.84 -2.025 17.72 3.238
    time         (time_idx) datetime64[ns] 2019-01-11T05:26:31.323722

but do note that while I get a segfault with ds["time"].astype(np.datetime64), ds["time"].astype(str).astype(np.datetime64)

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
3 replies
@keewis
Comment options

@JessicaS11
Comment options

JessicaS11 May 5, 2022
Maintainer Author

@max-sixty
Comment options

Answer selected by JessicaS11
Comment options

You must be logged in to vote
1 reply
@JessicaS11
Comment options

JessicaS11 May 5, 2022
Maintainer Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants