Search code examples
pythonpandasdatetimesubstring

how to convert panda date time to particular date format and then extract substring out of it


I have this date time column in panda. I am converting it to specific date format. i want to convert this date into string and extract the substring out of it. I am finding it hard to do it. does anyone know how.

data in my datetime column looks like follows:

2024-01-30 13:45:00
2024-01-30 14:45:00
2024-01-30 15:45:00

I have converted it to date column using following statement:

da['date1'] = da['timestamp'].dt.strftime("%y%b%d")

Now my data looks like following:

24Jan30
24Jan30
24Jan30

Note this is in date format.

I want to extract year and month and from it. for that i have to convert it into string which is not giving me expected results.

expected output:

24Jan
24Jan
24Jan

Any help would be greatly appreciated. Thanks in advance.


Solution

  • You can just remove the %d (day of the month) at the end of your statement, like this:

    da['date1'] = da['timestamp'].dt.strftime("%y%b")

    %y: year without century

    %b: 3-letter abbreviation of month name


    If you want to extract the substring out of the date, you can use the following. It matches a sequence of 2 digits (corresponding to %y) followed by exactly 3 letters (corresponding to %b):

    df['year_month'] = df['date1'].apply(lambda x: re.match(r'\d{2}[A-Za-z]{3}', x).group())