0

I am getting an error in time series date-time parsing function. The data is attached. I tried this to read the Date and time column.

Data = pd.read_csv('Data.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Case Msee
  • 405
  • 5
  • 17

1 Answers1

0

Your strptime() function needs to be formatted exactly as the timestamp is formatted, including slashes and colons. You can find the details on the python documentation.

In this case, your timestamp is formatted as 1/1/2016 0:00 but your string format "%Y-%m-%d %H:%M" is expecting 2016-1-1 0:00. If you use '%d/%m/%Y %H:%M' as your format string then the strptime() function works as expected. For example:

import datetime as dt

with open('datac.csv','r') as file:
    for line in file:
        try:
            time = line.split(',')[0] #splits the line at the comma and takes the first bit
            time = dt.datetime.strptime(time, '%d/%m/%Y %H:%M')
            print(time)
        except:
            pass

You should be able to adapt your code with this in mind.

Ari Cooper-Davis
  • 3,374
  • 3
  • 26
  • 43