I am trying to write data to a csv file in Python. However I am having issues with the format of the timestamp when writing it.
My data is in UNIX millisecond format. I am using pandas inbuilt to_datetime(unit = 'ms') method for this. However, a lot of my early data has no intra-second records. The data is only stored at truncated second intervals. So, whilst I wish for my data to be stored liked this 2015-01-01 00:00:00.000, it instead stores it like this 2015-01-01 00:00:00 and drops the millisecond component completely. Once I get to this year, then I do have data that can be written as 2020-01-01 00:01:07.748, for example.
The part of my code which writes this can be seen here:
df = pd.DataFrame(data, columns = ['Trade ID', 'Date', 'Amount', 'Price'])
df['Date'] = pd.to_datetime(df['Date'], unit = 'ms')
df.to_csv(csv_file_path, mode = 'a', header = False, index = False)
I know for a fact that it has nothing to do with the line which actually writes it to the csv. As it keeps the millisecond data for records which do have intra-second records. I know that it is in the line where it converts it from milliseconds to datetime. I have tried using the format parameter as well in the method but that throws an error as you cannot specify both unit and format.