尝试更改数据帧中的条目,从而引发关键错误

时间:2019-07-16 10:52:23

标签: python pandas dataframe datetime

我从this dataset创建了一个名为autos的数据框。 我在此数据框中有3列,其中以日期作为条目。我要删除删除日期中的小时,分​​钟和秒部分。 示例:

data = [["2016-03-24 11:52:17"], ["2016-03-24 10:58:45"], ["2016-03-14 12:52:21"]] 
auto = pd.DataFrame(data, columns = ['date_crawled']) 

输出:

          date_crawled
0  2016-03-24 11:52:17
1  2016-03-24 10:58:45
2  2016-03-14 12:52:21

我想我可以通过创建以下函数来做到这一点,该函数将在日期列中进行格式化。

import datetime as dt
def datetimeconv(date_column):
    for i in range(0,371528,1):
        for elements in auto[i,date_column]:
            elements=dt.datetime.strptime(elements,"%Y-%m-%d %H:%M:%S")
            elements=elements.strftime("%d-%m-%Y")
            auto.loc[i,date_column]=(elements)

当我尝试在date_crawled列上进行测试时:

datetimeconv("date_crawled")

我遇到以下错误:

KeyError                                  Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3077             try:
-> 3078                 return self._engine.get_loc(key)
   3079             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: (0, 'date_crawled')

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-71-2e8c9398d8c4> in <module>
      6             elements=elements.strftime("%d-%m-%Y")
      7             auto.loc[i,date_column]=(elements)
----> 8 datetimeconv("date_crawled")
      9 

<ipython-input-71-2e8c9398d8c4> in datetimeconv(date_column)
      2 def datetimeconv(date_column):
      3     for i in range(0,371528,1):
----> 4         for elements in auto[i,date_column]:
      5             elements=dt.datetime.strptime(elements,"%Y-%m-%d %H:%M:%S")
      6             elements=elements.strftime("%d-%m-%Y")

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2686             return self._getitem_multilevel(key)
   2687         else:
-> 2688             return self._getitem_column(key)
   2689 
   2690     def _getitem_column(self, key):

~\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
   2693         # get column
   2694         if self.columns.is_unique:
-> 2695             return self._get_item_cache(key)
   2696 
   2697         # duplicate columns & possible reduce dimensionality

~\Anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
   2487         res = cache.get(item)
   2488         if res is None:
-> 2489             values = self._data.get(item)
   2490             res = self._box_item_values(item, values)
   2491             cache[item] = res

~\Anaconda3\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath)
   4113 
   4114             if not isna(item):
-> 4115                 loc = self.items.get_loc(item)
   4116             else:
   4117                 indexer = np.arange(len(self.items))[isna(self.items)]

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3078                 return self._engine.get_loc(key)
   3079             except KeyError:
-> 3080                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   3081 
   3082         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

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: (0, 'date_crawled')

为什么会出现关键错误?

2 个答案:

答案 0 :(得分:1)

KeyError原因:

您必须使用df.loc[i,'date_crawled']而不是df[i,'date_crawled']。后者尝试通过元组(i,'date_crawled')选择具有层次结构索引(multiindex)的列(系列)。这样的列在您的数据框中不存在,因此是KeyError。

大熊猫的正常做法是:

auto['date_crawled'] = auto['date_crawled'].apply(lambda x: pd.to_datetime(x).strftime("%d-%m-%Y"))

或者,正如尼尔斯·沃纳(Nils Werner)在评论中指出的那样,

auto['date_crawled'] = pd.to_datetime(auto['date_crawled']).dt.strftime("%d-%m-%Y")



要回答您的代码为何不起作用(除了KeyError之外)的问题:在for elements in auto.loc[i,date_column]中,您要遍历每个条目中的各个字符。以下是工作版本:

def datetimeconv(date_column):
    for i in range(0,len(auto)):
            elements=auto.loc[i,date_column]
            elements=dt.datetime.strptime(elements,"%Y-%m-%d %H:%M:%S")
            elements=elements.strftime("%d-%m-%Y")
            auto.loc[i,date_column]=(elements)

但是,切勿在数据框行上进行显式迭代,请尽可能使用pandas方法。这段代码只是为了说明您的错误所在。

答案 1 :(得分:0)

您可以将这些列转换为DateTime列,并使用pd.to_datetime(column).dt.date删除时间:

df[['date_crawled', 'ad_created', 'last_seen']] = df[['date_crawled', 'ad_created', 'last_seen']].apply(lambda x: pd.to_datetime(x).dt.date)
df
# date_crawled  ad_created  last_seen
# 0 2016-03-24  2016-03-24  2016-04-07
# 1 2016-03-24  2016-03-24  2016-04-07
# 2 2016-03-14  2016-03-14  2016-04-05
# 3 2016-03-17  2016-03-17  2016-03-17
# 4 2016-03-31  2016-03-31  2016-04-06