Change date format with pandas - python

I have a data frame with a column 'object type' called 'Fecha' and I would like to cast it as DateTime type.
However, pandas cannot recognize the format as it is some kind of Spanish format (i.e: '1 abr, 2019'). What should I do to change to a standard format like dd/mm/yyyy?
Abr corresponds to Abril which is April in English.
I have tried with pd.to_datetime() and strftime method but pandas cannot recognize the format.
ValueError: ('Unknown string format:', '1 abr. 2019')
Greetings and thanks in advance

If you set the locale to es_ES you can convert the string series to datetime using pd.to_datetime and the appropriate format codes from here.
In this case the format code is %d %b %Y (well, not exactly since the days are not zero padded, but pd.to_datetime doesn't seem to care).
import locale
locale.setlocale(locale.LC_TIME, locale='es_ES')
df['Fecha'] = pd.to_datetime(df['Fecha'], format='%d %b %Y')

Related

format 01-01-16 7:43 string to datetime

I have the following strings that I'd like to convert to datetime objects:
'01-01-16 7:43'
'01-01-16 3:24'
However, when I try to use strptime it always results in a does not match format error.
Pandas to_datetime function nicely handles the automatic conversion, but I'd like to solve it with the datetime library as well.
format_ = '%m-%d-%Y %H:%M'
my_date = datetime.strptime("01-01-16 4:51", format_)
ValueError: time data '01-01-16 4:51' does not match format '%m-%d-%Y %H:%M'
as i see your date time string '01-01-16 7:43'
its a 2-digit year not 4-digit year
that in order to parse through a 2-digit year, e.g. '16' rather than '2016', a %y is required instead of a %Y.
you can do that like this
from datetime import datetime
datetime_str = '01-01-16 7:43'
datetime_object = datetime.strptime(datetime_str, '%m-%d-%y %H:%M')
print(type(datetime_object))
print(datetime_object)
give you output 2016-01-01 07:43:00
First of all, if you want to match 2016 you should write %Y while for 16 you should write %y.
That means you should write:
format_ = '%m-%d-%y %H:%M'
Check this link for all format codes.

How to custom parse dates in pandas

I know this seems like a duplicated question (and it kinda is), but previous answers didn't let me achieve what I'm looking for. I have a date Series with the following format:
date
Jun 13 14:46
Jun 13 17:11
And so, I wanted to turn it into a datetime object. I did the following:
pd.to_datetime(df.date, format='%b %d %I:%M')
Which based on this question should be enough: Convert custom date formats in pandas
But, I'm still getting ValueError: time data 'Jun 13 14:46' does not match format '%b %d %I:%M' (match)
What am I missing?
Thanks
try pd.to_datetime(df.date, format='%b %d %H:%M')
%I is for a 12-hour clock.
%H is for a 24-hour clock.

Is there a way to convert string to a date with this format (2-Feb-19) in python?

I am trying to convert a string into a date before I can do some arithmetic on the date.
My string has this format 2-FEB-19, I want to keep the same format after converting it to a date. How can I do that?
I tried df['dates'].to_datetime('2-Feb-19', format='%d-%m-%y') and I got an error message "time data '2-Feb-19' does not match format '%d-%m-%y' (match)"
You are using %m, which wants a month as a zero-padded number, such as 02. use %b.
It looks from your question like you are dealing with a single string rather than a Pandas Series or other sequence. For that, you can use strptime() from Python's datetime module in the standard library:
>>> datetime.datetime.strptime('2-FEB-19', '%d-%b-%y')
datetime.datetime(2019, 2, 2, 0, 0)
If you do really want to use Pandas, whether it be on a scalar (string) or sequence, you may want to use the %b format specifier:
>>> pd.to_datetime('2-FEB-19', format='%d-%b-%y')
Timestamp('2019-02-02 00:00:00')
Python's reference on strptime directives is at the datetime docs.
Granted, Pandas (and the underlying date-parsing libraries it uses) are pretty good at inference, and will usually guess things right:
>>> pd.to_datetime('2-FEB-19')
Timestamp('2019-02-02 00:00:00')
You should use the appropriate format:
df['dates'].to_datetime('2-Feb-19', format='%d-%b-%y')
%b "Month as locale’s abbreviated name": Jan, Feb, …, Dec (en_US);
Ref. https://docs.python.org/3/library/datetime.html

How can I convert text to DateTime?

I scraped a website and got the following Output:
2018-06-07T12:22:00+0200
2018-06-07T12:53:00+0200
2018-06-07T13:22:00+0200
Is there a way I can take the first one and convert it into a DateTime value?
Just parse the string into year, month, day, hour and minute integers and then create a new date time object with those variables.
Check out the datetime docs
You can convert string format of datetime to datetime object like this using strptime, here %z is the time zone :
import datetime
dt = "2018-06-07T12:22:00+0200"
ndt = datetime.datetime.strptime(dt, "%Y-%m-%dT%H:%M:%S%z")
# output
2018-06-07 12:22:00+02:00
The following function (not mine) should help you with what you want:
df['date_column'] = pd.to_datetime(df['date_column'], format = '%d/%m/%Y %H:%M').dt.strftime('%Y%V')
You can mess around with the keys next to the % symbols to achieve what you want. You may, however, need to do some light cleaning of your values before you can use them with this function, i.e. replacing 2018-06-07T12:22:00+0200 with 2018-06-07 12:22.
You can use datetime lib.
from datetime import datetime
datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
datetime.strptime documentation
Solution here

OpenERP 6.1 datetime formatting

Why does
o.create_order.strftime("%d %B %Y")
returns nothing when
time.strftime("%d %B %Y")
returns the date "10 february 2013"???
o.create_order is a timestamp according to postgresql.
It contains "30/11/2012 09:38:34" as seen on the openErp sale order - Other information tab.
It is stored as "2012-11-30 08:38:34.272" when querying the database.
So I would expect to see "30 November 2012" but get nothing instead.
Am I misinterpreting the syntax?
I tested this from python 3.3:
>>> d1=datetime.datetime.today()
>>> print(d1.strftime("%d %B %Y"))
10 february 2013
How do I get it to work in OpenOffice Writer?
And by the way how do I get "February" instead of "february"?
Because o.create_order returns a string and not a datetime object, even if, internally, the database column is a timestamp. The OpenERP ORM returns a string in ISO 8601 format.
You need to use the formatLang method which is available in RML reports or create a datetime object using the datetime python module.
Try this:
datetime.strftime('%d %B %Y', o.create_order')
It is because o.create_order returns a string. So first you have to convert your string date into datetime format and then again you can convert it into any format you want as a string.
Try this:
#Converts string into datetime format.
dt1 = datetime.strptime(o.create_order,"%Y-%m-%d %H:%M:%S")
#Converts datetime into your given formate and returns string.
dt2 = datetime.strftime(dt,"%d %B %Y")
Hope this will solve your problem.

Categories