Python Transform from DateTime to Date

Comments

6 comments

  • Avatar
    Adrian Williams

    Hi Nancy,

    can you clarify what you are trying to achieve?

    If you just want to filter the input records where the value of a datetime field is before or after a particular datetime then you can just use the Filter node. For example, using the 'create' field in the default data of the Create Data node as the field to be examined you could configure the Filter node as follows:

     

     

    Because the 'create' field is a datetime field you must also set the time element of the value on the far right of the criteria properties. In this case the criteria is configured to include all values after the last second in 2005.

    You can examine the underlying code used by the Filter node by clicking on the Advanced tab. If required you can use this as the basis of a custom filter by modifying the auto-generated code, or you can paste the code into the ProcessRecords property of a Transform node and then customize it.

    For instance, the corresponding code for the above filter is as follows (within the [Code]...[/Code] block):

    [Code]

    _cond = ( \
      ( \
        fn.cmp(inputs[0]['create'],datetime.datetime.strptime("2005-12-31 23:59:59","%Y-%m-%d %H:%M:%S")) > 0 \
      ) \
    )

    if _cond:
      outputs[0] += inputs[0]

    [/Code]

     

    If you wanted to modify this to filter values between two datetimes you could change this to,say:

    [Code]

    _cond_1 = ( \
      ( \
        fn.cmp(inputs[0]['create'],datetime.datetime.strptime("2005-12-31 23:59:59","%Y-%m-%d %H:%M:%S")) > 0 \
      ) \
    )

    _cond_2 = ( \
      ( \
        fn.cmp(inputs[0]['create'],datetime.datetime.strptime("2010-01-01 00:00:00","%Y-%m-%d %H:%M:%S")) < 0 \
      ) \
    )

    if _cond_1 and _cond_2:
      outputs[0] += inputs[0]

    [/Code]

    0
    Comment actions Permalink
  • Avatar
    Adrian Williams

    If you want to extract the date component of a datetime field [for example the 'create' field in the Create Data default data] and output it as a date type field then you could use a Transform node and configure the ConfigureFields property as follows (excluding the [Code]..[/Code] delimiters:

    [Code]

    #Configure all fields from input 'in1' to be mapped
    #to the corresponding fields on the output 'out1'
    out1 += in1

    ## Define new output metadata for a date type field
    out1.dateValue = datetime.date

    [/Code]

     

    Then configure the ProcessRecords script as follows:

    [Code]

    #Copy all fields from input 'in1' to the corresponding output fields
    #in output 'out1'. Copies the values of the fields that have been setup
    #in the mapping defined in the ConfigureFields property
    out1 += in1

    if fields.create is Null:
      out1.dateValue = Null
    else:
      out1.dateValue = fields.create.date()

    [/Code]

     

    Note that you have to explicitly handle any Null values in the input data else the node will fail.

    0
    Comment actions Permalink
  • Avatar
    Adrian Williams

    I have posted a community custom node here that provides a generic mechanism to extract the date element of a datetime field.

     

    0
    Comment actions Permalink
  • Avatar
    Nick Lancaster

    Hi Adrian

    Would you need to go into and edit the filter each time you want to change the date range.Is there away to enter the date range without having to edit the filter each time (e.g like using user input box)?

    0
    Comment actions Permalink
  • Avatar
    Adrian Williams

    A node must be configured before it is run. If you want to use a form-based approach the Filter node can be configured with an 'After' filter criterion and a 'Before' filter criterion to select records where the date is between the two threshold dates:

    If you want to specify property values that can be used by multiple nodes within a data flow see the Advanced section of the help that discusses paramterization using data flow properties and run properties. The online version of this topic is here:

    https://d3sa-preview.infogixsaas.com/docs/dist/help/Default.htm#d-advanced-topics/using-derived-property-values.htm%3FTocPath%3DAdvanced%2520topics%7C_____4

    [Edit: ^ Updated URL]

     

    0
    Comment actions Permalink
  • Avatar
    Adrian Williams

    Data3Sixty Analyze v.3.2.7 introduced the Modify Fields node which allows you to modify field metadata without scripting.

    You can now use this node to convert a datetime type field to a date type.

    0
    Comment actions Permalink

Please sign in to leave a comment.



Powered by Zendesk