日期列未转换为日期时间类型

时间:2019-01-20 17:45:00

标签: pandas datetime python-3.5

我有以下数据框df:

     Opname_start_date              Opname_end_date
0  2016-11-21 11:33:00  2016-11-23 12:02:59.0000000
1  2016-07-06 09:16:00  2016-07-06 09:16:01.0000000
2  2016-08-11 10:18:00  2016-08-15 10:40:59.0000000
3  2016-09-23 11:28:00  2016-09-23 11:28:01.0000000
4  2016-08-11 14:06:00  2016-08-11 14:06:01.0000000
5  2016-10-26 09:42:00  2016-10-29 11:25:59.0000000
6  2016-12-02 15:03:00  2016-12-09 14:00:00.0000000
7  2016-07-08 09:00:00  2016-09-27 09:15:01.0000000
8  2016-06-14 09:01:00  2016-06-17 13:00:00.0000000
9  2016-12-08 13:54:00  2016-12-08 13:54:01.0000000

我想从Opname_start_date中减去Opname_end_date,并将其保存到新列中。我尝试了以下方法:

df['LOS'] = df['Opname_end_date'] - df['Opname_start_date']

但是它给了我以下错误:

TypeError: ufunc subtract cannot use operands with types dtype('<U27') and dtype('<M8[ns]')

我检查了数据帧的dtypes,它显示第一列是datetime64 [ns],但是第二列仍然是object。我尝试使用pd.to_datetime(df['Opname_end_date'])转换第二列,但它仍显示该列属于object类型,并且不会引发任何错误。 我无法弄清楚问题出在哪里。如果有人可以帮助我,那就太好了。谢谢

1 个答案:

答案 0 :(得分:2)

对我来说,to_datetime会将列Opname_end_date转换为datetimes

print (df.dtypes)
Opname_start_date    datetime64[ns]
Opname_end_date              object
dtype: object

df['LOS'] = pd.to_datetime(df['Opname_end_date']) - df['Opname_start_date']

或者:

df['Opname_end_date'] = pd.to_datetime(df['Opname_end_date'])
df['LOS'] =  df['Opname_end_date'] - df['Opname_start_date']

print (df)
    Opname_start_date              Opname_end_date              LOS
0 2016-11-21 11:33:00  2016-11-23 12:02:59.0000000  2 days 00:29:59
1 2016-07-06 09:16:00  2016-07-06 09:16:01.0000000  0 days 00:00:01
2 2016-08-11 10:18:00  2016-08-15 10:40:59.0000000  4 days 00:22:59
3 2016-09-23 11:28:00  2016-09-23 11:28:01.0000000  0 days 00:00:01
4 2016-08-11 14:06:00  2016-08-11 14:06:01.0000000  0 days 00:00:01
5 2016-10-26 09:42:00  2016-10-29 11:25:59.0000000  3 days 01:43:59
6 2016-12-02 15:03:00  2016-12-09 14:00:00.0000000  6 days 22:57:00
7 2016-07-08 09:00:00  2016-09-27 09:15:01.0000000 81 days 00:15:01
8 2016-06-14 09:01:00  2016-06-17 13:00:00.0000000  3 days 03:59:00
9 2016-12-08 13:54:00  2016-12-08 13:54:01.0000000  0 days 00:00:01