I am trying to run a python script in Power BI, in my database I have these set of abbreviation that I need to convert to their full form in a different column so for that I am writing a python script. It does the job perfectly but what else it does is that it changes the type of the columns which messes up with my date column and then after the script is run it changes the type the date becomes Microsot.ole.db.date
This is my script below:
# Get the table data
table = dataset.copy()
# Define the function to apply the condition
def update_job(job):
# Extract the abbreviation from the job
abbreviation = job.strip()[:4]
# Check if the abbreviation exists in the dictionary, if yes, return the corresponding full form
if abbreviation in abbreviations_to_full_forms:
return abbreviations_to_full_forms[abbreviation]
else:
return job
# Apply the function to the 'Job' column and create a new column 'Full Job'
table['Full Job'] = table['Job'].apply(update_job)
# Function to convert to date with error handling
def convert_to_date(val):
try:
return pd.to_datetime(val).date()
except (ValueError, AttributeError):
return val
# Convert date columns to date
date_columns = ['Actual_Start_Date', 'Misc_Title_3'] # Add your date columns here
for column in date_columns:
table[column] = table[column].apply(convert_to_date)
I tried manually changing the type, but the entire column gets filled up with errors; as you can see in the above code, I did apply some error handling as well, but it still does the same.
I want to prevent it from changing its type after running the python script.