aozk
aozk

Reputation: 418

pandas Timestamp to datetime.date

I have problem with converting pandas Series to datetime.datetime.

I got DataFrame - df, with column Timestamp of type: pandas._libs.tslibs.timestamps.Timestamp and column Timestamp-end of type: pandas._libs.tslibs.timedeltas.Timedelta enter image description here

I found that topic on SO: Converting pandas.tslib.Timestamp to datetime python but the suggestions on this topic did not work.

Is there any possibility to convert it into datetime? If no, how can I subtract Timestamp-end from Timestamp column of type to get date and time into Timestamp and Timedelta type?

How I created Timestamp column:

import adodbapi
import pandas as pd
import numpy as np
import datetime as dt

cursor = myConn.cursor()
cursor.execute(query)
# every row in query_list is type of SQLrow
query_list = [row for row in cursor]
df = pd.DataFrame({'TagAddress':[row[0] for row in query_list], 'Timestamp':[row[1] for row in query_list], 'Value':[row[3] for row in query_list]})

Timestamp-end column:

df['Timestamp-end'] = pd.NaT
# in for loop, dict values are type of timestamps.Timestamp
df['Timestamp-end'].iloc[i] = df['Timestamp'].iloc[i] - current_errors_timestamp[curr_fault_key]

My expected output (column Result):

I just want to subtract Timedelta from Timestamp to get new column Timestamp. With type datetime.datetime I can do it without any problems.

Timestamp               ErrorValue  Machine Station FAULT   Timestamp-end           Result
2020-06-20 08:01:09.562 370         T1      R1      1       0 days 00:00:06         2020-06-20 08:01:03
2020-06-20 08:01:21.881 370         T1      R1      0       0 days 00:00:12.319000  2020-06-20 08:01:09
2020-06-20 08:07:06.708 338         T1      R1      0       0 days 00:00:24.623000  2020-06-20 08:06:42
2020-06-20 08:07:31.041 338         T1      R1      0       0 days 00:00:18.333000  2020-06-20 08:07:13

Upvotes: 1

Views: 3692

Answers (1)

jezrael
jezrael

Reputation: 862406

I beleive you need convert column to dates:

df['Timestamp1'] = df['Timestamp'].dt.date

Or beter should be remove times, set them to 00:00:00:

df['Timestamp1'] = df['Timestamp'].dt.normalize()

And then subtract.

EDIT: You can subtract values and then use Series.dt.floor for seconds:

df['Timestamp-end'] = pd.to_timedelta(df['Timestamp-end'])
df['Result'] = df['Timestamp'].sub(df['Timestamp-end']).dt.floor('S')
print (df)
                Timestamp  ErrorValue Machine Station  FAULT   Timestamp-end  \
0 2020-06-20 08:01:09.562         370      T1      R1      1        00:00:06   
1 2020-06-20 08:01:21.881         370      T1      R1      0 00:00:12.319000   
2 2020-06-20 08:07:06.708         338      T1      R1      0 00:00:24.623000   
3 2020-06-20 08:07:31.041         338      T1      R1      0 00:00:18.333000   

               Result  
0 2020-06-20 08:01:03  
1 2020-06-20 08:01:09  
2 2020-06-20 08:06:42  
3 2020-06-20 08:07:12  

Upvotes: 2

Related Questions