简体   繁体   English

pandas 时间戳到 datetime.date

[英]pandas Timestamp to datetime.date

I have problem with converting pandas Series to datetime.datetime.我在将 pandas 系列转换为 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 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 在此处输入图像描述

I found that topic on SO: Converting pandas.tslib.Timestamp to datetime python but the suggestions on this topic did not work.我在 SO 上找到了该主题: Converting pandas.tslib.Timestamp to datetime python但有关此主题的建议不起作用。

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?如果不是,如何从Timestamp类型的列中减去Timestamp-end以将日期和时间转换为 Timestamp 和 Timedelta 类型?

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 ):我预期的 output (列Result ):

I just want to subtract Timedelta from Timestamp to get new column Timestamp .我只想从Timestamp中减去Timedelta以获得新列Timestamp With type datetime.datetime I can do it without any problems.使用datetime.datetime类型,我可以毫无问题地做到这一点。

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

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 :或者更好的应该是删除时间,将它们设置为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:编辑:您可以减去值,然后使用Series.dt.floor几秒钟:

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  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM