我认为您的问题是使用 loc 当您仅访问数据框中的一列时。您只需从代码中删除loc 即可消除此问题。
请看下面的玩具示例,
ops_data_clean_1 = pd.DataFrame()
ops_data_clean_1['Package committed-time'] = ['2018-01-01 00:00:30', '2018-01-01 00:49:00', '2018-03-01 00:00:45']
ops_data_clean_1['Flight launched-time'] = ['2018-01-01 01:00:30', '2018-01-01 02:49:00', '2018-03-01 00:54:45']
ops_data_clean_1['Package committed-time'] = pd.to_datetime(ops_data_clean_1['Package committed-time'])
ops_data_clean_1['Flight launched-time'] = pd.to_datetime(ops_data_clean_1['Flight launched-time'])
ops_data_clean_1['time_to_launch'] = ops_data_clean_1['Flight launched-time'] - ops_data_clean_1['Package committed-time']
ops_data_clean_1.head()
# Output
Package committed-time Flight launched-time time_to_launch
0 2018-01-01 00:00:30 2018-01-01 01:00:30 01:00:00
1 2018-01-01 00:49:00 2018-01-01 02:49:00 02:00:00
2 2018-03-01 00:00:45 2018-03-01 00:54:45 00:54:00
如果您想使用loc,您必须使用: 选择数据框的所有行,例如ops_data_clean_1.loc[:, 'Flight launched-time']
那么代码就变成了,
ops_data_clean_1 = pd.DataFrame()
ops_data_clean_1['Package committed-time'] = ['2018-01-01 00:00:30', '2018-01-01 00:49:00', '2018-03-01 00:00:45']
ops_data_clean_1['Flight launched-time'] = ['2018-01-01 01:00:30', '2018-01-01 02:49:00', '2018-03-01 00:54:45']
ops_data_clean_1.loc[:, 'Package committed-time'] = pd.to_datetime(ops_data_clean_1['Package committed-time'])
ops_data_clean_1.loc[:, 'Flight launched-time'] = pd.to_datetime(ops_data_clean_1['Flight launched-time'])
ops_data_clean_1['time_to_launch'] = ops_data_clean_1.loc[:, 'Flight launched-time'] - ops_data_clean_1.loc[:, 'Package committed-time']
ops_data_clean_1.head()
# Output
Package committed-time Flight launched-time time_to_launch
0 2018-01-01 00:00:30 2018-01-01 01:00:30 01:00:00
1 2018-01-01 00:49:00 2018-01-01 02:49:00 02:00:00
2 2018-03-01 00:00:45 2018-03-01 00:54:45 00:54:00