Convert string to date

Asked

Viewed 256 times

3

I have a date frame whose column that shows me the date has the format 'nov/19' and it’s an Object, I need to convert it to the date format.

I know that in the documentation of datetime it is said that it reads month 1 to 12, but I wonder if there is any way given to be able to make the conversion to date and the date continue to be displayed as 'nov/19'.

The code I’ve tried to make so far:

str_date = 'nov/19'

date = datetime.strptime(str_date, '%m/%Y').date()

However, I get the error:

ValueError: time data 'nov/19' does not match format '%m/%Y'
  • You want November-2019 as output?

  • You want to convert the nov/19 string to a valid date and continue displaying it as nov/19?

  • 2

    in fact I ended up discovering my mistake by knowing the other references of strftime because %m is for month in number and %Y is for year in YYYY format, I have already been able to find out which references are appropriate for my case, thanks anyway.

  • 2

    Then put your solution in case someone has the doubt and close the flw question

2 answers

2

If you consult the formats described in the documentation, will see that the abbreviated month name should be used %b, and for the year with 2 digits, it is used %y.

But there is another detail: when the year has 2 digits, according to the documentation, values between 0 and 68 are mapped for the years 2000 to 2068, and values between 69 and 99 are mapped for the years 1969 and 1999.

It’s not clear what values you want to work with and what dates you want to generate, but if you want change this rule, an alternative would be:

from datetime import datetime

def year_2to4_digit(two_digit_year, pivotyear = 1950):
    century = (pivotyear // 100) * 100
    if century + two_digit_year > pivotyear:
        return century + two_digit_year
    return century + 100 + two_digit_year

str_date = 'nov/19'
date = datetime.strptime(str_date, '%b/%y').date()
date = date.replace(year = year_2to4_digit(date.year % 100))

That is, values between 51 and 99 would be converted for the years between 1951 and 1999. The other values (0 to 50) would be between 2000 and 2050. If you want another "cut date", just change the parameter pivotyear in function year_2to4_digit. In its specific case ("19"), the API already maps to 2019, which seems to be the desired one. Anyway, I leave the option registered, since years with 2 digits can have this problem and it is important to know how to circumvent it.


Another detail is that for the abbreviated month name, by default is considered to be in English. That is, February will be "Feb", and if you try to do Parsing of the Portuguese version, i.e., "feb", will give error:

str_date = 'fev/19'
date = datetime.strptime(str_date, '%b/%y').date() # ValueError

In your case it is not clear which language is used, since in both English and Portuguese the abbreviation of November is "nov".

But if the idea is to do Parsing of dates in Portuguese, you can use the module locale:

import locale
# setar locale para português
locale.setlocale(locale.LC_ALL, 'pt_BR.utf8')

str_date = 'fev/19'
date = datetime.strptime(str_date, '%b/%y').date()

Recalling that in this case the locale must be installed in the system, as explained in this reply.

0

Correct answer:

date = datetime.datetime.strptime(str_date, '%b/%y').date().strftime('%b-%y')
  • the following link references to different date formats: https://strftime.org/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.