2

I have the following date (as an object format) : Tue 31 Jan in a pandas Series.

and I try to change it into : 31/01/2019

Please, how can I achieve this ? I understand more or less that pandas.Datetime can convert easily when a string date is clearer (like 6/1/1930 22:00) but not in my case, when their is a weekday name.

Thank you for your help.

cs95
  • 379,657
  • 97
  • 704
  • 746
prog-amateur
  • 121
  • 1
  • 7

3 Answers3

3

Concat the year and callpd.to_datetime with a custom format:

s = pd.Series(['Tue 31 Jan', 'Mon 20 Feb',])
pd.to_datetime(s + ' 2019', format='%a %d %b %Y')

0   2019-01-31
1   2019-02-20
dtype: datetime64[ns]

This is fine as long as all your dates follow this format. If that is not the case, this cannot be solved reliably.

More information on datetime formats at strftime.org.


Another option is using the 3rd party dateutil library:

import dateutil
s.apply(dateutil.parser.parse)

0   2018-01-31
1   2018-02-20
dtype: datetime64[ns]

This can be installed with PyPi.


Another, slower option (but more flexible) is using the 3rd party datefinder library to sniff dates from string containing random text (if this is what you need):

import datefinder
s.apply(lambda x: next(datefinder.find_dates(x)))

0   2018-01-31
1   2018-02-20
dtype: datetime64[ns]

You can install it with PyPi.

cs95
  • 379,657
  • 97
  • 704
  • 746
1

Convert to a datetime object

If you wanted to use the datetime module, you could get the year by doing the following:

import datetime as dt

d = dt.datetime.strptime('Tue 31 Jan', '%a %d %b').replace(year=dt.datetime.now().year)

This is taking the date in your format, but replacing the default year 1900 with the current year in a reliable way.

This is similar to the other answers, but uses the builtin replace method as opposed to concatenating a string.

Output

To get the desired output from your new datetime object, you could perform the following:

>>> d.strftime('%d/%m/%Y')
'31/01/2018'
Adam
  • 709
  • 4
  • 16
0

Here is two alternate ways to achieve the same result.

Method 1: Using datetime module

from datetime import datetime

datetime_object = datetime.strptime('Tue 31 Jan', '%a %d %b')

print(datetime_object) # outputs 1900-01-31 00:00:00

If you had given an Year parameter like Tue 31 Jan 2018, then this code would work.

from datetime import datetime

datetime_object = datetime.strptime('Tue 31 Jan 2018', '%a %d %b %Y')

print(datetime_object) # outputs 2018-01-31 00:00:00

To print the resultant date in a format like this 31/01/2019. You can use

print(datetime_object.strftime("%d/%m/%Y")) # outputs 31/01/2018

Here are all the possible formatting options available with datetime object.

Method 2: Using dateutil.parser

This method automatically fills in the Year parameter with current year.

from dateutil import parser

string = "Tue 31 Jan"

date = parser.parse(string)

print(date) # outputs 2018-01-31 00:00:00
CodeIt
  • 3,492
  • 3
  • 26
  • 37