1

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.

1

1 Answer 1

0

Try adjusting your code like below :

import pandas as pd

def convert_to_date(val):
    try:
        return pd.to_datetime(val).normalize()
    except (ValueError, AttributeError):
        return val

date_columns = ['Actual_Start_Date', 'Misc_Title_3']  
for column in date_columns:
    table[column] = table[column].apply(convert_to_date)

I just tried to keep the dates in a format that is more likely to be recognized by Power BI as dates, without converting them to date objects.(I added .normalize() to make sure that the time component is set to midnight, standardizing the datetime objects)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.