2017-09-06 192 views
1

感謝您花時間查看我的問題。Python-Pandas-Dataframe-datetime轉換不包含空值單元格

我嘗試使用下面的函數轉換熊貓數據框中的兩個日期列。我使用這個函數,因爲「Closed Date」有4221行,所以它不應該在null單元格上崩潰。

最終,更改結果爲原始行號的數據框。所以,我不想放鬆在關閉日期有空值的行。

數據幀概述:

<class 'pandas.core.frame.DataFrame'> 
Int64Index: 4272 entries, 0 to 4271 
Data columns (total 4 columns): 
Created Date 4272 non-null object 
Closed Date  4221 non-null object 
Agency   4272 non-null object 
Borough   4272 non-null object 
dtypes: object(4) 

設計功能:

col='Closed Date' 
df[(df[col].notnull())] = df[(df[col].notnull())].apply(lambda x:datetime.datetime.strptime(x,'%m/%d/%Y %I:%M:%S %p')) 

產生錯誤:

TypeError         Traceback (most recent call last) 
<ipython-input-155-49014bb3ecb3> in <module>() 
     9 
    10 col='Closed Date' 
---> 11 df[(df[col].notnull())] = df[(df[col].notnull())].apply(lambda  x:datetime.datetime.strptime(x,'%m/%d/%Y %I:%M:%S %p')) 
    12 print(type(df[(df[col].notnull())])) 

/anaconda/lib/python3.6/site-packages/pandas/core/frame.py in  apply(self, func, axis, broadcast, raw, reduce, args, **kwds) 
    4358       f, axis, 
    4359       reduce=reduce, 
-> 4360       ignore_failures=ignore_failures) 
    4361    else: 
    4362     return self._apply_broadcast(f, axis) 

/anaconda/lib/python3.6/site-packages/pandas/core/frame.py in  _apply_standard(self, func, axis, ignore_failures, reduce) 
    4454    try: 
    4455     for i, v in enumerate(series_gen): 
-> 4456      results[i] = func(v) 
    4457      keys.append(v.name) 
    4458    except Exception as e: 

<ipython-input-155-49014bb3ecb3> in <lambda>(x) 
     9 
    10 col='Closed Date' 
---> 11 df[(df[col].notnull())] = df[(df[col].notnull())].apply(lambda  x:datetime.datetime.strptime(x,'%m/%d/%Y %I:%M:%S %p')) 
    12 print(type(df[(df[col].notnull())])) 

TypeError: ('strptime() argument 1 must be str, not Series', 'occurred  at index Created Date') 
+0

你爲什麼不使用'DF [COL] = pd.to_datetime(DF [COL]格式=「%M /%d /%Y%I: %M:%S%p')'? 'NaNs'將被存儲爲'NaTs' – Zero

回答

1

我想你只需要to_datetime - 它轉換NaNNaT,使所有的值是列在日期時間:

col='Closed Date' 
df[col] = pd.to_datetime(df[col], format='%m/%d/%Y %I:%M:%S %p') 

樣品:

df = pd.DataFrame({'Closed Date':['05/01/2016 05:10:10 AM', 
            '05/01/2016 05:10:10 AM', 
            np.nan]}) 

col='Closed Date' 
df[col] = pd.to_datetime(df[col], format='%m/%d/%Y %I:%M:%S %p') 
print (df) 
      Closed Date 
0 2016-05-01 05:10:10 
1 2016-05-01 05:10:10 
2     NaT 

print (df.dtypes) 
Closed Date datetime64[ns] 
dtype: object 
+0

謝謝,這工作。 –

+0

啊再次感謝,在這裏的第二個帖子,所以仍然有點賺取平臺。這提醒我去我以前的帖子接受它:) –

+0

非常感謝你,美好的一天! – jezrael

相關問題