javascriptdata / danfojs

Danfo.js is an open source, JavaScript library providing high performance, intuitive, and easy to use data structures for manipulating and processing structured data.
https://danfo.jsdata.org/
MIT License
4.81k stars 209 forks source link

Dataframe "nunique" is broken. Quick fix suggested. #338

Closed KingWebsites closed 2 years ago

KingWebsites commented 2 years ago

I believe the nunique function for a dataFrame is broken.

Steps to reproduce the behavior: If I add an extra item to each column of the test data then the nunique function creates an error.

Here's the new code adapted from the API Reference (browser) with an extra item added to each column:

        let data = {
            "A": [-20, 30, 47.3, -20, 50],
            "B": [34, -4, 5, 6, 8],
            "C": [20, 20, 30, 30, 30],
            "D": ["a", "b", "c", "c", "d"]
        }

        let df = new dfd.DataFrame(data);
        df.nunique().print();

Expected result: I expect to see a new single-column dataframe with rows containing the total number of unique items for that row.

But I get an error of: "IndexError: You provided an index of length 5 but Ndframe rows has lenght of 4"

I believe somewhere the count of rows and columns is getting swapped by mistake. If the dataset is turned back into a 4x4 grid it works. It just doesn't like this new 4x5 one. The test data is 4x4 so the error is never seen during test.

Suggested fix: Apologies for not having the skills to fix this myself but I think the fix is to change this conditional from 0 to 1 in both these files: danfojs/danfojs-browser/src/core/frame.js ln. 2019 danfojs/danfojs-node/src/core/frame.js ln. 2021

Change if (axis === 0) { to if (axis === 1) {

My rough fix by downloading the bundle.js and editing that conditional has fixed the issue for me.

risenW commented 2 years ago

Thanks for raising this issue, I’ll take a look ASAP.

risenW commented 2 years ago

@KingWebsites @michael-pont Can you install and use the latest Typescript version (v1.0.0). This has been fixed.

If you need help migrating to the latest version, see: https://danfo.jsdata.org/examples/migrating-to-the-stable-version-of-danfo.js