Python 具有结束-开始列的时间数据帧到10分钟范围的数据帧
我有一个dataframe,其中描述的开始和结束时间在列中。说明在另一列中 我试图将数据集转换为10分钟范围的数据帧。在输出数据框中,选择开始日期和结束日期。如果没有数据,则应为nan 例如:Python 具有结束-开始列的时间数据帧到10分钟范围的数据帧,python,python-3.x,pandas,dataframe,timestamp,Python,Python 3.x,Pandas,Dataframe,Timestamp,我有一个dataframe,其中描述的开始和结束时间在列中。说明在另一列中 我试图将数据集转换为10分钟范围的数据帧。在输出数据框中,选择开始日期和结束日期。如果没有数据,则应为nan 例如: start_date = 15/09/2020 14:00:00 end_date = 16/09/2020 23:00:00 数据帧 start_time end_time info 15/09/2020 14:35:54 15/0
start_date = 15/09/2020 14:00:00
end_date = 16/09/2020 23:00:00
数据帧
start_time end_time info
15/09/2020 14:35:54 15/09/2020 15:05:48 A
15/09/2020 15:54:05 16/09/2020 02:15:22 B
16/09/2020 21:17:10 16/09/2020 22:15:04 A
...
timestamp info
15/09/2020 14:00:00 nan
15/09/2020 14:10:00 nan
15/09/2020 14:20:00 nan
15/09/2020 14:30:00 A
15/09/2020 14:40:00 A
15/09/2020 14:50:00 A
15/09/2020 15:00:00 A
15/09/2020 15:10:00 A
15/09/2020 15:20:00 nan
15/09/2020 15:30:00 nan
15/09/2020 15:40:00 nan
15/09/2020 15:50:00 B
15/09/2020 16:00:00 B
15/09/2020 16:10:00 B
15/09/2020 16:20:00 B
...
测试数据帧
start_time end_time info
15/09/2020 14:35:54 15/09/2020 15:05:48 A
15/09/2020 15:54:05 16/09/2020 02:15:22 B
16/09/2020 21:17:10 16/09/2020 22:15:04 A
...
timestamp info
15/09/2020 14:00:00 nan
15/09/2020 14:10:00 nan
15/09/2020 14:20:00 nan
15/09/2020 14:30:00 A
15/09/2020 14:40:00 A
15/09/2020 14:50:00 A
15/09/2020 15:00:00 A
15/09/2020 15:10:00 A
15/09/2020 15:20:00 nan
15/09/2020 15:30:00 nan
15/09/2020 15:40:00 nan
15/09/2020 15:50:00 B
15/09/2020 16:00:00 B
15/09/2020 16:10:00 B
15/09/2020 16:20:00 B
...
到目前为止,我已经做了:
data['timestampStart'] = pd.to_datetime(data['timestampStart'])
data['timestampEnd'] = pd.to_datetime(data['timestampEnd'])
range_time = pd.date_range(start=start_date, end=end_date, freq='10min')
test= pd.DataFrame()
test['timestamp'] = pd.to_datetime(range_time)
for index, row in data.iterrows():
if test['timestamp'] > data['timestampStart'] & test['timestamp'] < data['timestampEnd']:
test['description'] = data['description']
else:
test['description'] == np.nan
data['timestampStart']=pd.to_datetime(data['timestampStart'])
data['timestampEnd']=pd.to_datetime(data['timestampEnd'])
范围\时间=局部放电日期\范围(开始=开始\日期,结束=结束\日期,频率='10min')
test=pd.DataFrame()
测试['timestamp']=pd.to\u datetime(范围\u时间)
对于索引,data.iterrows()中的行:
如果测试['timestamp']>data['timestamp start']&测试['timestamp']
我在if语句中得到一个错误:
&:“Timestamp”和“Timestamp”的操作数类型不受支持
但我被困在如何解决它。欢迎提出任何建议
谢谢你可以试试这个:
def check_date(s,d):
if len(d[(d.timestampStart<s)&(d.timestampEnd>s)]) > 0:
return d[(d.timestampStart<s)&(d.timestampEnd>s)].iloc[0]['info']
else:
return None
test['description'] = test.timestamp.apply(lambda x: check_date(x, data))
我们基本上是写下每个“开始”/“结束”对之间的10分钟间隔,然后通过df.explode()将数据放在一列中。
然后,您可以将其合并到“测试”数据帧中,以获得所需的所有时隙
test = test.merge(new_data, on='timestamp', how='left')
我认为您的错误是因为您需要在多个条件周围加括号。if(test['timestamp']>data['timestamp start']&(test['timestamp']