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

shift is missing for Series #617

Open s-celles opened 1 year ago

s-celles commented 1 year ago

Is your feature request related to a problem? Please describe. A DanfoJS Series shift method seems to be missing https://pandas.pydata.org/docs/reference/api/pandas.Series.shift.html

Describe the solution you'd like

I want to convert to JS this Python code

df["Lap"] = ((df["Distance"] - df["Distance"].shift()) < 0).astype(int).cumsum()

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context shift method is very convenient to measure difference between 2 consecutive values.

Here is my full code

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <script src="https://cdn.jsdelivr.net/npm/danfojs@1.1.2/lib/bundle.min.js"></script>
     <script src="https://cdn.jsdelivr.net/npm/d3-fetch@3.0.1/dist/d3-fetch.min.js"></script>
     <script src="https://cdn.jsdelivr.net/npm/d3-dsv@3.0.1/dist/d3-dsv.min.js"></script>
    <title>KRP Telemetry</title>
</head>

<body>

    <div id="map_div"></div>

    <div id="plot_div_Engine"></div>

    <div id="plot_div_CylHeadTemp"></div>
    <div id="plot_div_WaterTemp"></div>
    <div id="plot_div_Gear"></div>
    <div id="plot_div_Speed"></div>
    <div id="plot_div_LatAcc"></div>
    <div id="plot_div_LonAcc"></div>
    <div id="plot_div_Steer"></div>
    <div id="plot_div_Throttle"></div>
    <div id="plot_div_Brake"></div>
    <div id="plot_div_FrontBrakes"></div>
    <div id="plot_div_Clutch"></div>
    <div id="plot_div_YawVel"></div>

    <script>

        const url = "https://raw.githubusercontent.com/scls19fr/krp_python_telemetry/main/Logdata%20Essay%20mini60%202023-10-31.csv"

        parsed = fetch(url)
            .then(response => response.text())
            .then(text => {
            // Normalize line breaks and split segments by double line breaks.
            const [metaText, columns, rows] = text.replace(/\r?\n/g, "\n").split(/\n\n/);
            // Parse first meta section as csv, then convert to an object
            const meta = Object.fromEntries(d3.csvParseRows(metaText));
            // Parse the two lines from the headers segment
            const [headers, units] = d3.csvParseRows(columns);
            // Parse the rows segment into an untyped array of arrays.
            const data = d3.csvParseRows(rows);

            df_head = new dfd.DataFrame([meta])
            df_head.print()

            df_units = new dfd.DataFrame([units], {columns: headers}) 
            df_units.print()

            df_data = new dfd.DataFrame(data, {columns: headers});
            //console.log(df_data);
            df_data.print();
            //df_data["Lap"] = df_data["Distance"].shift(); // ToFix
            //df_data["Lap"].print();

            df_data.plot("map_div").scatter({
                config: {
                    x: "PosX", y: "PosY"
                }
            });

            df_data.plot("plot_div_Engine").line({
                config: {
                    x: "Distance", y: "Engine"
                }
            });

            df_data.plot("plot_div_CylHeadTemp").line({
                config: {
                    x: "Distance", y: "CylHeadTemp"
                }
            });

            df_data.plot("plot_div_WaterTemp").line({
                config: {
                    x: "Distance", y: "WaterTemp"
                }
            });

            df_data.plot("plot_div_Gear").line({
                config: {
                    x: "Distance", y: "Gear"
                }
            });

            df_data.plot("plot_div_Speed").line({
                config: {
                    x: "Distance", y: "Speed"
                }
            });

            df_data.plot("plot_div_LatAcc").line({
                config: {
                    x: "Distance", y: "LatAcc"
                }
            });

            df_data.plot("plot_div_LonAcc").line({
                config: {
                    x: "Distance", y: "LonAcc"
                }
            });

            df_data.plot("plot_div_Steer").line({
                config: {
                    x: "Distance", y: "Steer"
                }
            });

            df_data.plot("plot_div_Throttle").line({
                config: {
                    x: "Distance", y: "Throttle"
                }
            });

            df_data.plot("plot_div_Brake").line({
                config: {
                    x: "Distance", y: "Brake"
                }
            });

            df_data.plot("plot_div_FrontBrakes").line({
                config: {
                    x: "Distance", y: "FrontBrakes"
                }
            });

            df_data.plot("plot_div_Clutch").line({
                config: {
                    x: "Distance", y: "Clutch"
                }
            });

            df_data.plot("plot_div_YawVel").line({
                config: {
                    x: "Distance", y: "YawVel"
                }
            });

            return {meta, headers, units, data};
            })

        //const df = new dfd.DataFrame({'pig': [20, 18, 489, 675, 1776],
        //                       'horse': [4, 25, 281, 600, 1900]}, {index: [1990, 1997, 2003, 2009, 2014]})
        //df.plot("plot_div").line()

    </script>
</body>

</html>
s-celles commented 1 year ago

Related #126