Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

How can I attribute a timestamp in my data frame after reading the csv file? I have a data set with measurements, but no time stamp. I know the frequency of the sensor data (200 Hz) and the start date/time.

I have tried to count the number of lines in my file and creating a time column. Using pd.insert I've inserted this time stamp. My problem is that when plotting these data, my x-axis does not show the attributed timestamps but rather the number of measurement. My code:

    #Importing signals 

    data = pd.read_csv('.../monday.txt')

    data.columns = ['l1','l2','l3','l4','l5','l6']

    print("Sensor data: ")

    print(data.head())

    print(data.dtypes)

    nbrMeasurments = sum(1 for line in open('.../monday.txt'))

    data.insert(0, "Time", pd.timedelta_range('11:24:26', 

    periods=nbrMeasurments-1, freq="5L"))

    print("Revised sensor data: ")

    print(data.head())

    print(data.dtypes)

In another file where there is a time stamps, it looks like the "index_col='Date'" in pd.read_csv('.../mondayV1.csv',index_col='Date',  usecols= [0,1,2], parse_dates=True) is the command that ensures that the x-axis is referenced by date and not measurment number "x":

                         SYS (mmHg)  DIA (mmHg)

    Date                                       

    2019-08-07 13:06:30         111          61

    2019-08-07 13:07:08         114          64

    2019-08-07 13:07:56         112          63

    2019-08-07 13:08:42         127          81

    2019-08-07 13:09:19         129          83

    Omron data types: 

    SYS (mmHg)    int64

    DIA (mmHg)    int64

With my insert attempt with the file without timestamp, "Time" is listed as a vaiable:

                 Time        l1        l2        l3        l4       l5       
    l6
    0        11:24:26  0.787261  0.943828  1.100903  0.835889  2.524946  
    2.252113
    1 11:24:26.005000  0.787068  0.943638  1.100871  0.835882  2.531180  
    2.253063
    2 11:24:26.010000  0.786951  0.943496  1.100779  0.835909  2.531573  
    2.253395
    3 11:24:26.015000  0.786879  0.943553  1.100877  0.835877  2.533841  
    2.254906
    4 11:24:26.020000  0.786682  0.943536  1.100651  0.835674  2.539893  
    2.257780
    Time    timedelta64[ns]
    l1              float64
    l2              float64
    l3              float64
    l4              float64
    ecg             float64
    ppg             float64

How can I attribute time to this file in the most efficient way?

1 Answer

0 votes
by (25.1k points)

You need to assign values to index so instead of using data.insert do it this way:

data = data.set_index(pd.timedelta_range('11:24:26', periods=nbrMeasurments-1, freq="5L"))

Related questions

Browse Categories

...