This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 3 years ago.
I have strings in following format:
Friday January 3 2020 16:40:57
Thursday January 2 2020 19:26:19
Sunday January 5 2020 01:24:55
Tuesday December 31 2019 17:31:42
What is the best way to convert them into python date and time?
You can use datetime.strptime:
from datetime import datetime
d = "Friday January 3 2020 16:40:57"
datetime_object = datetime.strptime(d, '%A %B %d %Y %H:%M:%S')
print(datetime_object)
You can use dateparser
Install:
$ pip install dateparser
Sample Code:
import dateparser
t1 = 'Friday January 3 2020 16:40:57'
t2 = 'Thursday January 2 2020 19:26:19'
t3 = 'Sunday January 5 2020 01:24:55'
t4 = 'Tuesday December 31 2019 17:31:42'
dt1 = dateparser.parse(t1)
dt2 = dateparser.parse(t2)
dt3 = dateparser.parse(t3)
dt4 = dateparser.parse(t4)
for dt in [dt1, dt2, dt3, dt4]:
print(dt)
Output:
2020-01-03 16:40:57
2020-01-02 19:26:19
2020-01-05 01:24:55
2019-12-31 17:31:42
Related
I would like to convert dates (Before) within a column (After) in date format:
Before After
23 Ottobre 2020 2020-10-23
24 Ottobre 2020 2020-10-24
27 Ottobre 2020 2020-10-27
30 Ottobre 2020 2020-10-30
22 Luglio 2020 2020-07-22
I tried as follows:
from datetime import datetime
date = df.Before.tolist()
dtObject = datetime.strptime(date,"%d %m, %y")
dtConverted = dtObject.strftime("%y-%m-%d")
But it does not work.
Can you explain me how to do it?
Similar to this question, you can set the locale to Italian before parsing:
import pandas as pd
import locale
locale.setlocale(locale.LC_ALL, 'it_IT')
df = pd.DataFrame({'Before': ['30 Ottobre 2020', '22 Luglio 2020']})
df['After'] = pd.to_datetime(df['Before'], format='%d %B %Y')
# df
# Before After
# 0 30 Ottobre 2020 2020-10-30
# 1 22 Luglio 2020 2020-07-22
If you want the "After" column as dtype string, use df['After'].dt.strftime('%Y-%m-%d').
I Have a df having 2 columns *Total Idle Time and Month as below:
Total Idle Time Month
0 0:00:00 December
1 0:02:24 December
2 26:00:00 December
3 0:53:05 December
4 28:03:39 December
Here the Total Idle Time column is of string format, but I want to convert it into time format as I want to add the total idle time in the month of December.
I tried converting the column to datetime as below:
data['Total Idle Time '] = pd.to_datetime(data['Total Idle Time '], format='%H:%M:%S')
However, I got an error as follow:
time data '28:03:39' does not match format '%H:%M:%S' (match)
I thought of converting the column to int and adding them up based on the hours and minutes, but I am not successful in doing so. Is there any way to do this thing?
You could try using pd.to_timedelta() instead here:
>>> df['Idle Time'] = pd.to_timedelta(df["Idle Time"])
>>> df
Total Idle_Time Month
0 0 0 days 00:00:00 December
1 1 0 days 00:02:24 December
2 2 1 days 02:00:00 December
3 3 0 days 00:53:05 December
4 4 1 days 04:03:39 December
You can use this to convert to numeric if you want, by scaling the results of .total_seconds():
# in hours
>>> df['Idle Time'] = df['Idle Time'].dt.total_seconds() / 3600
>>> df
Total Idle_Time Month
0 0 0.000000 December
1 1 0.040000 December
2 2 26.000000 December
3 3 0.884722 December
4 4 28.060833 December
I am trying to convert a datetime string (German) that comes from MS Project Excel Export.
02 Februar 2022 17:00
I read it from a Excel-Export of MS Project in to a pandas dataframe.
When converting it with
to_datetime(df["Anfang"], format= '%d %B %Y %H:%M').dt.date
but get the error
ValueError: time data '07 Januar 2019 07:00' does not match format '%d %B %Y %H:%M' (match)
from https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
%B Month as locale’s full name. September
What I am doing wrong here?
Do I have to check some local settings?
I am using German(Swiss)
import locale
locale.getdefaultlocale()
('de_CH', 'cp1252')
df in:
0 10 April 2019 08:00
1 07 Januar 2019 07:00
2 07 Januar 2019 07:00
3 07 Januar 2019 07:00
4 09 Oktober 2019 17:00
5 04 Dezember 2020 17:00
Name: Anfang, dtype: object
df out (wanted):
0 10-04-2019
1 07-01-2019
.
.
EDIT:
I changed my locale to ('de_DE', 'cp1252'), but I get the same error.
SOLVED:
By using matJ's answer, I got the error that "Die 15.06.21" was not matching the format, which led me to investigate the data. There I found two different date formats (Thanks, Microsoft!). After cleaning, the above code worked well!!!
So the error message of to_datetime wasn't precise as datetime.strptime.
Thanks for helping.
Johannes
One possible solution is use dateparser module:
import dateparser
df['Anfang'] = df['Anfang'].apply(dateparser.parse)
print (df)
Anfang
0 2019-04-10 08:00:00
1 2019-01-07 07:00:00
2 2019-01-07 07:00:00
3 2019-01-07 07:00:00
4 2019-10-09 17:00:00
5 2020-12-04 17:00:00
import dateparser
df['Anfang'] = df['Anfang'].apply(dateparser.parse).dt.date
print (df)
Anfang
0 2019-04-10
1 2019-01-07
2 2019-01-07
3 2019-01-07
4 2019-10-09
5 2020-12-04
I'd change the locale in a different way. Then your code should work.
The following works for me:
import locale
from datetime import datetime
locale.setlocale(locale.LC_ALL, 'de_DE') # changing locale to german
datetime.strptime('07 Januar 2019 07:00', '%d %B %Y %H:%M') # returns a datetime obj which you can format as you like
Let me know if that works for you as well.
I used to use year and week number to convert to date for some propose.
it works well before 2019, but when i tried to import 2019 wk1 data, it wired.
2019 wk1 becomes between 2019-01-07 ~ 2019-01-03
But on the contrary, if i use date to convert to year and wk, it's correct.
May I know what's wrong with my code? thanks
d = {'year': [2018, 2018, 2018, 2019, 2019, 2019], 'week': [0, 1, 52, 0, 1, 2]}
df = pd.DataFrame(data=d)
df['date'] = pd.to_datetime(df['year'].map(str) + df['week'].map(str) + '-4', format='%Y%W-%w')
df['yearwk'] = pd.to_datetime(df['date'], format='%Y-%M-%D').dt.strftime('%YW%V')
print(df)
year week date yearwk
0 2018 0 2018-01-04 2018W01
1 2018 1 2018-01-04 2018W01
2 2018 52 2018-12-27 2018W52
3 2019 0 2019-01-03 2019W01
4 2019 1 2019-01-10 2019W02
5 2019 2 2019-01-17 2019W03
I use given year and weeknum to convert to date. ideally, 2019WK1 should be between 2018-12-31 to 2019-01-05, but it became 2019-01-06 to 2019-01-13.
then I use that date to convert to year and wk, the result is what's I expected.
According to strftime() and strptime() Behavior,
%W: Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.
%w: Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.
You put 4 as %w, indicating you want date of Thursday of the week number you provide.
First week (%W=1) of 2018 that starts with Monday is: Jan 1 - Jan 7 or Jan 4 (Thursday)
First week (%W=1) of 2019 that starts with Monday is: Jan 7 - Jan 13 or Jan 10 (Thursday)
For 2018, there's no Thursday the week before, so %W=0 will output the same as %W=1. However, for 2019, there is a Thursday the week before, so %W=0 will output Jan 3.
This question already has answers here:
Python date string to date object
(9 answers)
Closed 5 years ago.
I'm a Python newbie and don't how to convert a Python 3.5x string
'2017-04-19 00:23'
into a date and time like
April 19, 2017 12:23 am
and even get individual units like
April
19
2017
12:23 am
or get day of week for 4/19/217
Wednesday
Use python datetime module, something like this :
from datetime import datetime
date_str = '2017-04-19 00:23'
date_obj = datetime.strptime(date_str, '%Y-%m-%d %H:%M')
# To get a particular part of the date in a particular format such as "Wednesday" for the "Datetime Object"
print(date_obj.strftime('%A'))
print(date_obj.strftime('%c'))
This will result in :
Wednesday
Wed Apr 19 00:23:00 2017
Check out the documentation.