Last answered:

22 Mar 2024

Posted on:

22 Mar 2024


Error in the code

while running the line:

loan_data['mths_since_earliest_cr_line'] = round(pd.to_numeric((pd.to_datetime('2017-12-01') - loan_data['earliest_cr_line_date']) / np.timedelta64(1, 'M')))

showing error:

ValueError: Unit M is not supported. Only unambiguous timedelta values durations are supported. Allowed units are 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns'

but in this 'm' is minutes and Not month. please provide a solution as early as possible

1 answers ( 0 marked as helpful)
Posted on:

22 Mar 2024


Hi Akash!

Thanks for reaching out!

I believe that in its latest versions, NumPy doesn't support 'M' (months).

However, you can convert the timedelta to days and then approximate the number of months by dividing by the average number of days in a month, commonly considered as 30.4375.

You can use the following line:

loan_data['mths_since_earliest_cr_line'] = round(pd.to_numeric((pd.to_datetime('2017-12-01') - loan_data['earliest_cr_line_date']).dt.days) / 30.4375)

This code should lead to identical results without causing an error.

Hope this helps.



Submit an answer