I have a string column in df which contains date in dd/MM/yyyy format and I want to convert that format to yyyy-MM-dd using with column
If you know you will have a consistent format in your column, you can pass this to 'to_datetime'.
You can try like below-
df['column_name'] = pd.to_datetime(df['column_name'], format='%d/%m/%y').dt.strftime('%Y-%m-%d')
Using python's datetime lib it can be done accordingly:
from datetime import datetime
date_string = '10/10/2000'
datetime_object = datetime.strptime(date_string, '%d/%m/%Y')
converted_date_string = datetime_object.strftime('%Y-%m-%d')
Related
I have the following string 20211208_104755, representing date_time format. I want to convert it to the python datetime format using datetime.strip() method.
mydatetime = "20211208_104755"
datetime_object = datetime.strptime(mydatetime, '%y/%m/%d')
However I am getting the following error.
ValueError: time data '20211208' does not match format '%y/%m/%d'
The second argument in strptime has to match the pattern of your datetime string. You can find the patterns and their meaning on https://docs.python.org/3/library/datetime.html
In your case, you can format it as
from datetime import datetime
mydatetime = "20211208_104755"
datetime_object = datetime.strptime(mydatetime, '%Y%m%d_%H%M%S')
print(datetime_object)
>>> 2021-12-08 10:47:55
what should work is to define how the mydatetime string is composed.
example:
%Y is the year (4 digits); check here for format (section strftime() Date Format Codes)
So in your example I would assume it's like this:
mydatetime = "20211208_104755"
datetime_object = datetime.strptime(mydatetime, '%Y%m%d_%H%M%S')
print (datetime_object)
result
2021-12-08 10:47:55
and
type(datetime_object)
datetime.datetime
I have a date it look like this
2021-12-14T20:32:34Z
how can i convert it to someting like this
2021-12-14 20:32
If you want to do this using Pandas, you can use pandas to convert iso date to datetime object then strftime to convert timestamp into string format
import pandas as pd
import datetime
iso_date = '2021-12-14T20:32:34Z'
fmt = '%Y-%m-%d %H:%M'
pd.to_datetime(iso_date).strftime(fmt)
to apply it to a series of dates of DataFrame column you can replace iso_date with the series of dates and use this code
pd.to_datetime(iso_date).dt.strftime(fmt)
How to convert a date format (YYYY-MM-DD) to (YYYY,MM,DD) in Python
OR, I have a date = 2020-06-11 which I want to convert into seconds.
Works: d2 = (dt.datetime(2020,6,30) - t0).total_seconds()
Fails: d2 = (dt.datetime(2020-6-30) - t0).total_seconds()
How do I do it ?
If you need to cast a string to a datetime object, you can use strptime for this.
from datetime import datetime
t = datetime.strptime('2020-06-11', '%Y,%m,%d')
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
I am extracting Data from Mongodb using some date filter. In mongo my date is in ISO format . As i am dynamically adding date from some variable which is in timestamp format(2019-07-15 14:54:53).Getting Empty Result
curs = col1.aggregate([{'$match':{update_col: {'$gte': last_updt }}},{'$project':json_acceptable_string}])
I am expecting Rows after filtering but acual its giving empty dataset
you can use datetime.strptime to parse the original string to a datetime object, then use datetime.isoformat to get it in ISO format.
try this:
import datetime
original_date = '2019-07-15 14:54:53'
date_obj = datetime.datetime.strptime(original_date, "%Y-%m-%d %H:%M:%S")
iso_date = date_obj.isoformat()
print(iso_date)
try this
from dateutil import parser as date_parser
dt_obj = date_parser.parse('2019-07-15 14:54:53')
where dt_obj is an object of standard datetime.datetime class
You can use fromisoformat.
Try
from datetime import datetime
iso_string = '2019-07-15 14:54:53'
you_date_obj = datetime.fromisoformat(iso_string)
I have a data frame that some of the columns have dates in this format (iso format):
YYYY-MM-DDThh:mm:ssTZD
I want to convert it to
YYYY-MM-DD HH:MM[:SS[.SSSSSS]]
For example when I do:
print (df["create_date"])
I get:
2014-11-24 20:21:49-05:00
How can I alter the date in the column ?
You need to do this:
from datetime import datetime
df["new_date"] = df["create_date"].strftime("%Y-%m-%d %H:%M[:%S[.%f]]")
If the column is type string, the try:
df["new_date"] = df["create_date"].dt.strftime("%Y-%m-%d %H:%M[:%S[.%f]]")
Then write this to csv/excel
import pandas as pd
df.to_csv("\\path\\file.csv")