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)
Related
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')
I have a date column in my dataframe that consists of strings like this...'201512'
I would like to convert it into a datetime object of just year to do some time series analysis.
I tried...
df['Date']= pd.to_datetime(df['Date'])
and something similar to
datetime.strptime(Date, "%Y")
I am not sure how datetime interfaces with pandas dataframes (perhaps somebody will comment if there is special usage), but in general the datetime functions would work like this:
import datetime
date_string = "201512"
date_object = datetime.datetime.strptime(date_string, "%Y%m")
print(date_object)
Getting us:
2015-12-01 00:00:00
Now that the hard part of creating a datetime object is done we simply
print(date_object.year)
Which spits out our desired
2015
More info about the parsing operators (the "%Y%m" bit of my code) is described in the documentation
I would look at the module arrow
https://arrow.readthedocs.io/en/latest/
import arrow
date = arrow.now()
#example of text formatting
fdate = date.format('YYYY')
#example of converting text into datetime
date = arrow.get('201905', 'YYYYMM').datetime
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")
I have a timeserie containing dates in format dd/mm/yy and datetime in format dd/mm/yy hh:MM.
I am using pd.to_datetime to convert them to proper datetime format, which works fine. However, I would like to convert the datapoints in format dd/mm/yy to dd/mm/yy 00:00 or even better, dd/mm/yy 08:00.
How can I do that?
from datetime import date
from datetime import datetime
datetime.today().strftime('%Y-%m-%d') + " 8:00"
Output:
'2017-01-27 8:00'
from datetime import datetime as dt
from datetime import date
clv['Date']=datetime.strptime(clv['Date'], "%m/%d/%Y").date()
You could use pd.to_datetime. After that you will convert your date string to Series of datetime64 dtype. Then you could access to date with dt.date:
clv['Date']=pd.to_datetime(clv['Date'], format="%m/%d/%Y").dt.date