[英]Finding datetime object in pandas df column
I have the following code, where I want to determine if a datetime object exists in a data frame.我有以下代码,我想确定数据框中是否存在日期时间 object。
Here is the code:这是代码:
df_grid['Date'] = pd.to_datetime(df_grid['Date'])
start_date = df_grid['Date'].iloc[0].date()
end_date = df_grid['Date'].iloc[-1].date()
current_date = start_date
while current_date < end_date:
current_date += datetime.timedelta(days=1)
print(current_date)
if current_date in df_grid['Date']: continue
print('not in')
And here is what the dataframe column looks like:这是 dataframe 列的样子:
Date
2021-11-01
2021-11-01
2021-11-01
2021-11-02
2021-11-02
2021-11-03
2021-11-03
...
Most of the dates do exist in the column;大多数日期确实存在于该列中; however, when I run the code, it indicates that none of the dates exist in the dataframe column.
但是,当我运行代码时,它表明 dataframe 列中不存在任何日期。 I've tried matching with and without.date() and get the same results.
我试过匹配 with 和 without.date() 并得到相同的结果。
Well, this is what I ended up doing to identify missing dates in the dateframe好吧,这就是我最终要做的,以识别日期框架中缺失的日期
while current_date < end_date:
missing = True
for row in df_grid.index:
if current_date == df_grid['Date'].iloc[row]:
missing = False
break
if missing == True:
[code to execute when date is missing]
current_date += datetime.timedelta(days=1)
Seems a lot more inefficient than what I originally tried to do, but at least this works.似乎比我最初尝试做的效率低得多,但至少这是有效的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.