Convert Time Format

jason_xiejason_xie Member Posts: 4 Contributor I
edited December 2018 in Help

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!

Answers

  • sgenzersgenzer Administrator, Moderator, Employee, RapidMiner Certified Analyst, Community Manager, Member, University Professor, PM Moderator Posts: 2,959 Community Manager

    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

  • jason_xiejason_xie Member Posts: 4 Contributor I

    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.

  • Thomas_OttThomas_Ott RapidMiner Certified Analyst, RapidMiner Certified Expert, Member Posts: 1,761 Unicorn
    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.
  • Edin_KlapicEdin_Klapic Moderator, Employee, RMResearcher, Member Posts: 299 RM Data Scientist

    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

     
  • debonairdebonair Member Posts: 1 Contributor I
    I have dates from several years and i want to get to categorise the data into months for e.g. data from july 2011, july 2012 etc should be aggregated into july
  • MartinLiebigMartinLiebig Administrator, Moderator, Employee, RapidMiner Certified Analyst, RapidMiner Certified Expert, University Professor Posts: 3,503 RM Data Scientist
    Hi,
    there are many ways to do this. One is to use Generate attribute's date_get function to obtain the Month. Afterwards you can aggregate with a group by on the month.

    Cheers,
    Martin
    - Sr. Director Data Solutions, Altair RapidMiner -
    Dortmund, Germany
Sign In or Register to comment.