Convert spanish date into a pandas datetime object [duplicate] - python

This question already has answers here:
Parse date and change format from spanish
(2 answers)
Closed 1 year ago.
I want to convert this df from an object to a datetime object, "ABR" is the Spanish for "APR"
Initial date
final date
30 ABR 21
31 MAY 21
30 JUN 21
31 JUL 21
my code:
import locale
locale.setlocale(locale.LC_TIME, '')
a["Fecha Inicial"].apply(lambda x: dt.strptime(x, "%d %b %y"))
ValueError
ValueError: time data '30 ABR 21' does not match format '%d %b %y'

This can solve your issue
import pandas as pd
import datetime as dt
a = pd.DataFrame({"Fecha Inicial": ['30 ABR 21', '30 JUN 21']})
mapping_dict = {'ENERO':'JAN',
'FEB':'FEB',
'MARZO':'MAR',
'ABR':'APR',
'MAYO':'MAY',
'JUN':'JUN',
'JUL':'JUL',
'AGOSTO':'AUG',
'SEPT':'SEP',
'OCT':'OCT',
'NOV':'NOV',
'DIC':'DEC'
}
a["Fecha Inicial"].apply(lambda x: dt.datetime.strptime(x.replace(x.split(' ')[1],mapping_dict[x.split(' ')[1]].capitalize()), "%d %b %y"))
locale has some issue that affects the dt.datetime.strptime.

Related

Convert string to datetime pandas

I have a DataFrame that contains strings which should be converted to datetime in order to sort the DataFrame. The strings are received from Syslogs.
The strings look like as the ones on the picture and below:
date
Mar 16 03:40:24.411
Mar 16 03:40:25.415
Mar 16 03:40:28.532
Mar 16 03:40:30.539
Mar 14 03:20:30.337
Mar 14 03:20:31.340
Mar 14 03:20:37.415
I tried to convert it with pandas.to_datetime(), but I received the following error:
OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-03-16 03:40:24
I may need the nanoseconds as well.
Is necessary specify format of string with this reference.
There is no year, so output year is default:
df['date'] = pd.to_datetime(df['date'], format='%b %d %H:%M:%S.%f')
print (df)
date
0 1900-03-16 03:40:24.411
1 1900-03-16 03:40:25.415
2 1900-03-16 03:40:28.532
3 1900-03-16 03:40:30.539
4 1900-03-14 03:20:30.337
5 1900-03-14 03:20:31.340
6 1900-03-14 03:20:37.415
You can add some year to column and then parse it like:
df['date'] = pd.to_datetime('2020 ' + df['date'], format='%Y %b %d %H:%M:%S.%f')
print (df)
date
0 2020-03-16 03:40:24.411
1 2020-03-16 03:40:25.415
2 2020-03-16 03:40:28.532
3 2020-03-16 03:40:30.539
4 2020-03-14 03:20:30.337
5 2020-03-14 03:20:31.340
6 2020-03-14 03:20:37.415
The best way is using pandas.to_datetime as mentioned above. If you are not familiar with date string formatting, you can getaway using date parser libraries. Example dateutil library:
# python -m pip install —user dateutil
from dateutil import parser
import pandas as pd
df = pd.DataFrame({'dates': ['Mar 16 03:40:24.411',' Mar 16 03:40:25.415','Mar 16 03:40:28.532']})
# parse it
df['dates'] = df['dates'].apply(parser.parse)
print(df)
dateutil parser will add current year to your dates.
vectoring
# using numpy.vectorize
import numpy as np
df['dates'] = np.vectorize(parser.parse)(df['dates'])
Note:
This is not optional for large datasets and should be used only when pd.to_datetime is not able to parse date.

How to extract date from string in Python [duplicate]

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

converting GMT to stftime using python [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 4 years ago.
I want to convert Wed, 14 Mar 2018 07:30:00 GMT to 2018/03/14 in python. I am getting unicode object has no attribute strftime error. Someone help me.
Use:
from datetime import datetime
date_given = 'Wed, 14 Mar 2018 07:30:00 GMT'
datetime.strptime(date_given, '%a, %d %b %Y %H:%M:%S %Z').strftime('%Y/%m/%d')
You can read up below links for more details:
Python documentation for strptime: Python 2, Python 3
Python documentation for strftime: Python 2, Python 3

Python convert seconds to datetime date and time [duplicate]

This question already has answers here:
How to convert integer timestamp into a datetime
(3 answers)
Closed 5 years ago.
How do I convert an int like 1485714600 such that my result ends up being Monday, January 30, 2017 12:00:00 AM?
I've tried using datetime.datetime but it gives me results like '5 days, 13:23:07'
Like this?
>>> from datetime import datetime
>>> datetime.fromtimestamp(1485714600).strftime("%A, %B %d, %Y %I:%M:%S")
'Sunday, January 29, 2017 08:30:00'
What you describe here is a (Unix) timestamp (the number of seconds since January 1st, 1970). You can use:
datetime.datetime.fromtimestamp(1485714600)
This will generate:
>>> import datetime
>>> datetime.datetime.fromtimestamp(1485714600)
datetime.datetime(2017, 1, 29, 19, 30)
You can get the name of the day by using .strftime('%A'):
>>> datetime.datetime.fromtimestamp(1485714600).strftime('%A')
'Sunday'
Or you can call weekday() to obtain an integers between 0 and 6 (both inclusive) that maps thus from monday to sunday:
>>> datetime.datetime.fromtimestamp(1485714600).weekday()
6

Python string to date [duplicate]

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.

Categories

Resources