Home > database >  How to change 5 minute interval data to hourly interval
How to change 5 minute interval data to hourly interval

Time:05-27

So I have data of the temperature recorded on a thermostat every 5 minutes. I would like to change the data so that the file has only the data recorded every hour. I know there is a way to group the data but I don't want the sum/min/max/avg etc. I want the temperature for each hour. Let me know if this makes sense and if you need anything else. The data looks like this

CodePudding user response:

Simplest way with python would be with the pandas package. If you don't have it:

pip install pandas

Then in your script import it and use it to filter out all date values in the first column that are not on the hour. Let me know if this works for you.

import pandas as pd

df = pd.read_csv("name.csv")
df1 = df[df['DateTime'].dt.strftime('%M:%S').eq('00:00')].copy()
print(df1)
df1.to_csv(index=False)

CodePudding user response:

Here is a solution I came up with using some randomly generated data. I just filtered the data frame to where the Series.dt.minute attribute equals 0. I hope this is helpful.

import pandas as pd
import datetime
import numpy as np

rng = np.random.default_rng()

test_list = []

date_time = datetime.datetime.strptime('2022-05-26', '%Y-%m-%d')
for i in range(30):
    date_time  = datetime.timedelta(minutes=5)
    new_row = {'datetime': date_time, 'temp': rng.integers(low=30, high=95, size=1)[0]}
    test_list.append(new_row)

test_df = pd.DataFrame(test_list)
hourly_df = test_df[test_df['datetime'].dt.minute == 0]
  • Related