Python Forum
Python date format changes to date & time - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python date format changes to date & time (/thread-41429.html)



Python date format changes to date & time - 1418 - Jan-14-2024

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)



RE: Python date format changes to date & time - deanhystad - Jan-14-2024

There is an option in the ExcelWriter to set the datetime format.

https://pandas.pydata.org/docs/reference/api/pandas.ExcelWriter.html#pandas.ExcelWriter

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

https://sparkbyexamples.com/pandas/pandas-excelwriter-explained-with-examples/


RE: Python date format changes to date & time - 1418 - Jan-15-2024

Hi Deanhystad, thanks for responding.

I will always be modifying data so I assume I will need Openpyxl not xlsxwriter...is that correct, cheers


RE: Python date format changes to date & time - deanhystad - Jan-15-2024

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.


RE: Python date format changes to date & time - 1418 - Jan-20-2024

I obviously need to do a lot more research on what libraries I need, cheers