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

Long to wide, filling missing element with nil #287

Open
KitaitiMakoto opened this issue Jan 5, 2024 · 1 comment · May be fixed by #288
Open

Long to wide, filling missing element with nil #287

KitaitiMakoto opened this issue Jan 5, 2024 · 1 comment · May be fixed by #288
Labels
bug Something isn't working
Milestone

Comments

@KitaitiMakoto
Copy link
Contributor

Hello,

Can I reshape a long data frame to a wide data frame filling missing element with nil?

When I have long data frame such as:

df = DataFrame.new(
  names: %w[name1 name1 name1 name2 name2 name3 name3 name3],
  NAME: %w[One Two Three One Three One Two Three],
  VALUE: [1.1, 2.1, 3.1, 1.2, 3.2, 1.3, 2.3, 3.3]
)

I want wide data frame as:

wide = DataFrame.new(
  names: %w[name1 name2 name3],
  One: [1.1, 1.2, 1.3],
  Two: [2.1, nil, 2.3],
  Three: [3.1, 3.2, 3.3]
)

How can I do this? to_wide raises error: #to_wide with missing values(DataFrameReshapingTest::to_wide): IndexError: element size differs (3 should be 4)

@KitaitiMakoto KitaitiMakoto linked a pull request Jan 6, 2024 that will close this issue
@heronshoes
Copy link
Contributor

Thank you @KitaitiMakoto .

I think this feature is useful.
I think it is better to separate steps in filling missing records.

  1. Create Cartesian Product(直積) of df.names.uniq and df.NAME.uniq.
  2. Fill missing records in df.
  3. #to_wide

Step 1 may require new method to create DataFrame from two Vectors.
Step 2 may use #join ? (I'm not sure now).

I will code them and compare performance with your proposal.

@heronshoes heronshoes added the bug Something isn't working label Feb 7, 2024
@heronshoes heronshoes added this to the 0.5.3 milestone Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants