ploadText data conversion issue in DolphinDB

Navigating Data Conversion in DolphinDB: From Integer to MINUTE Format

Hello fellow data enthusiasts! Today, I want to share a nifty workaround I discovered while working with data in DolphinDB, particularly how to convert an integer representing time into DolphinDB’s MINUTE data format. This journey began when I faced a challenge; the usual data import function ploadText brought in a column supposed to represent time as plain integers (like 85900), with no direct parameter to transform the format while loading.

For those not familiar, DolphinDB’s MINUTE format represents time, in minutes, since the midnight of the day, but our data was in a not-so-friendly format. Let’s break it down: the integer 85900 translates to 23:59 in the HH:MM format, or one minute to midnight. Ideally, we want this expressed as 1439m in DolphinDB’s MINUTE format (since there are 1439 minutes from midnight to one minute before the next midnight).

The Challenge

The problem was the lack of an in-built parameter in ploadText to handle this transformation directly. Initially, I was stumped. How could I convert a dataset full of these integer values into something meaningful within DolphinDB without manually tweaking each value?

The Breakthrough

After tinkering around with various DolphinDB functions and doing a bit of research, I developed a method. It turns out, the key lies in understanding and manipulating the native time formats of DolphinDB cleverly.

Here’s how I did it:

Step 1: First, import your data as usual with the function ploadText. Assume the column with time is named rawTime.

// Assume df is your loaded table
table df = ploadText("your_data_path");

Step 2: The next task is converting the rawTime integer values. This integer represents HHMMSS, so 85900 basically is 23:59:00. To extract the hour and minute:

// Convert integer time format HHMMSS to HH:MM and then to minutes since midnight
df = select rawTime, (int(rawTime / 10000) * 60) + int((rawTime % 10000) / 100) as timeInMinutes from df;

This script does the transformation. int(rawTime / 10000) pulls out the hour and multiplies it by 60 (to convert hours to minutes), while int((rawTime % 10000) / 100) gets the minute portion.

Step 3: Now, we have the time in minutes since midnight, but to fully integrate with DolphinDB’s time format capabilities, convert this integer into DolphinDB’s MINUTE type:

// Convert integer minutes to DolphinDB MINUTE type
df = update df set timeInMinutes = minute(timeInMinutes);

And just like that, the column timeInMinutes in df now holds the time in DolphinDB’s MINUTE format, compatible with all of DolphinDB’s time-series functionalities.

Conclusion

While a direct conversion feature in ploadText would have been convenient, DolphinDB is flexible enough to allow for manual transformations with just a few extra lines of code. This method has not only solved the issue but also enhanced my understanding and appreciation of handling time data in DolphinDB.

I hope this helps anyone who might be encountering similar data format challenges. The key takeaway here is not to shy away from the manual scripts. Sometimes, they open up a whole new way of understanding your data. Happy data wrangling!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *