performing date comparison with creation_date in user settings
![jrmathieu63](https://us.v-cdn.net/6038231/uploads/Dataiku/nAvatar4.png)
I have a python recipe that is trapping all our users creation_date and would like to compare this date to a string date defined as following:
settings = user.get_settings()
creation_date = settings.creation_date
before_date = date.today() + relativedelta(months=-3)
if creation_date < before_date:
I know this will not work since they are not the same type but I cannot convert creation_date to string date since there none values.
Anybody know if this variable can be converted to string?
Thanks
Operating system used: AL2
Best Answer
-
Was able to find the solution myself by using chatGPT.
Best way to handle missing dates / null/none (nan) values is to set a default date.
Not sure why this is not done by Dataiku when creating a new administrative field for reporting.
Example:
last_login_time = user.activity['lastSuccessfulLogin']
converted_last_login = date.fromtimestamp(last_login_time/1e3)
creation_date = settings.creation_date
if creation_date is not None:
creation_date_str = creation_date.strftime('%Y-%m-%d')
else:
creation_date_str = default_date.strftime('%Y-%m-%d')
creation_date = datetime.strptime(creation_date_str, '%Y-%m-%d').date()
This allows me to conditional search the user list for accounts that have not logged on for at least 6 months or never logged on and account defined for at least 3 months.pr6mths = date.today() + relativedelta(months=-6)
pr3mths = date.today() + relativedelta(months=-3)
#if last_login_time == 0 and enabled == True and converted_last_login < pr6mths and np.isnat(np.datetime64(creation_date)):
if last_login_time == 0 and enabled == True and converted_last_login < pr6mths and creation_date < pr3mths:
Enterprise system must review account access since our security is very concern with access that is not being used.
Access must be verified/recertified if not in use.
Answers
-
Here is more details on what I have attempted to do.
#Trap Last Login Datetime
df = []
for user in activity:
login = user.activity['login']
last_login_time = user.activity['lastSuccessfulLogin']
converted_last_login = date.fromtimestamp(last_login_time/1e3)
user = client.get_user(login)
settings = user.get_settings()
displayName = settings.get_raw()["displayName"]
email = settings.get_raw()["email"]
enabled = settings.enabled
creation_date = settings.creation_date
str_date = creation_date.strftime("%Y-%m-%d")The attempt to convert creation_date into a string using strtime just fails with the following error.
Job failed: Error in Python process: At line 31: AttributeError: 'NoneType' object has no attribute 'strftime'
Not sure what this error means and what is required to resolve this?