简体   繁体   中英

Generate offsets for each row in a dataframe before and after datetime value

I have the following dataframe:

    ID  time
0   12  2017-09-17 15:30:00
1   13  2017-09-24 18:00:00

I would like to add 1h before each time value and 2 hours after with a minute frequency:

    ID  time
0   12  2017-09-17 14:30:00
1   12  2017-09-17 14:31:00
2   12  2017-09-17 14:32:00
3   12  2017-09-17 14:33:00
...
59  12  2017-09-17 15:29:00
60  12  2017-09-17 15:30:00
...
179 12  2017-09-17 17:29:00
180 12  2017-09-17 17:30:00
181 13  2017-09-24 17:00:00
...

Does anyone know how to generate these kinds of offsets?

This should work.

import pandas as pd

# Sample data
data = pd.DataFrame({
               "ID": ["ID1", "ID2"], 
               "time": ["2017-09-17 15:30:00", "2017-09-24 18:00:00"]
               })

data['time'] = pd.to_datetime(data['time'])
print(data.head())
    ID                time
0  ID1 2017-09-17 15:30:00
1  ID2 2017-09-24 18:00:00
# Logic
ID = []
time = []
for idx, row in data.iterrows():
    tm = row['time']
    split = pd.date_range(
                tm - pd.DateOffset(hours=1), 
                tm + pd.DateOffset(hours=2), 
                freq="1min"
                )

    val = [row['ID']] * len(split)
    ID.extend(val)
    time.extend(split)
# Result
df = pd.DataFrame({"ID": ID, "time": time})
print(df.head(20))
     ID                time
0   ID1 2017-09-17 14:30:00
1   ID1 2017-09-17 14:31:00
2   ID1 2017-09-17 14:32:00
3   ID1 2017-09-17 14:33:00
4   ID1 2017-09-17 14:34:00
5   ID1 2017-09-17 14:35:00
.
.
.

You can set time to index and reindex :

df.time = pd.to_datetime(df.time)
df.set_index('time', inplace=True)

new_index = pd.concat(pd.date_range(end=a,freq='T', periods=61)
                        .to_series() for a in df.index)

new_df = df.reindex(new_index).bfill().reset_index()

Output ( new_df.head() ):

                  index       ID
0   2017-09-17 14:30:00     12.0
1   2017-09-17 14:31:00     12.0
2   2017-09-17 14:32:00     12.0
3   2017-09-17 14:33:00     12.0
4   2017-09-17 14:34:00     12.0

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