简体   繁体   English

Pandas 给出 datetime.date' 被强制为 datetime

[英]Pandas gives datetime.date' is coerced to a datetime

Im trying to compare two date values.我试图比较两个日期值。 My data contains a datecolumn.我的数据包含一个日期列。

    dateCols = ['Document Date']
    data = pd.read_excel(os.path.join(delivery_file_path, f), parse_dates=dateCols,
                                   dayfirst=True, sheet_name='Refined',skiprows=1)

    data['Document Date'] = pd.to_datetime(data['Document Date'])
    df = pd.DataFrame(columns=['Document Date'], data=data['Document Date'])
    print(df.dtypes)  <-- prints **datetime64[ns]**
    if (df['Document Date'] >= start) and (df['Document Date'] <= end):
.....

I get following error;我收到以下错误;

datetime.date' is coerced to a datetime. In the future pandas will
not coerce, and a TypeError will be raised. To retain the current
behavior, convert the 'datetime.date' to a datetime with
'pd.Timestamp'.
  if (df['Document Date'] >= start) and (df['Document Date'] <= end):
...
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

What is wrong with my above date comparison?我上面的日期比较有什么问题?

I sorted out like adding.any()我整理喜欢add.any()

if ((df['Document Date'] >= start) & (df['Document Date'] <= end)).any():

My requirement was, the file i process contain a monthly worth records or not.我的要求是,我处理的文件是否包含每月价值记录。 It is for data quality check.它用于数据质量检查。 So using any() would be fine.所以使用 any() 就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM