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

from_csv via String #534

Open
reedjosh opened this issue Jun 3, 2020 · 2 comments
Open

from_csv via String #534

reedjosh opened this issue Jun 3, 2020 · 2 comments

Comments

@reedjosh
Copy link

reedjosh commented Jun 3, 2020

Feature Request
I get csv output from a command. I would like a way to give Daru.from_csv a csv string for parsing instead of a path.

In Pandas I do:

pd.read_csv(StringIO(results.stdout))

I don't know if StringIO is do-able for Daru.from_csv, but I'm working around it now via:

def csv_string_to_df(csv_str)
	csv_as_arrays = ::CSV.parse(csv_str)
	headers = csv_as_arrays.shift
	csv_as_arrays = csv_as_arrays.transpose

	hsh = {}
	headers.each_with_index do |h, i|
  	hsh[h] = csv_as_arrays[i]
	end
	Daru::DataFrame.new(hsh)
end

I kind of think that if you just attempted to read the file first and then gave that to CSV.parse then I could use ruby's StringIO in place of a path since it would be read outside of the CSV lib.

Thank you!

@athityakumar
Copy link
Member

@reedjosh - Thanks a lot for suggesting this feature! 🎉

We currently have a read_csv method that reads from a csv file. If you want to have a similar from_csv method that imports from a CSV String / StringIO, it'd involve a very minor code change in the daru-io repository

Specifically in this file, you can add a from method that looks like this:

def from(file_data)
  @file_data = file_data
  self
end

and expose a from_csv method liek this in the same file:

Daru::DataFrame.register_io_module :from_csv, self

Would you be willing to contribute this feature, @reedjosh?

@reedjosh
Copy link
Author

reedjosh commented Jun 4, 2020

I would love to. It will be a bit before I can get to it though. Will update you when I get going on it.

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