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
Change the type to the date time and use the same logic you used before