我在当前周开始日期和日期周开始之间取得了不同,其中日期是日期列。这种差异以天为单位。当我试图定义函数将这几天转换为1周差异,2周差异等等。
我收到的错误是:
无法将类型'Timedelta'与'str'类型进行比较
请帮我解决这个问题。在定义函数时我担心我错了吗?这是定义函数的代码:
def check(diff):
for d in final_data['diff']:
if ((d > '0 days') and (d <= '7 days')):
weekdiff = 'OneWeekDiff'
elif ((d > '8 days') and (d <= '14 days')):
weekdiff = 'TwoWeekDiff'
else:
weekdiff = 'Current Week'
return weekdiff
要找出两列之间的差异,我只需要这样减法: final_data ['diff'] = final_data ['CurrentWeekStartDay'] - final_data ['InvoiceWeekstartDay']
打印(final_data [ '差异'] 0 14天 1 14天 2 14天
答案 0 :(得分:1)
您需要转换to_timedelta
字符串0 days
,7 days
...:
然后我稍微修改一下功能 - 删除循环和else
。您可以apply
对DataFrame
列
def check(d):
weekdiff = 'Current Week'
if ((d > pd.to_timedelta('0 days')) and (d <= pd.to_timedelta('7 days'))):
weekdiff = 'OneWeekDiff'
elif ((d > pd.to_timedelta('8 days')) and (d <= pd.to_timedelta('14 days'))):
weekdiff = 'TwoWeekDiff'
return weekdiff
print (final_data['diff'].apply(check))
样品:
final_data = pd.DataFrame({'b': {0: pd.Timestamp('2016-01-13 00:00:00'),
1: pd.Timestamp('2016-01-05 00:00:00'),
2: pd.Timestamp('2016-01-03 00:00:00')},
'a': {0: pd.Timestamp('2016-01-01 00:00:00'),
1: pd.Timestamp('2016-01-02 00:00:00'),
2: pd.Timestamp('2016-01-03 00:00:00')},
'diff': {0: pd.Timedelta('12 days 00:00:00'),
1: pd.Timedelta('3 days 00:00:00'),
2: pd.Timedelta('0 days 00:00:00')}})
print (final_data)
a b diff
0 2016-01-01 2016-01-13 12 days
1 2016-01-02 2016-01-05 3 days
2 2016-01-03 2016-01-03 0 days
print (final_data['diff'].apply(check))
0 TwoWeekDiff
1 OneWeekDiff
2 Current Week
Name: diff, dtype: object