Converting minutes data to hourly data
Use Grouper
for aggregate by hours with column Unique_id
by sum
:
df['datetime'] = pd.to_datetime(df['datetime'])
df = df.groupby([pd.Grouper(freq='H', key='datetime'), 'Unique_id']).sum().reset_index()
print (df)
datetime Unique_id Value
0 2018-01-28 00:00:00 105714 1285
1 2018-01-28 00:00:00 206714 888
2 2018-01-28 23:00:00 105714 225
3 2018-01-28 23:00:00 206714 410
How to convert minute data to hourly data correctly in R?
You can shift the time back 60 seconds, do as.hourly
, then shift the time forward 60 seconds. This maintains the groupings. You'll need to rename the columns too:
setNames(shift.time(to.hourly(shift.time(data, -60)), 60), c("Open", "High", "Low", "Close"))
#> Open High Low Close
#> 2020-01-01 01:00:00 1 3 1 3
#> 2020-01-01 02:00:00 4 6 4 6
#> 2020-01-01 03:00:00 7 9 7 9
#> 2020-01-01 04:00:00 10 12 10 12
Aggregate15 minute data to hourly
When working with time series, I suggest you work with xts
package for this, and for example hourly.apply
:
library(xts)
dat.xts <- xts(Total_Solar_Gesamt$TotalSolar_MW,
as.POSIXct(otal_Solar_Gesamt$Timedate))
hourly.apply(dat.xts,sum)
More general you can use period.apply
which is (lapply
equivalent) , for example to aggregate your data each 2 hours you can do the following:
ends <- endpoints(zoo.data,'hours',2)
period.apply(dat.xts,ends ,sum)
Aggregate 10 minute interval data to hourly
This worked perfectly:
df.resample('60T').mean()
how to convert 15 mins data to hourly in pandas?
Use resample
with closed='right'
. But first we convert your Time
column to datetime
type:
df['Time'] = pd.to_datetime(df['Time'])
df.resample('H', on='Time', closed='right').mean().reset_index()
Time Gen1 Gen2
0 2021-01-09 00:00:00 14.5 20.0
1 2021-01-09 01:00:00 50.0 41.0
To convert the Time
column back to time format, use:
df['Time'] = df['Time'].dt.time
Time Gen1 Gen2
0 00:00:00 14.5 20.0
1 01:00:00 50.0 41.0
Related Topics
How to Manually Set Geom_Bar Fill Color in Ggplot
Retrieve Census Tract from Coordinates
Ggplot2 Error "No Layers in Plot"
Ggplot2 Time Series Plotting: How to Omit Periods When There Is No Data Points
Subsetting a Data.Table by Range Making Use of Binary Search
How to Change the Order of the Panels in Simple Lattice Graphs
Porting Set Operations from R's Data Frames to Data Tables: How to Identify Duplicated Rows
With the R Package Xlsx, How to Set Na.Strings When Reading an Excel File
Scale_Y_Log10() and Coord_Trans(Ytrans = 'Log10') Lead to Different Results
Shiny R - Download the Result of a Table
In R, Match Function for Rows or Columns of Matrix
R Markdown - Format Text in Code Chunk with New Lines
How to Retrieve the Most Repeated Value in a Column Present in a Data Frame
How to Preprocess Features When Some of Them Are Factors
Extract Column Name in Mutate_If Call