我有一个datetime索引的数据帧。
index
2021-05-09 23:06:14.666
2021-05-09 23:06:14.786
2021-05-09 23:06:14.969
2021-05-09 23:06:15.022
2021-05-09 23:06:15.102
我正在尝试添加2000毫秒到索引datetime并创建一个新列。
df["new_time"] = df.index[df.index.get_indexer([df.index + datetime.timedelta(milliseconds=2000)], method='nearest')[0]]
但是我得到以下错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
谁能告诉我如何解决这个问题?
感谢您可以使用下面的命令来实现,不需要过于复杂
df["new_time"] = df.index + datetime.timedelta(milliseconds=2000)
第二个选项使用relativedelta:
import dateutil.relativedelta.relativedelta as rd
df["new_time"] = df.index + rd(microseconds=2000)