Reputation: 2219
I have a Pandas dataframe that includes a timestamp column starting from 0.
The first row always starts at time = 0, and the following rows get the relative time from that point. So, e.g. the second row comes 0.25 seconds after the first, obviously it gets the timestamp 0.25.
I want to use the timestamp column mainly for the ability to do resampling and interpolation. So, as far as I know, for that purpose it has to be some time related dtype (pd.Timestamp
in my case).
Now, what I additionally want, is to be able to save the dataframe as a CSV file afterwards. Unfortunately, the pd.Timestamp
column is stored as a datetime string of the format
1970-01-01 00:00:00.000000000
I'd however like to save it like it comes in: as a float value starting from 0.0.
I'm thinking about storing the timestamp in the dataframe as two separate columns, one in pd.Timestamp
format and the other with the same original value as float
.
Though additionally, data value floats in the frame are stored in the format %7.3f
. However, the float value of the timestamp should be more precise, rather something like %.6f
or even more decimal digits. So I'd in addition to all that above need a different float format for a single column.
How can I do all of that together?
Upvotes: 0
Views: 145
Reputation: 1587
Your times sounds more like Timedelta
s to me. You can initialise them as 0, add them together, and then represent them as a float with pd.Timedelta.total_seconds()
.
import pandas as pd
t0 = pd.Timedelta(0)
t1 = t0 + pd.Timedelta('0.25s')
t1_as_float = t1.total_seconds()
Upvotes: 2