Home > Enterprise >  how to convert a column with string datetime to datetime format
how to convert a column with string datetime to datetime format

Time:01-06

i want to convert a column with string date '19 Desember 2022' for example (the month name is in Indonesian), to supported datetime format without translating it, how do i do that?

already tried this one df_train['date'] = pd.to_datetime(df_train['date'], format='%d %B %Y') but got error time data '19 Desember 2022' does not match format '%d %B %Y' (match)

incase if anyone want to see the row image

CodePudding user response:

Pandas doesn't recognize bahasa(indonesian language) Try replacing the spelling of December (as pointed out you can use a one liner and create a new column):

df_train["formatted_date"] = pd.to_datetime(df_train["date"].str.replace("Desember", "December"), format="%d %B %Y")
print(df_train)

Output:

  user_type              date formatted_date
0   Anggota  19 Desember 2022     2022-12-19
1   Anggota  19 Desember 2022     2022-12-19
2   Anggota  19 Desember 2022     2022-12-19
3   Anggota  19 Desember 2022     2022-12-19
4   Anggota  19 Desember 2022     2022-12-19

CodePudding user response:

Try using dateparser

import dateparser
df_train = pd.DataFrame(['19 Desember 2022', '20 Desember 2022', '21 Desember 2022', '22 Desember 2022'], columns = ['date'])
df_train['date'] = [dateparser.parse(x) for x in df_train['date']]
df_train

Output:

    date
0   2022-12-19
1   2022-12-20
2   2022-12-21
3   2022-12-22
  • Related