To convert a string with the format "25 de mayo de 2023 14:53 hs." into a datetime object, you can use the strptime function from the datetime module.
import locale
from datetime import datetime
date_string = "25 de mayo de 2023 14:53 hs."
date_format = "%d de %B de %Y %H:%M hs."
# Set the appropriate locale for month names
locale.setlocale(locale.LC_TIME, 'es_ES.UTF-8')
# Parse the string into a datetime object
datetime_object = datetime.strptime(date_string, date_format)
print(datetime_object)
[EDIT]
If you want to convert a full column of data, use this code instead:
import locale
from datetime import datetime
import pandas as pd
# Set the appropriate locale for month names
locale.setlocale(locale.LC_TIME, 'es_ES.UTF-8')
# Define the conversion function
def convert_to_datetime(date_string):
date_format = "%d de %B de %Y %H:%M hs."
return datetime.strptime(date_string, date_format)
# Create a sample DataFrame
data = {'Fecha_de_venta': ['25 de mayo de 2023 14:53 hs.', '26 de junio de 2023 10:30 hs.', '27 de julio de 2023 18:45 hs.']}
df = pd.DataFrame(data)
# Apply the conversion function to the column
df['Fecha_de_venta'] = df['Fecha_de_venta'].apply(convert_to_datetime)
print(df['Fecha_de_venta'])