Home > Software design >  Python Date Time - Create New Column if Past Date change to Current Date MMM-YY
Python Date Time - Create New Column if Past Date change to Current Date MMM-YY

Time:08-08

need your help on creating new column.

  1. create new column
  2. if Date < Current Month and Year = change to value to Current Month and Year (MMM-YY)
  3. if Date >= Current Month and Year = no change

here is an example, all red are in the past hence in the new column the new date is this month (Aug-22)

I tried below but no luck:

string_input_with_date = df["Old Column"]
past = datetime.strptime(string_input_with_date, "%b-%y")
present = datetime.now()
past.date() < present.date()

[enter image description here]

1

CodePudding user response:

Try this:

import pandas as pd
import datetime

df = pd.DataFrame({'Old Column': ['Jan-20', 'Feb-20', 'Jan-20', 'Feb-21', 'Dec-23', 'Aug-22', 'Mar-22', 'Nov-22', 'Oct-22']})
df['tmp'] = '01-'   df['Old Column']
df['tmp'] = pd.to_datetime(df['tmp'], format="%d-%b-%y", errors='coerce')
present = datetime.datetime.now()
condition = df['tmp'].dt.date < datetime.date(present.year, present.month, 1)
df['New Column'] = df['Old Column']
df.loc[condition, 'New Column'] = present.strftime('%b-%y')
df['New Column']
  1. I added a tmp column, which is later transformed to_datetime
  2. And I used df.loc to replace all rows corresponding to the condition with present.strftime('%b-%y')

This is the output

0    Aug-22
1    Aug-22
2    Aug-22
3    Aug-22
4    Dec-23
5    Aug-22
6    Aug-22
7    Nov-22
8    Oct-22
Name: New Column, dtype: object
  • Related