0

For example, one of my column of my dataframe has data like below:

29-APR-19 11.50.00.000000000 PM

29-APR-19 11.50.00.000000000 AM

Hence, I need to update the column having PM to:

29-APR-19 23.50.00.000000000 PM

How can we do that?

I have tried formatting the column to specific date format types, but not able to find the solution.

PV8
  • 5,799
  • 7
  • 43
  • 87
  • does this one help? https://stackoverflow.com/questions/35032135/how-to-add-hour-to-pandas-dataframe-column , is your column a datetime column? you can chage it with `df.dytpes` – PV8 Jun 25 '19 at 09:05
  • https://stackoverflow.com/questions/19229190/convert-12-hour-into-24-hour-times – sygneto Jun 25 '19 at 09:12
  • 1
    Convert to `datetime` type for easier control over formatting. In your case use `pd.to_datetime(df['datetime'], format='%d-%b-%y %I.%M.%S.%f %p')`. Then use `df['datetime'].dt.strftime('%d-%b-%y %H.%M.%S.%f %p')` – Chris Adams Jun 25 '19 at 09:14
  • @ChrisA solution is the correct one. – Erfan Jun 25 '19 at 10:34

1 Answers1

0

If need same format here is necessary add 12 hours for datetimes below 12:00:00.

Solution is first convert column by to_datetime, then add 12 hours by condition and for same format use Series.dt.strftime:

print (df)
                              Date
0  29-APR-19 11.50.00.000000000 PM
1  29-APR-19 11.50.00.000000000 AM

df['Date'] = pd.to_datetime(df['Date'], format='%d-%b-%y %I.%M.%S.%f %p') 
df.loc[df['Date'].dt.hour < 12] += pd.Timedelta(12, 'h')

df['Date'] = df['Date'].dt.strftime('%d-%b-%y %I.%M.%S.%f %p')
print (df)

                           Date
0  29-Apr-19 11.50.00.000000 PM
1  29-Apr-19 11.50.00.000000 PM
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252