Python datetime formatting for yyyy-mm - python

Im have a csv file with date data which is formatted as 'yyyy-mm-dd' or 'yyyy-mm'.
When i import the csv file and convert to a dataframe, the datatype of my date column is object.
Im trying to convert this to datetime, but whenever i do, the dates which are formatted 'yyyy-mm' get converted to format 'yyyy-mm-dd'.
How can i convert the data from object to date but preserve the 'yyyy-mm' format?
Ive tried the methods 'pd.to_datetime', 'strptime' and 'strftime' which all result in my data changing to 'yyyy-mm-dd' format.
Thanks for your help

Related

What's the correct datetime format for this string date generated by python?

I have this date example '2022-08-30T11:53:52.204219' stored in database, when I get it from database it's type is string so I wanted to convert it to a date type by using this python code
datetime.strptime('2022-08-30T11:53:52.204219', "%Y-%m-%d'T'%H:%M:%S.%f")
I also tried this one
datetime.strptime('2022-08-30T11:53:52.204219', "yyyy-MM-dd'T'HH:mm:ssZ")
But I always get this error response 'time data '2022-08-30T11:53:52.204219' does not match format "%Y-%m-%d'T'%H:%M:%S.%f'
I need help to convert this string date to an actual date
As per comment:
from datetime import datetime
print(datetime.strptime('2022-08-30T11:53:52.204219', "%Y-%m-%dT%H:%M:%S.%f"))
Result:
2022-08-30 11:53:52.204219

python json load not able parse date format

i am trying to read json file with json load it throws error with date field (22-04-2000).for other fields which are strings i dont see any issue.seems there is parse issue with date format can someone help me on this?.i have tried placing that in String format too but still no luck.
There is no standard JSON representation of dates.
dates are stored in json in string format.
to push a date value to json object, first convert it to string.
var date = (new Date()).toString();
jsondata.push({"date": date});
to get date value back from json get the element then use
var date = new Date(jsondata.date);

Date format in pandas

I am changing date format using below code, however when I extracting this dataframe in excel, this date is appearing in text format (not date format).
new_data['Expiry']=new_data['Expiry'].dt.strftime('%d-%b-%Y')
How can I change my code so that my excel should also have date format?
PS: I don't want datetime format, need only date.
new_data['Expiry']=pd.to_datetime(new_data['Expiry'], format = '%d-%m-%Y')
new_data['Expiry']=new_data['Expiry'].dt.date

Format Date-time column without losing the Date-time data-type in Python

I am trying to format the date-time column to the given format. The only solution I came across in Python is via using 'strftime' which convert the date-time to unicode.
original = df.date
required_date_format ='%d/%m/%Y'
formatted = df.date.dt.strftime(required_date_format)
converted_after_formatting = pd.to_datetime(\
df.date.dt.strftime(required_date_format),\
format=required_date_format)
The outputs of the above code snippet are as follows:
I want a solution that can format the date-time column without loosing it's data-type.

Changing date formats with python from one to another

I have a series of tables that I am using to create a map in ArcGIS desktop. In the attribute table there is a date column in the format "19950129000000" and I would like to convert this format to something more meaningful such as "29/1/1995". The column says it is in a string format, but the metadata says it is in a date format.
I have done something similar before but I am having trouble getting it to work.
I've tried:
def dtConversion(date):
from datetime import datetime
od = datetime.strptime(date, "YYYYMMddhhmmss")
nd = datetime.strftime(od, "%d/%m/%Y")
return nd
esri_field_calculator_splitter
dtConversion(!CMPLDT!)
datetime.strptime('19950129000000', "%Y%m%d%H%M%S")

Categories