简体   繁体   中英

How to extract the mid- timestamp in an minute timeseries python?

The problem is to calculate the mid-point of every hour in a day when the sun is above horizon.

For Example: When Sunrise is at 8.42 AM, I need the mid timestamp between 8.42 AM and 9.00 AM then again mid timestamp between 9.00 AM and 10:00 AM and so on.

I have generated the Time series using pvlib when the sun is above horizon and here is the time series on January 1st, 2019 for the first hour. The time series is stored in a data frame.

Time series:

2019-01-01 08:42:00+01:00

2019-01-01 08:43:00+01:00

2019-01-01 08:44:00+01:00

2019-01-01 08:45:00+01:00

2019-01-01 08:46:00+01:00

2019-01-01 08:47:00+01:00

2019-01-01 08:48:00+01:00

2019-01-01 08:49:00+01:00

2019-01-01 08:50:00+01:00

2019-01-01 08:51:00+01:00

2019-01-01 08:52:00+01:00

2019-01-01 08:53:00+01:00

2019-01-01 08:54:00+01:00

2019-01-01 08:55:00+01:00

2019-01-01 08:56:00+01:00

2019-01-01 08:57:00+01:00

2019-01-01 08:58:00+01:00

2019-01-01 08:59:00+01:00

2019-01-01 09:00:00+01:00

I need to get the middle one in this minute time series which means I need to get either one of these.

2019-01-01 08:50:00+01:00

2019-01-01 08:51:00+01:00

A very intesting questions, which I've also met a few times when dealing with solar radiation time series. First off, it's always easier if you provide a small example, eg:

import pandas as pd
import numpy as np
date_range = pd.date_range('2019-01-01 08:42',periods=200, freq='1min')
df = pd.DataFrame(index=date_range, data={'data':np.arange(len(date_range))})

One solution is to define a column with the minute of each time step, then to do your mean resampling and add the mean minute as a timedelta to the index:

df['minute'] = df.index.minute
dfr = df.resample('1h').mean()
dfr['middle_time'] = dfr.index + pd.to_timedelta(dfr['minute']+0.5, unit='min')
print(dfr.head())

This results in:

                     data    minute     middle_time
2019-01-01 08:00:00    8.5    50.5      2019-01-01 08:51:00
2019-01-01 09:00:00   47.5    29.5      2019-01-01 09:30:00
2019-01-01 10:00:00  107.5    29.5      2019-01-01 10:30:00
2019-01-01 11:00:00  167.5    29.5      2019-01-01 11:30:00
2019-01-01 12:00:00  198.5     0.5      2019-01-01 12:01:00

You could of course also add the minute offset to the index itself, depending on if you pass the index or a datetime column to PVLib.

You can try something like this:

df['Time'] = pd.to_datetime(df['Time'])
df['Time'] = df['Time'].sort_values()
print(df.loc[int(len(df['Time'])/2)]['Time'])

2019-01-01 08:51:00+01:00

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