简体   繁体   中英

How to change date time format of column in pandas data frame

I have data frame (df2). It has column (date) which contains some date and time in format "Mon Aug 10 11:06:25 UTC 2015" I have to change it in format “Aug 10 11:06:25 2015”.

I have tried the following code but it is giving an error

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'

The easiest way would be to do:

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')

I'm pretty sure this will solve your main issue. The documentation .

From that point, you can manipulate the pd.Timestamp object more easily to display whatever format you want.

Good luck. Please let me know if this works for you or if you need further assistance.

Edit: @AsraKhalid, I suspect that the source of your error is actually in the first line: df2 = df2.rename(columns = {'Mon Aug 10 07:56:39 UTC 2015': 'date'}). You are probably thinking you are changing the column name, but there is actually a typo, but it is not being reported because df.rename by default suppreses errors. Try changing it to df2 = df2.rename(columns = {'Mon Aug 10 07:56:39 UTC 2015': 'date'}, errors="raise"). That way you will see whether 'Mon Aug 10 07:56:39 UTC 2015' is actually in the df or if you are misspelling it

You can use pandas apply() method. Please check the date formats I don't see why there is a UTC string in your timestamp. But based on your question, please try the following code:

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)

Example:

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)

Output:

Aug 10 11:06:25 2015

Please note that the output will be in string format

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM