So I have excel file with data where some headers are text and over are dates like 19.07.2023 etc:
Item/Date 19.07.2023 20.07.2023 21.07.2023
Some Item 58 51 57
When I importing this file to pandas it's looks like this:
After that I just do some merging with over excel file to get new column with latest date and exporting back to excel and my date of 19.07.2023 is 2023-07-19 0:00:00 now. How to avoid adding not needed time and also avoid changing format to d-m-Y? A can specify in excel after that this is Date and format will be changet to corrct one, but I want to avoid additional steps in the process.
Code example
import pandas as pd
test_df = pd.read_excel('file.xlsx')
test2_df = pd.read_excel('file2.xlsb')
test_total = test_df.merge(test2_df, how='outer').fillna(0)
test_total.to_excel('file_total.xlsx', index = False)
If you want just the dates and do not want time in YYYY-MM-DD
format :
df['Your_Column'] = pd.to_datetime(df['Your_Column']).dt.date
This will just give you dates. But, the datatype will be 'object'
.
as @D.L stated this should also work:
df['Your_Column'] = df['Your_Column'].dt.strftime('%Y-%m-%d')
But, the datatype will be 'object'
.
df['Your_Column'] = pd.to_datetime(df['Your_Column']).dt.normalize()
can change the datatype of your column to 'datetime64'