0

I want to convert this type of time: Μαρτίου 23 2021 19:20 Μαρτίου = month to datetime.

I used this format but didnt match:
df['Time'] = pd.to_datetime(df['Time'],format = '%B %d %Y %H:%M')

How could I solve it? Should I set the greek datetime in a way? How ??

  • Check this link - https://stackoverflow.com/questions/985505/locale-date-formatting-in-python. – tidakdiinginkan Jun 13 '21 at 22:32
  • @tidakdiinginkan the answer that you have linked won't work. Read the comments. – Mazen Jun 13 '21 at 23:19
  • @Maze Issue was closed citing the link I shared above. It won't have the entire solution that can be simply copy-pasted from, but a hint on how to go about doing it. – tidakdiinginkan Jun 13 '21 at 23:36
  • In addition, what you link is actually converts current time (which already in date format) to a different language, which isn't what the OP is having :) – Mazen Jun 13 '21 at 23:58

1 Answers1

1

You will need to use another library to translate Greek to English then you will convert the date from string to date type, here is a testable example:

import pandas as pd
from google_trans_new import google_translator

# set translator
translator = google_translator()

# dummy data
df = pd.DataFrame.from_dict(
    {
        "Time": [
            "Ιανουάριος 23 2021 19:20",
            "Φεβρουάριος 23 2021 19:20",
            "Μάρτιος 23 2021 19:20",
            "Απρίλιος 23 2021 19:20",
            "Μάιος 23 2021 19:20",
            "Ιούνιος 23 2021 19:20",
            "Ιούλιος 23 2021 19:20",
            "Αύγουστος 23 2021 19:20",
            "Σεπτέμβριος 23 2021 19:20",
            "Οκτώβριος 23 2021 19:20",
            "Νοέμβριος 23 2021 19:20",
            "Δεκέμβριος 23 2021 19:20",
        ]
    }
)

def convert(given_date):
    data = given_date.split(" ")
    # convery from Greek to English
    data[0] = translator.translate(data[0], lang_tgt="en")
    return " ".join(data)

# convert from Greek to English using mapping in Pandas
df["Time"] = df["Time"].apply(convert)
# convert time from string to date
df["Time"] = pd.to_datetime(df["Time"], format="%B %d %Y %H:%M")

# display results
print(df["Time"].iloc[:])

Clarification

After looking into it, it seems that googletrans library has some issues, so I am using google_trans_new to do the translation part. [1]

[1] googletrans stopped working with error 'NoneType' object has no attribute 'group'

Mazen
  • 486
  • 4
  • 8