简体   繁体   中英

How to convert hourly data to half hourly

I have the following dataframe:

          datetime           temp
0   2015-01-01 00:00:00     11.22
1   2015-01-01 01:00:00     11.32
2   2015-01-01 02:00:00     11.30
3   2015-01-01 03:00:00     11.25
4   2015-01-01 04:00:00     11.32
... ... ...
31339   2018-07-29 19:00:00 17.60
31340   2018-07-29 20:00:00 17.49
31341   2018-07-29 21:00:00 17.44
31342   2018-07-29 22:00:00 17.39
31343   2018-07-29 23:00:00 17.37

I want to convert this dataframe to have data each half hour, and inpute each new position with the mean between the previous and the following value (or any similar interpolation), that is, for example:

         datetime           temp
0   2015-01-01 00:00:00       11.00
1   2015-01-01 00:30:00      11.50
2   2015-01-01 01:00:00       12.00

Is there any pandas/datetime function to assist in this operation? Thank you

You can use the resample() function in Pandas. With this you can set the time to down/upsample to and then what you want to do with it (mean, sum etc.). In your case you can also interpolate between the values.

For this to work your datetime column will have to be a datetime dtype, then set it to the index.

df['datetime'] = pd.to_datetime(df['datetime'])
df.set_index('datetime', inplace=True)

Then you can resample to 30 minutes ('30T') and then interpolate.

df.resample('30T').interpolate()

Resulting in...

                       temp
datetime                   
2015-01-01 00:00:00  11.220
2015-01-01 00:30:00  11.270
2015-01-01 01:00:00  11.320
2015-01-01 01:30:00  11.310
2015-01-01 02:00:00  11.300
2015-01-01 02:30:00  11.275
2015-01-01 03:00:00  11.250
2015-01-01 03:30:00  11.285
2015-01-01 04:00:00  11.320

Read more about the frequency strings and resampling in the Pandas docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM