Checking value of a field while creating a Pandas data frame & populating it differently, based on type(value)

Question:

Good day. I’m using JIRA APIs to get data from JIRA about stories and put it in a dataframe/Excel. There is one particular field "issue.fields.aggregatetimeoriginalestimate" – which can have a "None" type or a "float" value in seconds. Is there a way to dynamically check for this and populate the appropriate value in the Pandas dataframe, using code while the population is going on in a for loop?

Here’s what I’m trying to achieve:

jira_issues = jira.search_issues(jql,maxResults=0)
    
# JSON to pandas DataFrame
issues = pd.DataFrame()

for issue in jira_issues_ncr:
    d = {
        'Self':  issue.self,
        'Project':         str(issue.fields.project),
        'JIRA ID':         issue.key,
        'Summary':         str(issue.fields.summary),
        'Original Story Points': str(issue.fields.customfield_15972),
        'Story Points':    str(issue.fields.customfield_10010),
        'Aggregate Orig Estimate (Hrs)':    {
                                                if type(issue.fields.aggregatetimeoriginalestimate) != None):
                                                 issue.fields.aggregatetimeoriginalestimate/(60.0*60.0)
                                                else:
                                                    str(issue.fields.aggregatetimeoriginalestimate)
                                            },
        'Original Estimate':     str(issue.fields.timeoriginalestimate),
        'Remaining Estimate':    str(issue.fields.timeestimate),
        'Priority':        str(issue.fields.priority.name),
  #      'Severity':        str(issue.fields.customfield_10120),
        'Resolution':      str(issue.fields.resolution),
        'Status':          str(issue.fields.status.name),
        'Assignee':        str(issue.fields.assignee),
        'Creator' :        str(issue.fields.creator),
        'Reporter':        str(issue.fields.reporter),
        'Created' :        str(issue.fields.created),   
  #      'Found by':        str(issue.fields.customfield_11272),
  #      'Root cause':      str(issue.fields.customfield_10031),
  #      'Earliest place to find':        str(issue.fields.customfield_11380),
  #      'Test Escape Classification':        str(issue.fields.customfield_11387),
        'Labels':          str(issue.fields.labels),
        'Components':      str(issue.fields.components),
  #   'Description':     str(issue.fields.description),
  #      'FixVersions':     str(issue.fields.fixVersions),
        'Issuetype':       str(issue.fields.issuetype.name),
  #      'Resolution_date': str(issue.fields.resolutiondate),
        'Updated':         str(issue.fields.updated),
  #      'Versions':        str(issue.fields.versions),
  #   'Status_name':     str(issue.fields.status.name),
  #      'Watchcount':      str(issue.fields.watches.watchCount),
    }
    issues = issues.append(d, ignore_index=True)    

Please let me know how this can be achieved inside the for loop, such that:
if the value of the field is not "None", I want to do a calculation (value/(60.0*60.0) and then populate the field "Aggregate Orig Time Estimate (Hrs)" or if it is type "None", then just put the value as is "None" in the data frame? (I guess we could also put a 0.0, if None is found).

I’m a novice in Python so will appreciate any assistance.

When I tried to run this, I get:

    d = {
        ^
SyntaxError: '{' was never closed
Asked By: Nish

||

Answers:

The curly brackets { and } are used in Python to define dictionaries. So this part of your code is not valid:

'Aggregate Orig Estimate (Hrs)':    {
                                                if type(issue.fields.aggregatetimeoriginalestimate) != None):
                                                 issue.fields.aggregatetimeoriginalestimate/(60.0*60.0)
                                                else:
                                                    str(issue.fields.aggregatetimeoriginalestimate)
                                            },

You can write it on one line though:

'Aggregate Orig Estimate (Hrs)': issue.fields.aggregatetimeoriginalestimate/(60.0*60.0) if issue.fields.aggregatetimeoriginalestimate else 0.0

From Python 3.8 you can shorten it further with an assigment :=:

'Aggregate Orig Estimate (Hrs)': agg/(60.0*60.0) if (agg:=issue.fields.aggregatetimeoriginalestimate) else 0.0
Answered By: Tranbi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.