Python Forum

Full Version: Python date format changes to date & time
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, my original data is short date (15/10/2023) but when I run the below code python changes it to 2023-10-15 00:00:00, how can I prevent this? Cheers.
import pandas as pd 

inspdata=pd.read_excel('Inspections data.xlsx')

#remove columns 
inspdata.drop(['Notice Creation','count()'],axis=1, inplace=True)

#replace header row names
inspdata.columns=['EForm ID','Contractor','Address','Area','Classification','Program','Notice Ref #','Category','Inspection Type',
'Result','Actual Date','Created Date','Status','CIO','PO']

#delete first 4 rows below the header
inspdata.drop([0,1,2,3],axis=0,inplace=True) #keeps header row

#replace text
inspdata['Classification'].replace(['VACQREP','VACAP'],['VAC','VAC'],inplace=True)

print(inspdata.head(10))

inspdata.to_excel('Inspections Data Python.xlsx',index=False)
There is an option in the ExcelWriter to set the datetime format.

https://pandas.pydata.org/docs/reference...xcelWriter

From your other posts it looks like you will become an expert ExcelWriter user. I found this a nice intro.

https://sparkbyexamples.com/pandas/panda...-examples/
Hi Deanhystad, thanks for responding.

I will always be modifying data so I assume I will need Openpyxl not xlsxwriter...is that correct, cheers
Are you referring to this: "By default, it uses xlsxwriter if it is installed otherwise it uses openpyxl"

I think either will work fine. You'll modify the data in pandas. You only use xlsxwriter or openpyxl to write the pandas dataframe to an excel file.
I obviously need to do a lot more research on what libraries I need, cheers