gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
How to convert object to time in pandas data frame if the object has format '09.06.2023,…
- User
How to convert object to time in pandas data frame if the object has format '09.06.2023, 00:00:00' where 09 is day, 06 is month and 2023 is year?
- Assistant
To convert an object in the format '09.06.2023, 00:00:00' into a datetime format in a pandas DataFrame, you can use the
pd.to_datetime()function along with the appropriate format code.Here's an example of how to do it:
import pandas as pd # Create a sample DataFrame data = {'date_column': ['09.06.2023, 00:00:00', '15.07.2024, 12:30:15', '21.12.2025, 09:45:30']} df = pd.DataFrame(data) # Convert the 'date_column' from object to datetime df['date_column'] = pd.to_datetime(df['date_column'], format='%d.%m.%Y, %H:%M:%S') # Print the updated DataFrame print(df)Output:
date_column 0 2023-06-09 00:00:00 1 2024-07-15 12:30:15 2 2025-12-21 09:45:30In the
pd.to_datetime()function, theformatparameter is defined as'%d.%m.%Y, %H:%M:%S'to match the provided date format. Here,%dcorresponds to day,%mto month,%Yto year,%Hto hour,%Mto minute, and%Sto second. Adjust the format code as needed based on your specific datetime format.