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.8k stars 210 forks source link

Series to DataFrame #619

Open s-celles opened 1 year ago

s-celles commented 1 year ago

Hello,

I don't know if it's a feature request or doc request... but I haven't found how to convert a Series to a DataFrame. Similar to https://pandas.pydata.org/docs/reference/api/pandas.Series.to_frame.html

Kind regards

s-celles commented 12 months ago

This is probably not the best approach but here is a solution

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>MWE</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
  <body>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/danfojs@1.1.2/lib/bundle.min.js"></script>
    <script>
      (function(window, document, undefined) {
        // code that should be taken care of right away
        window.onload = init;

        function row2col(row_array) {
          col_array = []
          row_array.forEach(function(row) {
            col_array.push([row])
          })
          return col_array;
        }

        function toframe(ser, colname) {
          df = new dfd.DataFrame(row2col(ser.values), { columns: [colname], index: ser.index });
          return df
        }

        function init() {
          console.log("init");
          ser = new dfd.Series([10, 11, 12, 13], { index: ["a", "b", "c", "d"] });
          ser.print();
          df = toframe(ser, "NewCol");
          df.print()
        }
      })(window, document, undefined);
    </script>
    <h1>MWE</h1>
  </body>
</html>

and console ouput

init
startup:4 
╔═══╤════╗
║ a │ 10 ║
╟───┼────╢
║ b │ 11 ║
╟───┼────╢
║ c │ 12 ║
╟───┼────╢
║ d │ 13 ║
╚═══╧════╝

startup:4 
╔════════════╤═══════════════════╗
║            │ NewCol            ║
╟────────────┼───────────────────╢
║ a          │ 10                ║
╟────────────┼───────────────────╢
║ b          │ 11                ║
╟────────────┼───────────────────╢
║ c          │ 12                ║
╟────────────┼───────────────────╢
║ d          │ 13                ║
╚════════════╧═══════════════════╝

Maybe Series could have a method for that.

Any opinion?

s-celles commented 12 months ago

Moreover if Series had a name attribute we could, by default use it to give DataFrame column an apropriate name

bml1g12 commented 7 months ago

Moreover, in typescript, I'm unclear how to type narrow something to being a DataFrame, when we know it is not a Series

df.constructor.name === "DataFrame" df instanceof dfd.DataFrame

Both seem to be giving me issues