MCPcopy Index your code
hub / github.com/javascriptdata/danfojs / $openCsvInputStream

Function $openCsvInputStream

src/danfojs-base/io/node/io.csv.ts:338–425  ·  view source on GitHub ↗
(filePath: string, options: CsvInputOptionsNode)

Source from the content-addressed store, hash-verified

336 * ```
337 */
338const $openCsvInputStream = (filePath: string, options: CsvInputOptionsNode) => {
339 const { header } = { header: true, ...options }
340 let isFirstChunk = true
341 let ndFrameColumnNames: any = []
342
343 const csvInputStream = new stream.Readable({ objectMode: true });
344 csvInputStream._read = () => { };
345
346 if (filePath.startsWith("http") || filePath.startsWith("https")) {
347 const dataStream = request.get(filePath);
348
349 // reject any non-2xx status codes
350 dataStream.on('response', (response: any) => {
351 if (response.statusCode < 200 || response.statusCode >= 300) {
352 throw new Error(`HTTP ${response.statusCode}: ${response.statusMessage}`);
353 }
354 });
355
356 const parseStream: any = Papa.parse(Papa.NODE_STREAM_INPUT, { header, dynamicTyping: true, ...options });
357 dataStream.pipe(parseStream);
358 let count = 0
359
360 parseStream.on("data", (chunk: any) => {
361 if (isFirstChunk) {
362 if (header === true) {
363 ndFrameColumnNames = Object.keys(chunk)
364 } else {
365 ndFrameColumnNames = chunk
366 }
367 isFirstChunk = false
368 return
369 }
370
371 const df = new DataFrame([Object.values(chunk)], {
372 columns: ndFrameColumnNames,
373 index: [count++]
374 })
375 csvInputStream.push(df);
376 });
377
378 parseStream.on("finish", () => {
379 csvInputStream.push(null);
380 return (null);
381 });
382
383 return csvInputStream;
384 } else {
385 const fileStream = fs.createReadStream(filePath)
386
387 fs.access(filePath, fs.constants.F_OK, (err) => {
388 if (err) {
389 throw new Error("ENOENT: no such file or directory");
390 }
391
392 let count = 0
393 Papa.parse(fileStream, {
394 ...{ header, dynamicTyping: true, ...options },
395 step: results => {

Callers

nothing calls this directly

Calls 3

startsWithMethod · 0.80
getMethod · 0.80
valuesMethod · 0.65

Tested by

no test coverage detected