TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time' site:stackoverflow.com
TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time' site:stackoverflow.com
我正在尝试减去两个日期时间对象的值,如下所示:
df["Time Taken"] = (pd.to_datetime(df['end_time']).dt.tz_convert('Asia/Kolkata').dt.time) - \
(pd.to_datetime(df['start_time']).dt.tz_convert('Asia/Kolkata').dt.time)
在如下所示的数据框中:
id serial reference_number date warehouse owner start_time end_time
0 352 655556555 5002 2022-05-15T13:33:00.208423Z Delhivery Goa Warehouse Delhivery_Goa 2022-05-31T11:26:10.977240Z 2022-05-31T11:59:55.421159Z
1 362 E2806995000040043136895A 5007 2022-05-18T10:37:28.871625Z Delhivery Goa Warehouse Delhivery_Goa 2022-05-31T11:26:10.977240Z 2022-05-31T11:59:55.421159Z
2 363 E28069950000500431368519 5008 2022-05-18T10:44:32.398842Z Delhivery Goa Warehouse Delhivery_Goa 2022-05-31T11:26:10.977240Z 2022-05-31T11:59:55.421159Z
3 364 E28069950000400431368536 5008 2022-05-18T10:44:32.398842Z Delhivery Goa Warehouse Delhivery_Goa 2022-05-31T11:26:10.977240Z 2022-05-31T11:59:55.421159Z
4 370 E28069950000500431368549 5010 2022-05-18T12:30:34.599759Z Delhivery Goa Warehouse Delhivery_Goa 2022-05-31T11:26:10.977240Z 2022-05-31T11:59:55.421159Z
我也尝试了 timedelta
,但出现以下错误:
only leading negative signs are allowed
我该怎么做?
首先减去由 Series.sub
, for seconds use Series.dt.total_seconds
::
转换为日期时间的列
df["Time Taken"] = (pd.to_datetime(df['end_time']).sub(pd.to_datetime(df['start_time']))
.dt.total_seconds())
对于来自 timedeltas 的格式 HH:MM:SS
使用:
def f(x):
ts = x.total_seconds()
hours, remainder = divmod(ts, 3600)
minutes, seconds = divmod(remainder, 60)
return ('{}:{:02d}:{:02d}').format(int(hours), int(minutes), int(seconds))
df["Time Taken"] = (pd.to_datetime(df['end_time']).sub(pd.to_datetime(df['start_time']))
.apply(f))
你让这种方式变得比它需要的更复杂:
刚刚
df["Time Taken"] = (
pd.to_datetime(df['end_time'])
- pd.to_datetime(df['start_time'])
)
这应该会为您提供一列时间增量,您可以根据需要设置格式。
您可以跳过的步骤:
- 正在转换时区。数据库中的时间已经指定了具体的时间点,因此可以计算出差异。从 UTC 转换为本地不会更改结果。
- 从
.dt.time
的日期中提取时间。这是导致错误的原因,因为时间不支持减法。没有为这些实现减法,因为两次之间的差异的概念不是 well-defined。比如(凌晨 1 点 - 凌晨 4 点)-3 小时或 +21 小时甚至 -2 或 -4 小时,因为夏令时和正常时间之间发生了切换?
我正在尝试减去两个日期时间对象的值,如下所示:
df["Time Taken"] = (pd.to_datetime(df['end_time']).dt.tz_convert('Asia/Kolkata').dt.time) - \
(pd.to_datetime(df['start_time']).dt.tz_convert('Asia/Kolkata').dt.time)
在如下所示的数据框中:
id serial reference_number date warehouse owner start_time end_time
0 352 655556555 5002 2022-05-15T13:33:00.208423Z Delhivery Goa Warehouse Delhivery_Goa 2022-05-31T11:26:10.977240Z 2022-05-31T11:59:55.421159Z
1 362 E2806995000040043136895A 5007 2022-05-18T10:37:28.871625Z Delhivery Goa Warehouse Delhivery_Goa 2022-05-31T11:26:10.977240Z 2022-05-31T11:59:55.421159Z
2 363 E28069950000500431368519 5008 2022-05-18T10:44:32.398842Z Delhivery Goa Warehouse Delhivery_Goa 2022-05-31T11:26:10.977240Z 2022-05-31T11:59:55.421159Z
3 364 E28069950000400431368536 5008 2022-05-18T10:44:32.398842Z Delhivery Goa Warehouse Delhivery_Goa 2022-05-31T11:26:10.977240Z 2022-05-31T11:59:55.421159Z
4 370 E28069950000500431368549 5010 2022-05-18T12:30:34.599759Z Delhivery Goa Warehouse Delhivery_Goa 2022-05-31T11:26:10.977240Z 2022-05-31T11:59:55.421159Z
我也尝试了 timedelta
,但出现以下错误:
only leading negative signs are allowed
我该怎么做?
首先减去由 Series.sub
, for seconds use Series.dt.total_seconds
::
df["Time Taken"] = (pd.to_datetime(df['end_time']).sub(pd.to_datetime(df['start_time']))
.dt.total_seconds())
对于来自 timedeltas 的格式 HH:MM:SS
使用:
def f(x):
ts = x.total_seconds()
hours, remainder = divmod(ts, 3600)
minutes, seconds = divmod(remainder, 60)
return ('{}:{:02d}:{:02d}').format(int(hours), int(minutes), int(seconds))
df["Time Taken"] = (pd.to_datetime(df['end_time']).sub(pd.to_datetime(df['start_time']))
.apply(f))
你让这种方式变得比它需要的更复杂:
刚刚
df["Time Taken"] = (
pd.to_datetime(df['end_time'])
- pd.to_datetime(df['start_time'])
)
这应该会为您提供一列时间增量,您可以根据需要设置格式。
您可以跳过的步骤:
- 正在转换时区。数据库中的时间已经指定了具体的时间点,因此可以计算出差异。从 UTC 转换为本地不会更改结果。
- 从
.dt.time
的日期中提取时间。这是导致错误的原因,因为时间不支持减法。没有为这些实现减法,因为两次之间的差异的概念不是 well-defined。比如(凌晨 1 点 - 凌晨 4 点)-3 小时或 +21 小时甚至 -2 或 -4 小时,因为夏令时和正常时间之间发生了切换?