RapidMiner

Highlighted
Learner III jason_xie
Learner III

Convert Time Format

Hi All, 

 

I need some help with converting my time format. Within the dataset that I have, my time is in 24 hour format  eg. 19:00 and 00:00. I am trying to convert these times into 12 hour format where 19:00 would turn into 7:00pm and 00:00 would turn into 12:00am. 

 

I used Nominal to Date operator, and set the date type to time, date format to h:mm a, but it's giving me a cannot parse date error since 00:00 is an unparseable date. Any thoughts on solving this problem? 

 

Thanks!

4 REPLIES
Community Manager Community Manager
Community Manager

Re: Convert Time Format

hello @jason_xie - I really should defer to @Marco_Boeck on this as I know how much he likes working with dates and times. But does this help?

 

<?xml version="1.0" encoding="UTF-8"?><process version="8.0.000-BETA">
  <context>
    <input/>
    <output/>
    <macros/>
  </context>
  <operator activated="true" class="process" compatibility="8.0.000-BETA" expanded="true" name="Process">
    <process expanded="true">
      <operator activated="true" class="generate_data_user_specification" compatibility="8.0.000-BETA" expanded="true" height="68" name="Generate Data by User Specification" width="90" x="45" y="85">
        <list key="attribute_values">
          <parameter key="datetime" value="date_now()"/>
        </list>
        <list key="set_additional_roles"/>
      </operator>
      <operator activated="true" class="generate_attributes" compatibility="8.0.000-BETA" expanded="true" height="82" name="Generate Attributes" width="90" x="179" y="85">
        <list key="function_descriptions">
          <parameter key="datetime" value="date_add(datetime,5,DATE_UNIT_HOUR)"/>
        </list>
      </operator>
      <operator activated="true" class="date_to_nominal" compatibility="8.0.000-BETA" expanded="true" height="82" name="Date to Nominal" width="90" x="313" y="85">
        <parameter key="attribute_name" value="datetime"/>
        <parameter key="date_format" value="dd/MM/yyyy HH:mm"/>
      </operator>
      <operator activated="true" class="nominal_to_date" compatibility="8.0.000-BETA" expanded="true" height="82" name="Nominal to Date" width="90" x="447" y="85">
        <parameter key="attribute_name" value="datetime"/>
        <parameter key="date_type" value="time"/>
        <parameter key="date_format" value="dd/MM/yyyy HH:mm"/>
      </operator>
      <connect from_op="Generate Data by User Specification" from_port="output" to_op="Generate Attributes" to_port="example set input"/>
      <connect from_op="Generate Attributes" from_port="example set output" to_op="Date to Nominal" to_port="example set input"/>
      <connect from_op="Date to Nominal" from_port="example set output" to_op="Nominal to Date" to_port="example set input"/>
      <connect from_op="Nominal to Date" from_port="example set output" to_port="result 1"/>
      <portSpacing port="source_input 1" spacing="0"/>
      <portSpacing port="sink_result 1" spacing="0"/>
      <portSpacing port="sink_result 2" spacing="0"/>
    </process>
  </operator>
</process>

Scott

Scott Genzer
Senior Community Manager
RapidMiner, Inc.
Learner III jason_xie
Learner III

Re: Convert Time Format

Thanks so much, seems like it works the best if the variable has both date and time information. In my case, I have date and time as 2 different variables and the best way to approach it is to concac these two variables together and convert the time format then.

 

Would love to hear if  @Marco_Boeck[community.rapidminer.com] has any other inputs on this issue.

RM Certified Expert
RM Certified Expert

Re: Convert Time Format

Have you tried concatenating them first and then applying the Nominal to Date operator? Concatenation can be done via the Generate Attribute operator or Generate Concatenation too.
RM Staff
RM Staff

Re: Convert Time Format

Hi @jason_xie,

 

the format you were probably searching for was simply "hh:mm" and not "hh:mm a".

The date format parameter within the Operator Nominal to Date needs to know what the Attribute looks like.This means if your Attribute does not have AM or PM, the conversion cannot work.

If your string only has values like 00:00 then this setting will not work.

 

By the way:

Whereas the attribute types date / time / date_time are internally stored in milliseconds since 01/01/1970, the *visual* representation in RapidMiner is always of the format "MMM dd, yyyy hh:mm:ss a z" (e.g. Nov 20, 2017 1:16:53 PM CET).

If you want to have a different visualization you can only convert the attribute to nominal using the Operator Date to Nominal or a conversion function within Generate Attributes (e.g. date_str_custom()).

 

Best regards,
Edin

 
Polls
How can RapidMiner increase participation in our new competitions?
Twitter Feed