打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

我正在尝试使用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 ['Timestamp']列转换为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,)

谢谢!


**********************************

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


*****************************

如果你有每个时期的开始和结束时间,那么你真的不需要创建一个范围 . 您可以将逻辑与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
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
pandas小记:pandas时间序列分析和处理Timeseries
一文带你搞懂pandas中的时间处理(详细)
掌握Pandas时间序列分析的20个知识点
全网最全的Pandas入门教程
利用Python进行数据分析:【Pandas】(Series+DataFrame)
pandas中Timestamp类用法详解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服