我需要减去两个日期,格式是Y-M-D hh:mm:ss,但我一直没有得到结果,即使我找到了许多接近我的追求的解决方案。 到目前为止我得到的是:
import pandas as pd
df = pd.read_excel('file.xlsx',header=0)
df['Time'] = pd.to_datetime(df['Time'])
df['Time']
import datetime as dt
df['Time'] = df['Time'].apply(lambda x: dt.datetime.strftime(x, '%Y-%m-%d %H:%M:%S'))
df['Time']
#print(df['Time'])
s1 = df['Time'].head(1)
print(s1)
s2=df.iloc[-1,2]
print(s2)
format = '%Y-%m-%d %H:%M:%S'
startDateTime = dt.datetime.strptime(s1, format)
endDateTime = dt.datetime.strptime(s2, format)
diff = endDateTime - startDateTime
我试过了pd.to\ U日期时间但我仍然得到这个错误:
TypeError: strptime() argument 1 must be str, not Series
你能帮我克服这个问题吗。 谢谢您!你知道吗
看起来您正在尝试使用head()函数检索df['Time']系列的第一个元素,但该函数不起作用。尝试:s1=df['Time'][0]。你知道吗
实际上,您正在将一个系列(s1)传入strtime函数,该函数需要一个字符串。你知道吗
如果将列转换为日期时间,则无需进行其他对话,只需将值选择为标量并减去:
相关问题 更多 >
编程相关推荐