MCPcopy Create free account
hub / github.com/javascriptdata/danfojs / $toCSV

Function $toCSV

src/danfojs-base/io/node/io.csv.ts:293–325  ·  view source on GitHub ↗
(df: NDframe | DataFrame | Series, options?: CsvOutputOptionsNode)

Source from the content-addressed store, hash-verified

291 * ```
292 */
293const $toCSV = (df: NDframe | DataFrame | Series, options?: CsvOutputOptionsNode): string | void => {
294 let { filePath, sep, header } = { sep: ",", header: true, filePath: undefined, ...options }
295
296 if (df.$isSeries) {
297 const csv = df.values.join(sep);
298
299 if (filePath !== undefined) {
300 if (!(filePath.endsWith(".csv"))) {
301 filePath = filePath + ".csv"
302 }
303 fs.writeFileSync(filePath, csv, "utf8")
304 } else {
305 return csv;
306 }
307 } else {
308 const rows = df.values as ArrayType2D
309 let csvStr = header === true ? `${df.columns.join(sep)}\n` : ""
310
311 for (let i = 0; i < rows.length; i++) {
312 const row = `${rows[i].join(sep)}\n`;
313 csvStr += row;
314 }
315
316 if (filePath !== undefined) {
317 if (!(filePath.endsWith(".csv"))) {
318 filePath = filePath + ".csv"
319 }
320 fs.writeFileSync(filePath, csvStr, "utf8")
321 } else {
322 return csvStr;
323 }
324 }
325};
326
327
328/**

Callers 1

$writeCsvOutputStreamFunction · 0.70

Calls 2

joinMethod · 0.80
endsWithMethod · 0.80

Tested by

no test coverage detected