data-forge / data-forge-ts

The JavaScript data transformation and analysis toolkit inspired by Pandas and LINQ.
http://www.data-forge-js.com/
MIT License
1.34k stars 77 forks source link

Coerce series data types? #12

Closed grahamalama closed 6 years ago

grahamalama commented 6 years ago

Apologies in advance as I'm pretty inexperienced with JavaScript!

I had an issue reading a CSV file where columns of floats were being read in as strings. This proved frustrating when trying to sum() a Series, as it just returned one big concatenated string.

It might be useful to add a method to the Series class to change the data type, similar to pandas.

Maybe something akin this, which I just wrote for a project, though I'd assume you want to lazily evaluate this as well:

/*
 * Returns a new Series whose values are coerced to the specified data type
*/
function coerce_data_type(series,data_type) {
    var coerced_series;
    switch (data_type) {
        case 'number':
            coerced_series = series.toArray().map(value => Number(value));
            break;
        case 'boolean':
            coerced_series = series.toArray().map(value => Boolean(value));
            break;
        case 'string':
            coerced_series = series.toArray().map(value => String(value));
            break;
        default:
            // TODO - error handling
            coerced_series = series.toArray();
            break;
    }
    return new dataForge.Series(coerced_series);
}
grahamalama commented 6 years ago

Never mind . . . should have dug deeper into the documentation 😳

ashleydavis commented 6 years ago

Thanks for logging feedback.

I take it you know how to use parseFloats now?

Also there is a dynamicTyping option you can pass in when loading a CSV file. This will load float and boolean columns to the correct data type.

ashleydavis commented 6 years ago

If you like the library please be sure to put a star on the repo!