[SOLVED] ValueError: time data '14.03.2022 00:00:00.000 GMT-0400' does not match format '%d.%m.%Y %H:%M:%S.%f %z' (match)

Issue

I want to change the date format of my data but it doesn’t seem to work.

here is the error that it give me when I try to run my code "ValueError: time data ‘14.03.2022 00:00:00.000 GMT-0400’ does not match format ‘%d.%m.%Y %H:%M:%S.%f %z’ (match) "

here my code:

import pandas as pd
import numpy as np
from scipy.signal import argrelextrema
import matplotlib.pyplot as plt

# import our historical data
data = pd.read_csv('Data/EURUSD.csv')
data.columns = ['Date', 'open', 'high', 'low', 'close', 'volume']
data = data.drop_duplicates(keep=False)
data.Date = pd.to_datetime(data.Date, format='%d.%m.%Y %H:%M:%S.%f %z')
data = data.set_index(data.Date)
data = data['open','high','low','close','volume']
price = data.close.iloc[:100]

# find our relative extrema
max_idx = argrelextrema(price.values, np.greater, order=1)
min_idx = argrelextrema(price.values, np.less, order=1)

print(max_idx)
print(min_idx)

EURUSD.CSV

Local time,Open,High,Low,Close,Volume
14.03.2022 00:00:00.000 GMT-0400,1.09049,1.09177,1.09015,1.09175,6011.13
14.03.2022 01:00:00.000 GMT-0400,1.09175,1.09186,1.09089,1.09116,7262.199999999998
14.03.2022 02:00:00.000 GMT-0400,1.09114,1.09231,1.09093,1.09178,5828.28
14.03.2022 03:00:00.000 GMT-0400,1.09177,1.09407,1.09109,1.09320,12058.280000000004
14.03.2022 04:00:00.000 GMT-0400,1.09321,1.09601,1.09303,1.09567,14770.299999999996

Solution

According to strtime.org %z is

UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object
is naive).

So clearly GMT-0400 is not valid value for that.

As you are already using pandas you might simply do not provide format, which will cause pandas to detect it automatically, consider following example

import pandas as pd
timestring = '14.03.2022 00:00:00.000 GMT-0400'
timestamp = pd.to_datetime(timestring)
print(timestamp)

output

2022-03-14 00:00:00+04:00

Answered By – Daweo

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *