Home > Back-end >  How Can I Turn A MonthYear String Into A Datetime Pandas?
How Can I Turn A MonthYear String Into A Datetime Pandas?

Time:10-27

I currently have a string in the format of month year. The month is 3 characters long and the year is 4. Below are some examples of some of the dates that I have.

apr2021
jun2021
mar2020
dec2020

I have a date in a pandas dataframe that is in the form of yyyy-mm-dd hh:mm:ss. I want to compare this string to the date in pandas and determine if it is less than or equal to it? Is there a way to turn this month-year string into an actual date format?

I'm going to strip the date to yyyy-mm-dd and then try to get the month-year string in the same format and then do a direct comparison. I'm not quite sure if there is a more efficient way to do this though?

Example pseudo-code below:

temp_date = apr2021 -> 01-04-2021
pandas_date = 29-03-2021

if temp_date < pandas_date:
    do x
else:
    do y

CodePudding user response:

You can use pandas.to_datetime to convert your dates, both in your dataframe and for an isolated string. Then you will be able to perform comparisons using >/< or the pandas methods gt/lt/…:

import pandas as pd
df = pd.DataFrame({'temp_date': ['apr2021', 'jun2021', 'mar2020', 'dec2020']})
df['temp_date'] = pd.to_datetime(df['temp_date'])

pandas_date = pd.to_datetime('29-03-2021') # optional

import numpy as np

df['new_col'] = np.where(df['temp_date'].gt(pandas_date), 'greater', 'lower')

output:

   temp_date  new_col
0 2021-04-01  greater
1 2021-06-01  greater
2 2020-03-01    lower
3 2020-12-01    lower

NB. following up on the comment, there is a full list of datetime formats in datetime's documentation, in your case this would be %b%Y: pd.to_datetime('apr2021', format='%b%Y')

CodePudding user response:

pd.to_datetime is what you're looking for.

>>> import pandas as pd
>>> pd.to_datetime("apr2021")
Timestamp('2021-04-01 00:00:00')
  • Related