Python Transform from DateTime to Date
Hello. I am using 3.1.7 and I need to take a datetime field and transform it to a date field.
I tried the strftime but that won't work. I'm trying to filter by date and in 3.1.7 there may be a bug because it lets me choose the two dates from that datetime field but then gives me an error.
I can't filter by date if I change the date to a string because I don't get the calendar.
Any thoughts or suggestions?
-
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]
-
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 += in1if 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.
-
I have posted a community custom node here that provides a generic mechanism to extract the date element of a datetime field.
-
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:
[Edit: ^ Updated URL]
Please sign in to leave a comment.
Comments
6 comments