0

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

  • Does this answer your question? [How to change the datetime format in Pandas](https://stackoverflow.com/questions/38067704/how-to-change-the-datetime-format-in-pandas) – janreggie Oct 24 '22 at 01:52

2 Answers2

0

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.

janreggie
  • 334
  • 1
  • 4
  • 12
0
  • 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")))
Lowin Li
  • 139
  • 6