按自定义和可变长度日期时间范围分箱Pandas Dataframe

时间:2018-03-26 20:29:55

标签: python pandas datetime binning

我正在尝试使用pd.cut来记录详细说明在不同时间长度内进行测试的结果的行。容器的目的是最终添加一个新列,我可以根据该测试结果记录的时间戳是否落在该测试的定义时间范围内来定义每行测试结果所属的测试。

df中的数据如下所示

Index  DeviceID    QuantResult1    QuantResult2  Timestamp
  0    15D         7903            387403        2017-10-14 00:28:00
  1    15D         3429734         786           2017-10-14 00:29:10
  2    15D         2320923         546           2017-10-14 00:31:15
  3    15D         232             435869        2017-10-14 00:50:05
  4    15D         34032984        12            2017-10-14 01:10:07

到目前为止,我尝试为每个单独的测试创建单独的日期时间范围,以便

test_1 = pd.date_range(start = '2017-10-14 00:20:00', end = '2017-10-14 00:33:15', freq = 'S')
test_2 = pd.date(pd.date_range(start = '2017-10-14 00:49:15', end = '2017-10-14 01:15:15', freq = 'S')

然后我在所有测试中创建了一个date_ranges列表

 test_list = [test_1,test_2]

然后我尝试使用pd.cut

 df = pd.cut(df['Timestamp'],bins = test_list, labels = ['Test_1','Test_2'])

但是我收到以下错误:

TypeError: <class 'int'> type object 0. 

最终我希望数据看起来像这样

    Index  DeviceID    QuantResult1    QuantResult2  Timestamp         Test
  0    15D         7903            387403        2017-10-14 00:28:00  Test_1 
  1    15D         3429734         786           2017-10-14 00:29:10  Test_1
  2    15D         2320923         546           2017-10-14 00:31:15  Test_1  
  3    15D         232             435869        2017-10-14 00:50:05  Test_2
  4    15D         34032984        12            2017-10-14 01:10:07  Test_2

你能告诉我错误的来源是什么吗?

我已经尝试将test_list映射为整数,并在将df [&#39; Timestamp&#39;]列转换为np.int64后与df进行比较,如下所示。

test_list_int64 = list(map(np.int64,test_list))
df = pd.cut(df['Timestamp'].astype(np.int64),bins = test_list_int64)

但是这种努力产生了以下错误:

ValueError: operands could not be broadcast together with shapes (1320,) (87480,) 

谢谢!

2 个答案:

答案 0 :(得分:0)

boundary = pd.to_datetime('2017-10-14 00:30:00')

现在使用np.where

test['Test'] = np.where(test['TimeStamp']< boundary, 'test_1', np.where(test['TimeStamp'], 'test_2', None))

输出:

    Index  DeviceID    QuantResult1    QuantResult2              TimeStamp           Test
    0   15D     7903       387403   2017-10-14 00:28:00     2017-10-14 00:28:00     test_1
    1   15D     3429734     786     2017-10-14 00:29:10     2017-10-14 00:29:10     test_1
    2   15D     2320923     546     2017-10-14 00:31:15     2017-10-14 00:31:15     test_2
    3   15D     232         435869  2017-10-14 00:50:05     2017-10-14 00:50:05     test_2
    4   15D     34032984    12      2017-10-14 01:10:07     2017-10-14 01:10:07     test_2

答案 1 :(得分:0)

如果你有每个时期的开始和结束时间,那么你真的不需要创建一个范围。您可以将逻辑与datetime对象一起使用。如果有的话,应该很容易推广到越来越多的测试。

import pandas as pd

start_t1 = pd.to_datetime('2017-10-14 00:20:00')
stop_t1 = pd.to_datetime('2017-10-14 00:33:15')
start_t2 = pd.to_datetime('2017-10-14 00:49:15')
stop_t2 = pd.to_datetime('2017-10-14 01:15:15')

df.loc[(df.Timestamp > start_t1) & (df.Timestamp < stop_t1), 'Test'] = 'Test_1'
df.loc[(df.Timestamp > start_t2) & (df.Timestamp < stop_t2), 'Test'] = 'Test_2'

  DeviceID  Quant Result2  QuantResult1           Timestamp    Test
0      15D         387403          7903 2017-10-14 00:28:00  Test_1
1      15D            786       3429734 2017-10-14 00:29:10  Test_1
2      15D            546       2320923 2017-10-14 00:31:15  Test_1
3      15D         435869           232 2017-10-14 00:50:05  Test_2
4      15D             12      34032984 2017-10-14 01:10:07  Test_2
相关问题