7

I am working with timedeltas and it seems this code

copy_for_U.Time.astype('timedelta64[m]',copy=False);

does not change the dataframe - as it should, if I understood correctly from the doc, where it says:

Signature: full_df.Time.astype(dtype, copy=True, raise_on_error=True, **kwargs) Docstring: Cast object to input numpy.dtype Return a copy when copy = True (be really careful with this!)

jjrr
  • 1,038
  • 2
  • 13
  • 26
  • 7
    I believe the change will be made in place only if it can be made without creating a copy. The change of `dtype` you require here is "too much" of a change, so I think it (silently) returns a copy. This really is underlying numpy behaviour, see [`astype`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.astype.html). – IanS Jan 10 '17 at 10:49
  • 1
    IIUC do you need `copy_for_U.Time = copy_for_U.Time.astype('timedelta64[m]')` ? – jezrael Jan 10 '17 at 10:52
  • @jezrael yeah, that's what I did and it worked...but I wasted an afternoon looking somewhere else, as I couldn't plot – jjrr Jan 10 '17 at 12:34

1 Answers1

1

In order for the changes to be applied to the dataframe, one needs to assign the dataframe to the variable one wants (or pass inplace=True - this may be a nice thread to read).

Also, when doing that, you don't need to pass the copy=False, as @jezrael suggests.

Given that, this should solve your problem

copy_for_U.Time = copy_for_U.Time.astype('timedelta64[m]') 
Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83