Convert dataframe column to datetime only if length of string is not zero

给你一囗甜甜゛ 提交于 2021-01-29 07:02:17

问题


I'd like to convert a dataframe column which has a date string. But in some cases, the date string might be empty due to certain conditions. So I just want all the other rows in that column to be converted to datetime format except the rows in that particular column which might be blank. Is it possible?

What I've tried so far:

Option1:

df['etime'] = pd.to_datetime(df['etime'],errors='ignore').dt.strftime('%Y-%m-%d %H:%M')

Option 2:

    for ind in df.index:
        if (df['etime'].str.len()[ind] == 0) :
            df.loc[ind, 'etime'] = "----"
        else:
           df.loc[ind, 'etime'] = <need to convert this row to datetime>

Kindly provide your suggestions.

Dataframe sample:

data = pd.DataFrame({

    'day' : [15, 17, 20, 14, 25],

    'etime': ["20200811235205", "", "20200811215205", "20200811225205", "20200811235203"]

})

回答1:


Two steps,

first lets create a series with your datetimes and coerce the bad values into NaTs

s = pd.to_datetime(data['etime'],errors='coerce',format='%Y%m%d%H%M%S')

second, lets find any values that aren't NaT and replace them with your target formatting.

data.loc[~s.isna(),'etime'] = s.dt.strftime('%Y-%m-%d %H:%M')

   day             etime
0   15  2020-08-11 23:52
1   17                  
2   20  2020-08-11 21:52
3   14    20200811265205
4   25  2020-08-11 23:52
  • assuming 26 is a typo in your hour column at index 3.



回答2:


You can try something like:

df["etime"] =  df["etime"].apply(lambda x: pd.to_datetime(x,errors='ignore').strftime('%Y-%m-%d %H:%M') if len(x) !=0 else "----")


来源:https://stackoverflow.com/questions/63393340/convert-dataframe-column-to-datetime-only-if-length-of-string-is-not-zero

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!