如何更改熊猫数据框中列的日期时间格式

时间:2019-11-06 17:54:19

标签: python-3.x pandas datetime-format

我有数据帧(df2)。它的列(日期)以“ Mon Aug 10 11:06:25 UTC 2015”的格式包含一些日期和时间,我必须将其格式更改为“ Aug 10 11:06:25 2015”。

我尝试了以下代码,但给出了错误

df2['date'] = pd.to_datetime(df2['date'], errors='coerce')
df2['date'] = df2['date'].dt.strftime('%b %d %H:%M:%S %Y')
df2

    ---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2896             try:
-> 2897                 return self._engine.get_loc(key)
   2898             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'date'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-403-66f0c1caed0e> in <module>
      1 df2 = df2.rename(columns = {'Mon Aug 10 07:56:39 UTC 2015': 'date'})
      2 
----> 3 df2['date'] = pd.to_datetime(df2['date'], errors='coerce')
      4 df2['date'] = df2['date'].dt.strftime('%b %d %H:%M:%S %Y')
      5 df2

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2978             if self.columns.nlevels > 1:
   2979                 return self._getitem_multilevel(key)
-> 2980             indexer = self.columns.get_loc(key)
   2981             if is_integer(indexer):
   2982                 indexer = [indexer]

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2897                 return self._engine.get_loc(key)
   2898             except KeyError:
-> 2899                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2900         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2901         if indexer.ndim > 1 or indexer.size > 1:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'date'

2 个答案:

答案 0 :(得分:1)

最简单的方法是:

import pandas as pd
df2['date'] = pd.to_datetime(df2['date'], errors='coerce')
df2['date'] = df2['date'].dt.strftime('%b %d %H:%M:%S %Y')

我很确定这可以解决您的主要问题。 documentation

从那时起,您可以更轻松地操作pd.Timestamp对象以显示所需的任何格式。

祝你好运。请告诉我这是否适合您或您是否需要进一步的帮助。

编辑: @AsraKhalid,我怀疑您的错误源实际上在第一行:df2 = df2.rename(columns = {'Mon Aug 10 07:56:39 UTC 2015 ':'date'})。您可能以为您正在更改列名,但是实际上有一个拼写错误,但是没有报告,因为默认情况下df.rename支持错误。尝试将其更改为df2 = df2.rename(列= {'UTC 2015年8月10日星期一07:56:39':'date'},errors =“ raise”)。这样一来,您将看到“ df 10月10日星期一07:56:39 UTC”实际上是在df中还是拼写错误

答案 1 :(得分:0)

您可以使用pandas apply()方法。请检查date formats。我看不到您的时间戳为何有UTC字符串。但是根据您的问题,请尝试以下代码:

from datetime import datetime

def change_date_string(date_string):

   date_string = str(date_string).replace('UTC', '')
   date_object = datetime.strptime(date_string, "%a %b %d %H:%M:%S %Y").strftime('%b %d %H:%M:%S %Y')
   return date_object

df2['date'] = df2['date'].apply(change_date_string)

示例:

from datetime import datetime

date_string = 'Mon Aug 10 11:06:25 UTC 2015'
date_string = str(date_string).replace('UTC', '')
date_object = datetime.strptime(date_string, "%a %b %d %H:%M:%S %Y").strftime('%b %d %H:%M:%S %Y')
print(date_object)

输出:

Aug 10 11:06:25 2015

请注意,输出将为字符串格式