OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

compute new datetime dataframe column [duplicate]

  • Thread starter Thread starter Swawa
  • Start date Start date
S

Swawa

Guest
I want to create a new datetime column in a python dataframe. First data point is a given time stamp with the format

Code:
print(sinfo.BeginTime)
2021-07-08T08:56:46.637
Name: BeginTime, dtype: object

All subsequent times are calculated based on this first timestamp and a continuous column deltaTime, which contains seconds with some decimal places and is of type <class 'pandas.core.indexes.numeric.Int64Index'> and looks like that:

Code:
index    deltaT
2374     1.20047
2374     1.20047
2376     1.14953
2377     1.14413
2378     1.15073
          ...   
10537    1.14900
10538    1.15000
10539    1.10000
10540    1.25100
10541    1.10000
Name: deltaT, Length: 7108, dtype: float64

How can I connect the two data types? My code does not seem to work:

Code:
#convert deltaT into datetime format
df.deltaT = pd.to_datetime(df.deltaT, unit='s')

This gives not exactly what I expected (as there is still a date different from 0.0.000 what would mean, If I add it to the date of today I would get a date far in the future...:

Code:
RadioRxTimePk
2374    1970-01-01 00:00:01.200469999
2374    1970-01-01 00:00:01.200469999
2376    1970-01-01 00:00:01.149530000
2377    1970-01-01 00:00:01.144130000
2378    1970-01-01 00:00:01.150730000
                     ...             
10537   1970-01-01 00:00:01.148999999
10538   1970-01-01 00:00:01.149999999
10539   1970-01-01 00:00:01.100000000
10540   1970-01-01 00:00:01.251000000
10541   1970-01-01 00:00:01.100000000
Name: deltaT, Length: 7108, dtype: datetime64[ns]

If I add a test time stamp, there is a Type error:

Code:
# make a new column and 
df['time'] = df.testdatetime + df.deltaT

TypeError: cannot add DatetimeArray and DatetimeArray

as expected...

Any hint?
<p>I want to create a new datetime column in a python dataframe.
First data point is a given time stamp with the format</p>
<pre><code>print(sinfo.BeginTime)
2021-07-08T08:56:46.637
Name: BeginTime, dtype: object
</code></pre>
<p>All subsequent times are calculated based on this first timestamp and a continuous column deltaTime, which contains seconds with some decimal places and is of type
<class 'pandas.core.indexes.numeric.Int64Index'> and looks like that:</p>
<pre><code>index deltaT
2374 1.20047
2374 1.20047
2376 1.14953
2377 1.14413
2378 1.15073
...
10537 1.14900
10538 1.15000
10539 1.10000
10540 1.25100
10541 1.10000
Name: deltaT, Length: 7108, dtype: float64
</code></pre>
<p>How can I connect the two data types? My code does not seem to work:</p>
<pre><code>#convert deltaT into datetime format
df.deltaT = pd.to_datetime(df.deltaT, unit='s')
</code></pre>
<p>This gives not exactly what I expected (as there is still a date different from 0.0.000 what would mean, If I add it to the date of today I would get a date far in the future...:</p>
<pre><code>RadioRxTimePk
2374 1970-01-01 00:00:01.200469999
2374 1970-01-01 00:00:01.200469999
2376 1970-01-01 00:00:01.149530000
2377 1970-01-01 00:00:01.144130000
2378 1970-01-01 00:00:01.150730000
...
10537 1970-01-01 00:00:01.148999999
10538 1970-01-01 00:00:01.149999999
10539 1970-01-01 00:00:01.100000000
10540 1970-01-01 00:00:01.251000000
10541 1970-01-01 00:00:01.100000000
Name: deltaT, Length: 7108, dtype: datetime64[ns]
</code></pre>
<p>If I add a test time stamp, there is a Type error:</p>
<pre><code># make a new column and
df['time'] = df.testdatetime + df.deltaT

TypeError: cannot add DatetimeArray and DatetimeArray
</code></pre>
<p>as expected...</p>
<p>Any hint?</p>
 
Top