You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Python代码以仅对DataFrame中的有效日期计算距今天数

Handling Invalid Dates When Calculating Days Since Current Date in a DataFrame

Hey, let's work through this problem together. Your code is hitting an error because when it encounters the "N/A" value in the Date column, datetime.strptime can't parse it into a valid date. On top of that, your loop is actually iterating over the DataFrame's column names (not rows), which would cause additional issues even without the invalid date. Here are two solid solutions:

Solution 1: Fix Your Loop with Error Handling

If you want to stick with a loop-based approach (great for learning!), we'll correct the row iteration and add checks for invalid dates:

import datetime
import pandas as pd

newdft = []
# Use iterrows() to loop through each row (idx = row index, row = row data)
for idx, row in dft.iterrows():
    temp_row = row.copy()
    date_value = row["Date"]
    
    # Check if the date is not "N/A" and not empty/missing
    if pd.notna(date_value) and date_value != "N/A":
        try:
            # Parse the date string
            parsed_date = datetime.datetime.strptime(date_value, "%m/%d/%Y")
            # Calculate days between current date and parsed date
            days_diff = (datetime.datetime.now() - parsed_date).days
            temp_row["Days"] = days_diff
        except ValueError:
            # Catch any other invalid date formats (e.g., typos like "13/01/2022")
            temp_row["Days"] = pd.NA
    else:
        # Handle "N/A" or missing values
        temp_row["Days"] = pd.NA
    
    newdft.append(temp_row)

# Convert the list of rows back to a DataFrame
newdft = pd.DataFrame(newdft)

Pandas has built-in tools for date handling that are way faster than manual loops, especially with large datasets. Here's the cleaner approach:

  1. First, convert the Date column to proper datetime type, forcing invalid values to NaT (Pandas' "Not a Time" marker):
dft["Date"] = pd.to_datetime(dft["Date"], format="%m/%d/%Y", errors="coerce")

The errors="coerce" parameter automatically turns unparseable values (like "N/A") into NaT.

  1. Calculate the days difference in one line—NaT values will result in NaN for the Days column:
dft["Days"] = (datetime.datetime.now() - dft["Date"]).dt.days

Bonus: Customize Missing Values

If you want to replace NaN in the Days column with a default value (like 0), just add:

dft["Days"] = dft["Days"].fillna(0)

内容的提问来源于stack exchange,提问作者Mary

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 01:22:30