I am simply trying to load some .txt files, which contain a date and time column that looks like the image below. It’s just four columns, whitespace delimited, and rather straightforward. The time column has values like “2024-05-10T00:00:00Z”, “2024-05-10T00:01:00Z”, and so on, increasing minute by minute.
It appears that the format of these timestamps are a four-digit year, two digit month and day separated by hyphens, a T separator, two digit hours, minutes, and seconds all separated by colons, and a Z. I would assume this format looks like YYYY-mm-ddTHH:MM:SSZ, so we would write that as ‘%Y-%m-%dT%H:%M:%SZ’
Here is the code I use to do this:
import os
import pandas as pd
magnetometer = {} # Dict
directory = os.path.dirname(__file__)
for filename in os.listdir(directory):
if filename.endswith('.txt'):
df = pd.read_csv(os.path.join(directory, filename), delim_whitespace=True, header=0)
df['Time'] = pd.to_datetime('Time', format='%Y-%m-%dT%H:%M:SZ')
df_name = os.path.splitext(filename)[0]
magnetometer[df_name] = df
This raises a ValueError of ValueError: time data "Time" doesn't match format "%Y-%m-%dT%H:%M:SZ", at position 0.
If I have it print the first value of each iteration (by adding a print statement) before it tries to convert it to a DateTime, I get 2024-05-10T00:00:00Z
which baffles me. It doesn’t look like there is any extra whitespace in the string, nor are there any differently formatted values, especially since it raises the error on the very first value. Even changing the format to ‘ISO8601’ raises this error. Any ideas what’s wrong here?
Here is an excerpt of the data: