I have a pandas dataframe df_data_raw that has a column with the name "timestemp". This column has timestemp information in the format "2022-05-01 00:15:00+00:00" for every 15 minutes. I would like to convert this time information into the following format "01.05.2022 00:15". Can you tell me how to do this?
You can use
df['timestamp'] = pd.to_datetime(df['timestamp']).dt.strftime('%d.%m.%Y %H:%M')
s = '2022-05-01 00:15:00+00:00'
print(pd.to_datetime(s).strftime('%d.%m.%Y %H:%M'))
01.05.2022 00:15
Use str.strftime with the '%d.%m.%Y %H:%M' format:
df_data_raw['timestemp'] = (pd.to_datetime(df_data_raw['timestemp'])
.dt.strftime('%d.%m.%Y %H:%M')
)
Use datetime
import datetime
timestamp = "2022-05-01 00:15:00+00:00"
dt_obj = datetime.datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S+00:00")
dt_string = dt_obj.strftime("%d.%m.%Y %H:%M:%S")
print(dt_string)
Output
01.05.2022 00:15:00
You can use pd.to_datetime with the appropriate format argument.
pd.to_datetime(df_data_raw['timestemp'], format='%d.%m.%Y %H:%M')
Related
I have a column in Pandas dataframe which is a datetime entry column in string.
I have tried using the the syntax but it gives rise to this error.
Syntax
pd.to_datetime(df['Datetime'], format = '%y-%m-%d %H:%M:%S')
Error
time data '2020-11-01 16:23:12' does not match format '%y-%m-%d %H:%M:%S'
Try %Y,
this is the cheatsheet: https://strftime.org/
Yes, you've used the wrong format for the year.
pd.to_datetime(df["Datetime"], format="%Y-%m-%d %H:%M:%S")
I have two columns Date_x and Date_y. I would like to compare them (i.e Date_x + 1 hour < Date_y)
Format of the strings looks as follows "2020-01-29 11:31:32.754292 UTC"
I have tried converting it using datetime:
from datetime import datetime as dt
df["Date_x"] = [dt.strptime(x, '%Y-%m-%d %H:%M:%S.%f') for x in df['Date_x']]
However, it throws an error regarding the UTC part. I tried removing it with no avail.
Last traceback:
time data '2020-01-29 18:30:28' does not match format '%Y-%m-%d %H:%M:%S.%f'
How would you go about converting the string to hh:mm:ss only?
You could use an if statement:
df["Date_x"] = [dt.strptime(x, '%Y-%m-%d %H:%M:%S.%f') if '.' in x else dt.strptime(x, '%Y-%m-%d %H:%M:%S') for x in df['Date_x']]
But why not just pd.to_datetime:
df["Date_x"] = pd.to_datetime(df["Date_x"], infer_datetime_format=True)
I'm trying to convert strings in my dataset('2016-01-01 00:00:00') to time stamps using pd.to_datetime.
Im trying:
pd.to_datetime(train["timestamp"],format='%Y/%m/%d %I:%M:%S')
but I get
time data '2016-01-01 00:00:00' does not match format '%Y/%m/%d %I:%M:%S' (match)
How can I fix this?
If you want it to be in the specific format that you mentioned, that is %Y/%m/%d %I:%M:%S, then do it like this.
First convert your string to datetime format using to_datetime:
df['timestamp'] = pd.to_datetime(df['timestamp'])
Now that your column is in datetime format, convert to the following format using strftime:
df['timestamp'] = df['timestamp'].dt.strftime('%Y/%m/%d %I:%M:%S')
Output:
timestamp
0 2016/01/01 12:00:00
1 2016/01/01 12:00:00
As others pointed out, use %H instead of %I for 24 hour format, like this:
df['timestamp'] = df['timestamp'].dt.strftime('%Y/%m/%d %H:%M:%S')
That's because your format in your df is different. Try the following using -, also use %H for 24-hour clock:
pd.to_datetime(train["timestamp"],format='%Y-%m-%d %H:%M:%S')
2 issues here:
Use - instead of /
%I is for Hour 00-12, use %H for Hour 00-23
pd.to_datetime(train["timestamp"],format='%Y-%m-%d %H:%M:%S')
I have a dataset containing a column "date":
date item
20.3.2010 17:08 a
20.3.2010 11:16 b
2010-03-20 15:55:14.060 c
2010-03-21 13:56:45.077 d
I would like to convert all values that have format as 20.3.2010 17:08 into 2010-03-21 13:56:45.077.
Does anybody have an idea?
Thank you.
Check on below:
from datetime import datetime
INPUT_FORMAT = '%d.%m.%Y %H:%M'
OUTPUT_FORMAT = '%Y-%m-%d %H:%M:%S.%f'
datetime.strptime('20.3.2010 17:08',INPUT_FORMAT).strftime(OUTPUT_FORMAT)
#Output '2010-03-20 17:08:00.000000'
You could find more information in offcial strptime and strftime.
To do a 100% match with 3 digits microseconds you could use this SO approach.
df['date'] = pd.to_datetime(df['date'], , format = '%Y-%m-%d %H:%M:%S.%f')
You can find more information on pd.to_datetime() here, and the format string type can be found here.
To convert a string date to date format dropping the '00:00:00' I use :
import datetime
strDate = '2017-04-17 00:00:00'
datetime.datetime.strptime(strDate, '%Y/%m/%d %H:%M:%S').strftime('%Y-%m-%d')
Returns :
ValueError: time data '2017-04-17 00:00:00' does not match format '%Y/%m/%d %H:%M:%S'
Is %H:%M:%S not correct format ?
This is the correct way:
datetime.datetime.strptime(strDate, '%Y-%m-%d %H:%M:%S').strftime('%Y-%m-%d')
Notice the - instead of / in strptime. The date is converted to: 2017-04-17.
If you would like to have it displayed a different way, have a look here.