Home > Enterprise >  Python - Transform/Change a date format
Python - Transform/Change a date format

Time:10-24

I have a problem which is changing the date format from '21 Sep 2022 04:37:17' to '2022-09-21 04:37:17'. I'm using the code:

import pandas as pd
fileIn = 'C:/Users/xxx.xlsx'
df = pd.read_excel(fileIn, index_col=0)
diaStrava = df['Data da atividade'].str[:2]
mesStrava = df['Data da atividade'].str[6:9]
anoStrava = df['Data da atividade'].str[13:18]
if mesStrava == 'out':
    c_mesStrava = '10'
dataStrava = diaStrava   '-'   c_mesStrava   '-'   anoStrava

And I'm getting the error:

... Traceback (most recent call last):

... if mesStrava == 'out': ... in nonzero raise ValueError( ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

can anyone help me? Thanks

CodePudding user response:

Instead of converting the date format using string manipulation, you could instead convert the string to datetime and then back to string. See pd.to_datetime and Series.dt.strftime

---should work, although this line might be problematic since it's not in English

if mesStrava == 'out':

Consider using dateparser to parse non-English dates.

CodePudding user response:

  • firstly, create the dataframe
import pandas as pd
df = pd.DataFrame({'Data da atividade':['21 Sep 2022 04:37:17']})
print(df)
  • secondly, transfer
print(pd.to_datetime(df['Data da atividade']).apply(lambda x: x.strftime("%Y-%m-%d %H:%M:%S")))
  • Related