The
CodePudding user response:
One possible solution is applying a custom function to convert the date string into your desire format.
You can try this:
import pandas as pd
import datetime as dt
data = {'Col1': ['a', 'b', 'c'],
'Col2': ['101-200', 'Aug-1', 'Sep-8']}
goal = {'Col1': ['a', 'b', 'c'],
'Col2': ['101-200', '1-8', '8-9']}
def convert_date_str(date_str):
try:
date_val = dt.datetime.strptime(date_str, '%b-%d')
return date_val.strftime('%-d-%-m')
except:
return date_str
current_df = pd.DataFrame(data)
goal_df = pd.DataFrame(goal)
current_df['Col2'] = current_df['Col2'].apply(convert_date_str)
print(current_df)
compare_flag = goal_df.equals(current_df)
print(f'goal_df == current_df: {compare_flag}')
Output:
Col1 Col2
0 a 101-200
1 b 1-8
2 c 8-9
goal_df == current_df: True