I have following dataframe:
time u10 ... tsn tp
longitude latitude ...
20.0 45.0 2014-01-01 00:00:00 2.595551 ... 272.453827 0.000014
45.0 2014-01-01 01:00:00 2.615493 ... 273.159973 0.000000
45.0 2014-01-01 02:00:00 2.587403 ... 273.122192 0.000000
45.0 2014-01-01 03:00:00 2.528865 ... 273.050903 0.000000
45.0 2014-01-01 04:00:00 2.556740 ... 272.772491 0.000000
I want to subtract neighboring records of column u10
for all values of column time
,except one value of column time
( where time ends with 00:00:00
)
I need following output:
time u10 ... tsn tp
longitude latitude ...
20.0 45.0 2014-01-01 00:00:00 2.595551 ... 272.453827 0.000014
45.0 2014-01-01 01:00:00 0.019942 ... 273.159973 0.000000
45.0 2014-01-01 02:00:00 -0.02809 ... 273.122192 0.000000
45.0 2014-01-01 03:00:00 -0.058538 ... 273.050903 0.000000
I can do df.loc
combining with df['time'].shift()-df['time']
but that will work for all record.
How can I make this work with desired output?
P.S. I am looking for vectorized solution.
CodePudding user response:
Use np.where. Where midnight occurs, keep u10 values, where it doesnt find the consecutive differences.
df['u10']=np.where(df['time'].dt.time==time(0,0,0), df['u10'], df['u10'].diff())
time u10
longitude latitude
20.0 45.0 2014-01-01 00:00:00 2.595551
45.0 2014-01-01 01:00:00 0.019942
45.0 2014-01-01 02:00:00 -0.028090
45.0 2014-01-01 03:00:00 -0.058538
45.0 2014-01-01 04:00:00 0.027875