Convert Pandas Column to DateTime

匿名 (未验证) 提交于 2019-12-03 02:52:02

问题:

I have one field in a pandas DataFrame that was imported as string format. It should be a datetime variable. How do I convert it to a datetime column and then filter based on date.

Example:

  • DataFrame Name: raw_data
  • Column Name: Mycol
  • Value Format in Column: '05SEP2014:00:00:00.000'

回答1:

Use the to_datetime function, specifying a format to match your data.

raw_data['Mycol'] =  pd.to_datetime(raw_data['Mycol'], format='%d%b%Y:%H:%M:%S.%f') 


回答2:

You can use the DataFrame method .apply() to operate on the values in Mycol:

>>> df = pd.DataFrame(['05SEP2014:00:00:00.000'],columns=['Mycol']) >>> df                     Mycol 0  05SEP2014:00:00:00.000 >>> import datetime as dt >>> df['Mycol'] = df['Mycol'].apply(lambda x:                                      dt.datetime.strptime(x,'%d%b%Y:%H:%M:%S.%f')) >>> df        Mycol 0 2014-09-05 


回答3:

raw_data['Mycol'] =  pd.to_datetime(raw_data['Mycol'], format='%d%b%Y:%H:%M:%S.%f') 

works, however it results in a Python warning of A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

I would guess this is due to some chaining indexing.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!