2016-09-23 116 views
1

我從.json文件與熊貓DataFrame導入數據,結果是有點破:熊貓數據框中的日期和時間,從JSON格式

   >> print df 
       summary        response_date 
        8.0 {u'$date': u'2009-02-19T10:54:00.000+0000'} 
       11.0 {u'$date': u'2009-02-24T11:23:45.000+0000'} 
       14.0 {u'$date': u'2009-03-03T17:55:07.000+0000'} 
       16.0 {u'$date': u'2009-03-10T12:23:04.000+0000'} 
       19.0 {u'$date': u'2009-03-17T17:19:55.000+0000'} 
       13.0 {u'$date': u'2009-03-25T15:10:52.000+0000'} 
       22.0 {u'$date': u'2009-04-02T16:57:31.000+0100'} 
       15.0 {u'$date': u'2009-04-08T22:29:09.000+0100'} 
       20.0 {u'$date': u'2009-04-16T18:14:20.000+0100'} 
       13.0 {u'$date': u'2009-04-29T10:47:06.000+0100'} 
       15.0 {u'$date': u'2009-05-06T13:45:45.000+0100'} 
       20.0 {u'$date': u'2009-05-26T10:41:52.000+0100'} 

如何擺脫「日期」和其他亂七八糟的用日期和時間創建一個正常的列。爲了從ISO8601格式轉換我通常使用:

df.response_date = pd.to_datetime(df.response_date) 

更新1

 summary     response_date         closed_date         open_date 
      24.0 2011-10-15T00:00:00.000+0100           NaN           NaN 
      24.0 2011-11-24T09:00:00.000+0000           NaN           NaN 
      19.0 2011-10-01T09:00:00.000+0100           NaN           NaN 
      25.0 2011-10-29T09:00:00.000+0100           NaN           NaN 
      19.0 2011-10-08T09:00:00.000+0100           NaN           NaN 
      -1.0 2011-11-09T17:20:00.000+0000 {u'$date': u'2011-11-16T15:20:00.000+0000'} {u'$date': u'2011-11-09T15:20:00.000+0000'} 
      -1.0 2011-11-16T17:20:00.000+0000 {u'$date': u'2011-11-23T15:20:00.000+0000'} {u'$date': u'2011-11-16T15:20:00.000+0000'} 
      -1.0 2011-11-23T17:20:00.000+0000 {u'$date': u'2011-11-30T15:20:00.000+0000'} {u'$date': u'2011-11-23T15:20:00.000+0000'} 
      -1.0 2011-11-30T17:20:00.000+0000 {u'$date': u'2011-12-07T15:20:00.000+0000'} {u'$date': u'2011-11-30T15:20:00.000+0000'} 

因此,

>> df.response_date = pd.DataFrame(df.response_date.values.tolist()) 

完美地工作,但其他列包含NaN值,並與插補「-1」沒有幫助。

>> print type(df.ix[0,'scheduleClosedAt']) 
<type 'int'> 

更新2

爲什麼這個(屏蔽)方法不起作用?

>> df.reset_index(inplace=True) 
>> indx_nan_closed = df.closed_date.isnull() 
>> df[~indx_nan_closed].closed_date = pd.DataFrame(df[~indx_nan_closed].closed_date.values.tolist()) 

這條線相當於一個在上面,但與掩蔽陣列,所以想要這種方法僅適用於非NaN值,但結果是,我的數據幀「DF」保持不變。這很奇怪。

有什麼想法?

+0

什麼是'打印類型(df.ix [ 0,'response_date')'? – jezrael

+0

@jezrael

回答

2

您可以使用DataFrame構造與values如果type轉換列response_datelistdict

print (type(df.ix[0,'response_date'])) 
<class 'dict'> 

df.response_date = pd.DataFrame(df.response_date.values.tolist()) 
df.response_date = pd.to_datetime(df.response_date) 
print (df) 
    summary  response_date 
0  8.0 2009-02-19 10:54:00 
1  11.0 2009-02-24 11:23:45 
2  14.0 2009-03-03 17:55:07 

如果typestring,使用splitstrip

print (type(df.ix[0,'response_date'])) 
<class 'str'> 

df.response_date = df.response_date.str.split().str[1].str.strip("'u}") 
df.response_date = pd.to_datetime(df.response_date) 

print (df) 
    summary  response_date 
0  8.0 2009-02-19 10:54:00 
1  11.0 2009-02-24 11:23:45 
2  14.0 2009-03-03 17:55:07 

編輯的評論:

2個可能的解決方案:

首先是通過fillnadict

df.closed_date = df.closed_date.fillna(pd.Series([{}])) 

另一個是boolean indexing

import numpy as np 
import pandas as pd 

df = pd.DataFrame({'summary':[19.0, -1.0,-1.0], 
        'response_date':['2011-10-08T09:00:00.000+0100','2011-11-09T17:20:00.000+0000','2011-11-16T17:20:00.000+0000'], 
       'closed_date':[np.nan, {u'$date': u'2011-11-16T15:20:00.000+0000'}, {u'$date': u'2011-11-23T15:20:00.000+0000'}]}, 
        columns=['summary','response_date','closed_date']) 

print (df) 
    summary     response_date \ 
0  19.0 2011-10-08T09:00:00.000+0100 
1  -1.0 2011-11-09T17:20:00.000+0000 
2  -1.0 2011-11-16T17:20:00.000+0000 

           closed_date 
0          NaN 
1 {'$date': '2011-11-16T15:20:00.000+0000'} 
2 {'$date': '2011-11-23T15:20:00.000+0000'} 
a = df.ix[df.closed_date.notnull(), 'closed_date'] 
print (a) 
1 {'$date': '2011-11-16T15:20:00.000+0000'} 
2 {'$date': '2011-11-23T15:20:00.000+0000'} 
Name: closed_date, dtype: object 

df['closed_date'] = pd.DataFrame(a.values.tolist(), index=a.index) 
df.closed_date = pd.to_datetime(df.closed_date) 
print (df) 

    summary     response_date   closed_date 
0  19.0 2011-10-08T09:00:00.000+0100     NaT 
1  -1.0 2011-11-09T17:20:00.000+0000 2011-11-16 15:20:00 
2  -1.0 2011-11-16T17:20:00.000+0000 2011-11-23 15:20:00 
+0

和在這種情況下: ??? –

+0

它看起來像'NaN',然後使用'fillna'。 – jezrael

+0

絕對,但「fillna(-1)」不起作用。請參閱其他「有問題」列的更新問題 –