Home > database >  How to convert "2006-04-01 00:00:00.000 0200" to "2006-04-01"?
How to convert "2006-04-01 00:00:00.000 0200" to "2006-04-01"?

Time:09-11

I am doing some data cleaning in python. I uploaded a dataset and I am trying to clean the date column. I want to visualize the date column with another component. But when I visualize it with the line plot on the x-axis the date displays as 2006-04-01 00:00:00.000 0200. I need it to display just 2006-04-01. The date column name is "Formatted Date". And the first couple of rows of the "Formatted Date" columns are the followings.

0: 2006-04-01 00:00:00.000 0200

1: 2006-04-01 01:00:00.000 0200

2: 2006-04-01 02:00:00.000 0200

3: 2006-04-01 03:00:00.000 0200

When I use the line plot for visualizations and use the "Formatted Date" column as the x-axis I just want it to display 2006-04-01. How would I do this?

CodePudding user response:

I think you're looking for this: df['date_column'] = pd.to_datetime(df['datetime_column']).dt.date

CodePudding user response:

Using string:

df['date_column'] = df['datetime_column'].str[:len('2006-04-01')  1]

Using datetime:

from datetime import datetime


dt = datetime.strptime('2006-04-01 00:00:00.000  0200', "%y-%m-%d %H:%M:%S")

dt.strftime("%y-%m-%d")
  • Related