熊猫在自定义开始时间戳进行重新采样

时间:2018-06-22 12:19:18

标签: pandas

我正在尝试在给定的开始时间重新采样数据

my program:
sales = [{'Timestamp': '2018-06-22 15:15:00', 'Jan': 150, 'Feb': 200, 'Mar': 140},
        {'Timestamp': '2018-06-22 15:44:00',  'Jan': 250, 'Feb': 250, 'Mar': 250},
        {'Timestamp': '2018-06-22 15:46:00',  'Jan': 200, 'Feb': 210, 'Mar': 215},
        {'Timestamp': '2018-06-22 16:16:00',  'Jan': 200, 'Feb': 210, 'Mar': 215},
        {'Timestamp': '2018-06-22 16:18:00',  'Jan': 200, 'Feb': 210, 'Mar': 215},
        {'Timestamp': '2018-06-22 16:20:00',  'Jan': 50,  'Feb': 90,  'Mar': 95 }]
df = pd.DataFrame(sales)
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
df = df.set_index('Timestamp')

ResampledDF = pd.DataFrame()
ResampledDF['J'] = df.Jan.resample("30T").max()
ResampledDF['F'] = df.Feb.resample("30T").max()
ResampledDF['M'] = df.Mar.resample("30T").max()
print(ResampledDF)

输出:

                       J    F    M
Timestamp                         
2018-06-22 15:00:00  150  200  140
2018-06-22 15:30:00  250  250  250
2018-06-22 16:00:00  200  210  215

这里输出从15:00:00开始自动采样数据,而我希望第一行在15:15:00,第二行在15:45:00等,如下所示

必需的输出:

                        J    F    M
 Timestamp                         
 2018-06-22 15:15:00  250  250  250
 2018-06-22 15:45:00  200  210  215
 2018-06-22 16:15:00  200  210  215

1 个答案:

答案 0 :(得分:4)

使用base parameter

In [233]: df.resample('30T', base=15).max()
Out[233]:
                     Feb  Jan  Mar
Timestamp
2018-06-22 15:15:00  250  250  250
2018-06-22 15:45:00  210  200  215
2018-06-22 16:15:00  210  200  215