python - 无法使用 pd.to_datetime 转换为日期时间

标签 python datetime csv pandas

我正在尝试读取 csv 文件并将其转换为数据帧以用作时间序列。 csv 文件是这种类型:

         #Date      Time    CO_T1_AHU.01_CC_CTRV_CHW__SIG_STAT
0          NaN       NaN                                     %   
1          NaN       NaN  Cooling Coil Hydronic Valve Position   
2   2014-01-01  00:00:00                                     0   
3   2014-01-01  01:00:00                                     0   
4   2014-01-01  02:00:00                                     0   
5   2014-01-01  03:00:00                                     0   
6   2014-01-01  04:00:00                                     0

我使用以下方式读取文件:

df = pd.read_csv ('filepath/file.csv', sep=';', parse_dates = [[0,1]])

产生这个结果:

             #Date_Time   FCO_T1_AHU.01_CC_CTRV_CHW__SIG_STAT
0               nan nan                                     %   
1               nan nan  Cooling Coil Hydronic Valve Position   
2   2014-01-01 00:00:00                                     0   
3   2014-01-01 01:00:00                                     0   
4   2014-01-01 02:00:00                                     0   
5   2014-01-01 03:00:00                                     0   
6   2014-01-01 04:00:00                                     0

继续将字符串转换为日期时间并将其用作索引:

pd.to_datetime(df.values[:,0])
df.set_index([df.columns[0]], inplace=True)

所以我明白了:

                      FCO_T1_AHU.01_CC_CTRV_CHW__SIG_STAT
#Date_Time                                                  
nan nan                                                 %   
nan nan              Cooling Coil Hydronic Valve Position   
2014-01-01 00:00:00                                     0   
2014-01-01 01:00:00                                     0   
2014-01-01 02:00:00                                     0   
2014-01-01 03:00:00                                     0   
2014-01-01 04:00:00                                     0 

但是,pd.to_datetime 无法转换为日期时间。有没有办法找出错误是什么?

非常感谢。 路易斯

最佳答案

字符串条目“nan nan”无法使用 to_datetime 进行转换,因此请将其替换为空字符串,以便现在可以将它们转换为 NaT:

In [122]:

df['Date_Time'].replace('nan nan', '',inplace=True)
df
Out[122]:
             Date_Time  index       CO_T1_AHU.01_CC_CTRV_CHW__SIG_STAT
0                           0                                     %   
1                           1  Cooling Coil Hydronic Valve Position   
2  2014-01-01 00:00:00      2                                     0   
3  2014-01-01 01:00:00      3                                     0   
4  2014-01-01 02:00:00      4                                     0   
5  2014-01-01 03:00:00      5                                     0   
6  2014-01-01 04:00:00      6                                        0
In [124]:

df['Date_Time'] = pd.to_datetime(df['Date_Time'])
df

Out[124]:
            Date_Time  index       CO_T1_AHU.01_CC_CTRV_CHW__SIG_STAT
0                 NaT      0                                     %   
1                 NaT      1  Cooling Coil Hydronic Valve Position   
2 2014-01-01 00:00:00      2                                     0   
3 2014-01-01 01:00:00      3                                     0   
4 2014-01-01 02:00:00      4                                     0   
5 2014-01-01 03:00:00      5                                     0   
6 2014-01-01 04:00:00      6                                        0

更新

实际上,如果您只是设置 coerce=True 那么它就可以很好地转换:

df['Date_Time'] = pd.to_datetime(df['Date_Time'], coerce=True)

关于python - 无法使用 pd.to_datetime 转换为日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26613083/

相关文章:

Python CGI - 根据用户时区更改数据

python - 通过pycharm IDE运行py.test中的特定测试

c# - 为什么 ISO-8601 规范在涉及小数时似乎被普遍忽略?

php - PHPExcel 上的 .csv 文件问题

python - c-扩展Python内存泄漏,为什么?

python - 在 pandas 中使用混合日期时间格式

java - Java 是否有一个好的*严格*日期解析器?

php - 在 mysql 中使用 SELECT INTO OUTFILE 命令保存 XLS 文件

csv - 将 Import-CSV 结果从字符串转换为任意数据类型

python - 在 wsgi 测试环境中提供静态文件