英文:
Trouble shooting pd.to_timedelta calculation failure
问题
我先用中文翻译代码部分:
for RxDat in df2:
condition = (df['Tdate'] > RxDat - pd.to_timedelta(46, unit="D")) & (df['Tdate'] < RxDat)
您提到您之前成功使用了上述条件语句,但现在遇到了以下错误:
TypeError: unsupported operand type(s) for -: 'str' and 'Timedelta'
您提供了以下数据来说明错误:
df['Tdate']
包含的日期数据如下:
[Timestamp('2004-08-25 00:00:00'), Timestamp('2004-10-13 00:00:00'), Timestamp('2004-12-13 00:00:00'), Timestamp('2005-02-21 00:00:00'), Timestamp('2005-04-28 00:00:00'), Timestamp('2005-08-24 00:00:00')]
df2['RxDate']
包含的日期数据如下:
[Timestamp('2004-08-20 00:00:00'), Timestamp('2004-08-23 00:00:00'), Timestamp('2004-08-18 00:00:00'), Timestamp('2004-08-15 00:00:00'), Timestamp('2004-08-12 00:00:00'), Timestamp('2004-08-13 00:00:00')]
您尝试了多种方式但无法找出错误的原因。
英文:
I have previously used the following conditional statement successfully
for RxDat in df2:
condition = (df['Tdate'] > RxDat - pd.to_timedelta(46, unit="D")) & (df['Tdate'] < RxDat)
Now I am getting the following error
> TypeError: unsupported operand type(s) for -: 'str' and 'Timedelta'
I have extracted the following data to illustrate the error
df['Tdate']
contains
[Timestamp('2004-08-25 00:00:00'), Timestamp('2004-10-13 00:00:00'), Timestamp('2004-12-13 00:00:00'), Timestamp('2005-02-21 00:00:00'), Timestamp('2005-04-28 00:00:00'), Timestamp('2005-08-24 00:00:00')]
df2['RxDate']
contains
[Timestamp('2004-08-20 00:00:00'), Timestamp('2004-08-23 00:00:00'), Timestamp('2004-08-18 00:00:00'), Timestamp('2004-08-15 00:00:00'), Timestamp('2004-08-12 00:00:00'), Timestamp('2004-08-13 00:00:00')]
I have tried looking at this a few ways and cannot see why I get the error?
答案1
得分: 3
如果循环由 d2
则 RxDat
为列名:
for RxDat in df2:
使用:
for RxDat in df2['RxDate']:
非循环解决方案,使用广播,输出为 2D 的 NumPy 数组:
a = df['Tdate'].to_numpy()[:, None]
b = df2['RxDate'].sub(pd.to_timedelta(46, unit="D")).to_numpy()
c = df2['RxDate'].to_numpy()
condition = (a > b) & (a < c)
英文:
If loop by d2
then RxDat
are columns names:
for RxDat in df2:
Use:
for RxDat in df2['RxDate']:
Non loop solution with broadcasting, output is 2d numpy array:
a = df['Tdate'].to_numpy()[:, None]
b = df2['RxDate'].sub(pd.to_timedelta(46, unit="D")).to_numpy()
c = df2['RxDate'].to_numpy()
condition = (a > b) & (a < c)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论