Read CSV into a numpy array

Create a function read_csv_into_numpy that receives a CSV file path as parameter and imports it as a CSV. The function also receives an optional parameter has_header to help you identify if the CSV file has a header or not.

Return it as a NumPy array.

For testing purposes, we'll be using the first 10 lines of a GPX track file (see the original here).

Use the tests for further clarification.

Test Cases

test read csv with header - Run Test

def test_read_csv_with_header():
    expected = np.array([
        [48.89016,  2.68927, 71.],
        [48.89   ,  2.68973, 72.],
        [48.88987,  2.68981, 72.],
        [48.88924,  2.68957, 67.],
        [48.88934,  2.69005, 67.],
        [48.88949,  2.6914 , 65.],
        [48.88868,  2.69198, 64.],
        [48.88772,  2.6933 , 59.],
        [48.8877 ,  2.69348, 59.]
    ])

    assert np.array_equal(
        read_csv_into_numpy('EntreDhuisEtMarne2014.csv'),
        expected)

test read csv without header - Run Test

def test_read_csv_without_header():
    expected = np.array([
        [48.89016,  2.68927, 71.],
        [48.89   ,  2.68973, 72.],
        [48.88987,  2.68981, 72.],
        [48.88924,  2.68957, 67.],
        [48.88934,  2.69005, 67.],
        [48.88949,  2.6914 , 65.],
        [48.88868,  2.69198, 64.],
        [48.88772,  2.6933 , 59.],
        [48.8877 ,  2.69348, 59.]
    ])

    assert np.array_equal(
        read_csv_into_numpy('EntreDhuisEtMarne2014__headerless.csv', has_header=False),
        expected)

Solution 1

import csv
import numpy as np

def read_csv_into_numpy(file_path, has_header=True):
    with open(file_path) as fp:
        reader = csv.reader(fp)
        if has_header:
            header = next(reader)
        lines = [l for l in reader]
        return np.array(lines, dtype='float')
Files associated with this lesson:

EntreDhuisEtMarne2014__headerless.csv

EntreDhuisEtMarne2014.csv

import numpy as np def read_csv_into_numpy(file_path, has_header): pass