Python 大熊猫的负时程

Python 大熊猫的负时程,python,pandas,Python,Pandas,我有一个包含两列的数据集:实际时间和承诺时间(表示某个进程的实际和承诺开始时间) 例如: import pandas as pd example_df = pd.DataFrame(columns = ['Actual Time', 'Promised Time'], data = [ ('2016-6-10 9:00', '2016-6-10 9:00'), ('2016-6-

我有一个包含两列的数据集:实际时间和承诺时间(表示某个进程的实际和承诺开始时间)

例如:

import pandas as pd
example_df = pd.DataFrame(columns = ['Actual Time', 'Promised Time'],
                 data = [
                     ('2016-6-10 9:00', '2016-6-10 9:00'),
                     ('2016-6-15 8:52', '2016-6-15 9:52'),
                     ('2016-6-19 8:54', '2016-6-19 9:02')]).applymap(pd.Timestamp)
所以我们可以看到,
有时实际时间=承诺时间
,但也有
实际时间<承诺时间
的情况


我定义了一列来显示这两列之间的差异(
example_df['Actual Time']-example_df['Promised Time']
),但问题是第三行返回的是
-1天+23:52:00
,而不是
-00:08:00

差异结果为
timedelta
类型,默认为
ns
格式

您需要将结果的类型更改为所需的格式:

import pandas as pd

df=pd.DataFrame(data={
'Actual Time':['2016-6-10 9:00','2016-6-15 8:52','2016-6-19 8:54'],
'Promised Time':['2016-6-10 9:00','2016-6-15 9:52','2016-6-19 9:02']
},dtype='datetime64[ns]')

# here you need to add the `astype` part and to determine the unit you want
df['diff']=(df['Actual Time']-df['Promised Time']).astype('timedelta64[m]')



我假设您的数据帧已经在
datetime
dtype中<代码>防抱死制动系统工作正常

不带abs的

df['Actual Time'] - df['Promised Time']

Out[526]:
0            00:00:00
1   -1 days +23:00:00
2   -1 days +23:52:00
dtype: timedelta64[ns]
abs(df['Promised Time'] - df['Actual Time'])

Out[529]:
0   00:00:00
1   01:00:00
2   00:08:00
dtype: timedelta64[ns]
abs

df['Actual Time'] - df['Promised Time']

Out[526]:
0            00:00:00
1   -1 days +23:00:00
2   -1 days +23:52:00
dtype: timedelta64[ns]
abs(df['Promised Time'] - df['Actual Time'])

Out[529]:
0   00:00:00
1   01:00:00
2   00:08:00
dtype: timedelta64[ns]

样本

print (df)
       Actual Time   Promised Time
0   2016-6-10 9:00  2016-6-10 9:00
1  2016-6-15 10:52  2016-6-15 9:52 <- changed datetimes
2   2016-6-19 8:54  2016-6-19 9:02

def format_timedelta(x):
    ts = x.total_seconds()
    if ts >= 0:
        hours, remainder = divmod(ts, 3600)
        minutes, seconds = divmod(remainder, 60)
        return ('{}:{:02d}:{:02d}').format(int(hours), int(minutes), int(seconds)) 
    else:
        hours, remainder = divmod(-ts, 3600)
        minutes, seconds = divmod(remainder, 60)
        return ('-{}:{:02d}:{:02d}').format(int(hours), int(minutes), int(seconds)) 
然后是时间增量:

df['diff'] = (df['Actual Time'] - df['Promised Time'])
如果通过正常工作将负时间增量转换为秒:

df['diff1'] = df['diff'].dt.total_seconds()
但是,如果需要字符串表示中的负时间增量,可以使用自定义函数,因为尚未实现时间增量的
strftime

df['diff2'] = df['diff'].apply(format_timedelta)
print (df)
          Actual Time       Promised Time              diff   diff1     diff2
0 2016-06-10 09:00:00 2016-06-10 09:00:00          00:00:00     0.0   0:00:00
1 2016-06-15 10:52:00 2016-06-15 09:52:00          01:00:00  3600.0   1:00:00
2 2016-06-19 08:54:00 2016-06-19 09:02:00 -1 days +23:52:00  -480.0  -0:08:00

你能重新格式化你的数据吗?这样读很难。。是否有多行或这是一行?代码在哪里?我建议进行编辑,用生成示例数据的代码替换示例数据。要知道,如果使用带空格的列,则很难使用
pd.read\u clipboard()
复制数据。要了解更多信息,请访问这个伟大的问题:您想要绝对值,否
-00:08:00
?然后请更改答案,因为问题是如何将
-1天+23:52:00
更改为
-00:08:00
,并接受答案更改为
00:08:00