Timedelta and datetime data ( TypeError: cannot convert the series to <class 'int'>)

Timedelta and datetime data ( TypeError: cannot convert the series to <class 'int'>)

我有一个数据帧,其开始时间值为 0 days 04:52:00,结束时间为 0 days 08:54:00,第三个变量为 AT.

ST  ET  AT
0 days 04:52:00 0 days 08:54:00 1198
0 days 04:54:00 0 days 08:59:00 1195
0 days 04:56:00 0 days 10:16:00 1120
0 days 04:57:00 1 days 01:33:00 204
0 days 04:57:00 0 days 09:15:00 1182
0 days 05:02:00 0 days 08:53:00 1209
0 days 05:04:00 0 days 20:23:00 521

查询得到的三个变量的数据类型为pandas.core.series.Series

type(df['ST'])
type(df['ET'])
type(df['AT'])

df.to_dict()
{'ST': {0: '0 days 04:52:00',
  1: '0 days 04:54:00',
  2: '0 days 04:56:00',
  3: '0 days 04:57:00',
  4: '0 days 04:57:00',
  5: '0 days 05:02:00',
  6: '0 days 05:04:00'},
 'ET': {0: '0 days 08:54:00',
  1: '0 days 08:59:00',
  2: '0 days 10:16:00',
  3: '1 days 01:33:00',
  4: '0 days 09:15:00',
  5: '0 days 08:53:00',
  6: '0 days 20:23:00'},
 'AT': {0: 1198, 1: 1195, 2: 1120, 3: 204, 4: 1182, 5: 1209, 6: 521}}

我想从 ST 和 ET 中提取小时、分钟和秒值,并以日期时间格式使用它。

df['hst']= df['ST'].dt.components['hours'].astype(int)
df['mst']= df['ST'].dt.components['minutes'].astype(int)
df['sst']= df['ST'].dt.components['seconds'].astype(int)
df['het']= df['ET'].dt.components['hours'].astype(int)
df['met']= df['ET'].dt.components['minutes'].astype(int)
df['set']= df['ET'].dt.components['seconds'].astype(int)

然而,即使在使用 .astype(int) 之后 df['hst'], df['mst'], df['sst'], df['het'], df['met'], and df['set'] 的数据类型仍然是 pandas.core.series.Series

我在执行以下代码时收到错误消息:

from datetime import datetime, timedelta
start = datetime(2021,7,11,df['hst'],df['mst'],df['sst'])

错误:cannot convert the series to <class 'int'>

您可以将持续时间(pd.Series 类型 timedelta)添加到参考日期以获取日期时间列:

# make sure the data type of the elements is timedelta:
df['ST'] = pd.to_timedelta(df['ST'])
df['ET'] = pd.to_timedelta(df['ET'])

# we need a reference date to which we can add the durations
ref_date = pd.Timestamp("2021-07-11")

df['start'] = ref_date + df['ST']
df['end'] = ref_date + df['ET']

df.head()
               ST              ET    AT               start                 end
0 0 days 04:52:00 0 days 08:54:00  1198 2021-07-11 04:52:00 2021-07-11 08:54:00
1 0 days 04:54:00 0 days 08:59:00  1195 2021-07-11 04:54:00 2021-07-11 08:59:00
2 0 days 04:56:00 0 days 10:16:00  1120 2021-07-11 04:56:00 2021-07-11 10:16:00
3 0 days 04:57:00 1 days 01:33:00   204 2021-07-11 04:57:00 2021-07-12 01:33:00
4 0 days 04:57:00 0 days 09:15:00  1182 2021-07-11 04:57:00 2021-07-11 09:15:00