2017-09-29 6 views
0

'start_time'(datetime)、 'end_time'(datetime)、 'mode'などの列を持つDataFrameがあります。 テーブルの異なる行の範囲に重複はありません。 「CURRENT_TIME」が元の「START_TIME間のリサンプルである 「CURRENT_TIME」、「モード」、他の列データフレームの日付範囲での再サンプリング

I新しいデータフレームを作成したいが、それは、各ようなので、元のデータフレームの列をリサンプリング'と' end_time 'を指定し、他のすべての列は元のテーブルの値の単なるコピーです。

例: オリジナルDATAFRAME:

  current_time  mode 
2017-06-01 06:38:00.000  x 
2017-06-01 06:38:10.000  x 
2017-06-01 06:38:20.000  x 
2017-06-01 06:38:30.000  x 
2017-06-01 06:38:40.000  x 
2017-06-01 06:38:50.000  x 
2017-06-01 17:22:00.000  y 
2017-06-01 17:22:10.000  y 
2017-06-01 17:22:20.000  y 

私が探している:

'10S' の与えられた 'FREQ' の
   start_time     end_time mode 
2017-06-01 06:38:00.000 2017-06-01 06:39:00.000  x 
2017-06-01 17:22:00.000 2017-06-01 17:22:30.000  y 

が、私は、次のデータフレームを取得したいのですがこれを行うための合理的に効率的でエレガントな方法です。

ありがとうございます!

答えて

0

あなたは使用することができます。

#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 
+1

ありがとう! 'pd.date_range(r.start_time、r.end_time、freq = '10S'、closed = 'left')' –

+0

スーパー、それは良い改善です。 – jezrael