Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Series to DataFrame #619

Open
s-celles opened this issue Nov 19, 2023 · 3 comments
Open

Series to DataFrame #619

s-celles opened this issue Nov 19, 2023 · 3 comments

Comments

@s-celles
Copy link

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
Copy link
Author

s-celles commented Nov 20, 2023

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
Copy link
Author

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

@bml1g12
Copy link

bml1g12 commented Apr 18, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants