Search code examples
pythonpython-datetime

Python 3 , Unable to convert datetime stamp to datetime object, TypeError: an integer is required (got type str)


Im trying to process timeseries data in CSV and count the number of time epochs, for this example epoch = 1000 millisecs

Here is the python code:

import csv
  import os
  import datetime

    eeg_record = [] 
    path  = "C:\\Users\\ary\\Desktop\\nuerosky blink tests\\backup\\process_from_backup\\blink_50px\\task_blink40000radious50px_duration_40000.csv"
    """read CSV file"""
    def get_dirs(path_):
        if os.access(path_,os.F_OK and os.R_OK):
            f = open(path_)
            row_counter = 0
            for row in csv.reader(f):
                ##print(row) 
                ##skip the header 
                 if row_counter == 0:
                     row_counter = row_counter + 1
                     continue
                 else:
                     eeg_record.append(row)
            f.close()

    """get the number of epochs based on a epoch size in milliseconds"""
    def get_number_of_epochs(epoch_size_in_milliseconds):
            initial_datetime = datetime.datetime(2000,1,1,0,0,0,0)
            current_datetime = datetime.datetime(2000,1,1,0,0,0,0)
            epoch_counter = 0
            row_counter = 0
            epoch = datetime.timedelta(milliseconds=epoch_size_in_milliseconds)
            for row in eeg_record:
                if row_counter == 0:
                    row_counter =  row_counter + 1
                    initial_datetime = str_to_datetime_(row[1])
                else:    
                    current_datetime = str_to_datetime_(row[1])
                    if  initial_datetime - current_datetime >= epoch:
                        initial_datetime = current_datetime
                        epoch_counter = epoch_counter + 1
            print("counter: ",epoch_counter)

""" covert datetime of this format 2017-10-13 19:22:50:525 to datetime object"""
def  str_to_datetime_(datetime_str):
     return datetime.datetime(datetime_str,'%Y-%m-%d %H:%M:%S:%f')

Im running this via Spyder IDE and using Python 3.6.2

My input:

 get_number_of_epochs(1000)

Example of the data:

Time_Stamp_In_Milli_Secs,Time_Stamp_Formatted,Raw,A,B,C,D,Class_Stimulia
1.50795E+12,2017-10-13 19:22:13:249,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:249,55,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:250,55,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:250,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:250,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:250,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:250,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:251,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:251,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:251,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:251,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:251,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:251,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:252,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:252,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:252,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:252,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:252,55,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:253,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:253,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:266,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:266,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:266,55,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:267,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:267,55,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:267,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:267,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:267,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:268,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:268,53,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:280,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:280,54,-1,0,0,-1,NO_STIMULIA
1.50795E+12,2017-10-13 19:22:13:280,54,-1,0,0,-1,NO_STIMULIA

The error:

  File "<ipython-input-235-fac5a2a567a4>", line 33, in get_number_of_epochs
    initial_datetime = str_to_datetime_(row[1])

  File "<ipython-input-235-fac5a2a567a4>", line 45, in str_to_datetime_
    return datetime.datetime(datetime_str,'%Y-%m-%d %H:%M:%S:%f')

TypeError: an integer is required (got type str)

How can I fix this error using the current APIs in my code?


Solution

  • Do the following and see:

    1. change your str_to_datetime_ function to the following

      def  str_to_datetime_(datetime_str):
          return datetime.datetime.strptime(datetime_str, '%Y-%m-%d %H:%M:%S:%f')