如何在Python中更改函数中的日期时间
原始代码如下所示如何在Python中更改函数中的日期时间,python,pandas,datetime,Python,Pandas,Datetime,原始代码如下所示 def fn(row): d1 = row.sampletime d2 = d1 + pd.Timedelta(nDays - 1, 'D') return pd.Series(df2.loc[d1:d2].values.reshape((1, -1), order='F').squeeze(), index=cols) df1 = df1.join(df1.apply(fn, axis=1)) df1 我想将日期sampletime
def fn(row):
d1 = row.sampletime
d2 = d1 + pd.Timedelta(nDays - 1, 'D')
return pd.Series(df2.loc[d1:d2].values.reshape((1, -1),
order='F').squeeze(), index=cols)
df1 = df1.join(df1.apply(fn, axis=1))
df1
我想将日期sampletime
更改为提前3天,因此我又添加了两行代码,如下所示:
def fn(row):
m = datetime.timedelta(days = 3) #new line
df1['sampletime'] = df1['sampletime'] - m #new line
d1 = row.sampletime
d2 = d1 + pd.Timedelta(nDays - 1, 'D')
return pd.Series(df2.loc[d1:d2].values.reshape((1, -1),
order='F').squeeze(), index=cols)
df1 = df1.join(df1.apply(fn, axis=1))
df1
我收到了错误消息:
ValueError: columns overlap but no suffix specified: Index(['maxtemp1', 'maxtemp2', 'maxtemp3', 'mintemp1', 'mintemp2', 'mintemp3', 'rainfall1', 'rainfall2', 'rainfall3', 'wind1', 'wind2', 'wind3', 'day_week1', 'day_week2', 'day_week3'], dtype='object')
当数据帧中没有公共列时,会出现错误
以下是pandas.join()的参数:
DataFrame.join(self,other,on=None,how='left',lsuffix='',rsuffix='',sort=False)
要解决此问题,需要在调用函数时指定
lsuffix
和rsuffix
属性。如果数据帧中没有公共列,则会出现错误以下是pandas.join()的参数:
DataFrame.join(self,other,on=None,how='left',lsuffix='',rsuffix='',sort=False)
要解决此问题,需要在调用函数时指定lsuffix
和rsuffix
属性