【问题标题】:Timedelta and datetime data ( TypeError: cannot convert the series to <class 'int'>)Timedelta 和 datetime 数据(TypeError: cannot convert the series to <class 'int'>)
【发布时间】:2021-12-22 16:41:16
【问题描述】:

我有一个数据框,其开始时间值为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 &lt;class 'int'&gt;

【问题讨论】:

  • 您能否在问题中添加一些创建数据框的示例代码?我无法弄清楚如何获得像您所拥有的示例数据框。例如df.to_dict()的返回值
  • @user17242583 有答案吗?
  • df.apply(lambda x: datetime(2021,7,11,x['hst'],x['mst'],x['sst']),axis=1) 你正在传递系列(整个专栏),熊猫不知道如何处理它。使用 apply 并编写函数,就像对每一行进行操作
  • "查询获得的三个变量的数据类型是 pandas.core.series.Series" - 澄清一下,一个 DataFrame 沿一个公共轴组合了多个 pandas.Series。每个系列(DataFrame 的“列”)包含某些数据类型的元素,例如整数或时间增量(数据类型也可以在系列中混合)。

标签: python pandas datetime timedelta


【解决方案1】:

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

# 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    相关资源
    最近更新 更多