あなたは使用することができます。
#convert columns to datetimes if necessary
df['start_time']= pd.to_datetime(df['start_time'])
df['end_time']= pd.to_datetime(df['end_time'])
#subtract 10s for no last row from values from end_time column
df['end_time']= df['end_time'] - pd.Timedelta(10, unit='s')
#loop by list comprehension for list of date ranges
#concat to one big DataFrame
df1 = (pd.concat([pd.Series(r.Index,
pd.date_range(r.start_time, r.end_time, freq='10S'))
for r in df.itertuples()])
.reset_index())
df1.columns = ['current_time','idx']
print (df1)
current_time idx
0 2017-06-01 06:38:00 0
1 2017-06-01 06:38:10 0
2 2017-06-01 06:38:20 0
3 2017-06-01 06:38:30 0
4 2017-06-01 06:38:40 0
5 2017-06-01 06:38:50 0
6 2017-06-01 17:22:00 1
7 2017-06-01 17:22:10 1
8 2017-06-01 17:22:20 1
EDIT OPのコメントによって:
用パラメータclosed=left
場合:
pd.date_range(r.start_time, r.end_time, freq='10S', closed='left')
が、その後可能オミット減算です。
#join all another columns by index
df2 = df1.set_index('idx').join(df.drop(['start_time','end_time'], 1)).reset_index(drop=True)
print (df2)
current_time mode
0 2017-06-01 06:38:00 x
1 2017-06-01 06:38:10 x
2 2017-06-01 06:38:20 x
3 2017-06-01 06:38:30 x
4 2017-06-01 06:38:40 x
5 2017-06-01 06:38:50 x
6 2017-06-01 17:22:00 y
7 2017-06-01 17:22:10 y
8 2017-06-01 17:22:20 y
別の解決策:
#create column from index for last join (index values has to be unique)
df = df.reset_index()
#reshape dates to datetimeindex
df1 = (df.melt(df.columns.difference(['start_time','end_time']),
['start_time', 'end_time'],
value_name='current_time')
.drop('variable', 1)
.set_index('current_time'))
print (df1)
index mode
current_time
2017-06-01 06:38:00 0 x
2017-06-01 17:22:00 1 y
2017-06-01 06:38:50 0 x
2017-06-01 17:22:20 1 y
#group by index column and resample, NaNs are replaced by forward filling
df2 = df1.groupby('index').resample('10S').ffill().reset_index(0, drop=True).drop('index', 1)
print (df2)
mode
current_time
2017-06-01 06:38:00 x
2017-06-01 06:38:10 x
2017-06-01 06:38:20 x
2017-06-01 06:38:30 x
2017-06-01 06:38:40 x
2017-06-01 06:38:50 x
2017-06-01 17:22:00 y
2017-06-01 17:22:10 y
2017-06-01 17:22:20 y
ありがとう! 'pd.date_range(r.start_time、r.end_time、freq = '10S'、closed = 'left')' –
スーパー、それは良い改善です。 – jezrael