How to remove time from my date time column in python

132 Views Asked by At

I am a beginner in python and need some guidance. I read in several worksheets from a single Excel Workbook and appended them into a single dataframe in Python. However, the output file has changed my date column which is still defined as an object into a different date format and added time, see example below:

Original date format Date format outputed from Python
02/03/1992 1992-02-03 00:00:00

This is the code I used to append the worksheets

*import pandas as pd

# First, combine all the pages in each Workbook into one sheet

df_toAppend = pd.concat(pd.read_excel('C:/Sam/Predicting Outcomes .xlsx', sheet_name=None), ignore_index=True)*

I have tried to just extract the date from the column, but it says its needs to be a date stamp

I have tried the following to extract just the date part:

df['Date_of_Birth']=df['Date_of_Birth'].dt.date

but get the following error: AttributeError: Can only use .dt accessor with datetimelike values

I have also tried the following:

df['Date_of_Birth'] = pd.to_datetime(df['Date_of_Birth'], format ='%Y%M%d')

But the error message below is returned: ValueError: time data '(Timestamp('1982-06-10 00:00:00'), '%d%b%Y:%H:%M:%S.%f')' does not match format '%Y%M%d' (match)

I have also tried the following to split the time from the date and then try, but the error message says: AttributeError: Can only use .str accessor with string values!

pd.to_datetime(df['Date_of_Birth'].str.split(':', n=1).str[0])

Python is saying my Date_of_Birth column is an object. I just want Python to return my dates column as how I originally inputted them. I do not want it to change the order of days, months or years or to add a time. Thanks

1

There are 1 best solutions below

1
Samed Mukush On

Change the type to the date time and use the same logic you used before

# Convert the 'Date_of_Birth' column to datetime
df['Date_of_Birth'] = pd.to_datetime(df_combined['Date_of_Birth'])

# Convert the 'Date_of_Birth' column back to string with the original format
df['Date_of_Birth'] = df_combined['Date_of_Birth'].dt.strftime('%m/%d/%Y')