Thomas Kennedy
Thomas Kennedy

Reputation: 41

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

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"]

})

Upvotes: 1

Views: 1027

Answers (2)

Umar.H
Umar.H

Reputation: 23099

Two steps:

First, let's 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, let's 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.

Upvotes: 2

Let&#39;s try
Let&#39;s try

Reputation: 1058

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 "----")

Upvotes: 0

Related Questions