简体   繁体   English

如何用 pandas 中日期列中的最大日期填充缺失的日期?

[英]How to fill missing date witht the max of date in the date column in pandas?

I am trying to fill a missing value in a date column with the max of the date in the same column.我正在尝试用同一列中日期的最大值填充日期列中的缺失值。 however with the below code provided, it is not getting transformed.但是,通过提供以下代码,它不会被转换。

us_copy['Inv_Latest_Document_Date'] = us_copy['Inv_Latest_Document_Date'].fillna(us_copy.groupby('Vendor_Name')['Inv_Latest_Document_Date'].max())

Use GroupBy.transform for Series with same size like original:GroupBy.transform用于与原始大小相同的系列:

#if necessary convert to datetimes
us_copy['Inv_Latest_Document_Date'] = pd.to_datetime(us_copy['Inv_Latest_Document_Date'])

s = us_copy.groupby('Vendor_Name')['Inv_Latest_Document_Date'].transform('max')
us_copy['Inv_Latest_Document_Date'] = us_copy['Inv_Latest_Document_Date'].fillna(s)

EDIT: If need repalce by max of all column values:编辑:如果需要所有列值的最大值:

#if necessary convert to datetimes
us_copy['Inv_Latest_Document_Date'] = pd.to_datetime(us_copy['Inv_Latest_Document_Date'])

a = us_copy['Inv_Latest_Document_Date'].max()
us_copy['Inv_Latest_Document_Date'] = us_copy['Inv_Latest_Document_Date'].fillna(a)

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

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