2016-03-10 150 views
1

我已經索引我的數據幀到日期列。現在我想設置索引to_datetime。我的代碼如下:麻煩設置索引值date.time熊貓

import numpy as np 

import pandas as pd 

import glob 

​ 

df = pd.concat((pd.read_csv(f, sep='|', header=None, index_col=None, low_memory=False) for f in glob.glob('/home/jayaramdas/anaconda3/Thesis/FEC_data/itpas2_data/itpas2**.txt'))) 



df.columns = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', 'date', '15', '16', '17', '18', '19', '20', '21', '22']     

df.set_index(pd.to_datetime(df['date']), inplace=True) 

​ 



df1 = df[['1', '6', '7', '10', '12', '13', '15', '16', '17']].copy() 

df1.columns = ['cmte_id', 'trans_typ', 'entity_typ', 'state', 'employer', 'occupation', 'amount', 'fec_id', 'cand_id'] 

Print (df1) ​ 

但我的輸出看起來像它正在追加一個新的日期列。

    cmte_id trans_typ entity_typ state employer \ 
date                   
1970-01-01 00:00:00.008152007 C00112250  24K  ORG DC  NaN 
1970-01-01 00:00:00.009262007 C00119040  24K  CCM FL  NaN 
1970-01-01 00:00:00.009262007 C00119040  24K  CCM MD  NaN 
1970-01-01 00:00:00.00 

我的原始日期列是日期索引中的最後8位數字。此外,從read.csv文件中的前幾行如下(第一行中的日期值是08152007):

C00112250|N|Q3|G|27931381854|24K|ORG|HILLARY CLINTON FOR PRESIDENT EXP. COMM.|WASHINGTON|DC|20013|||08152007|2000|C00431569|P00003392|71006.E7975|307490|||4101720071081637544 
+0

它看起來像你的'date'值實際上大紀元,你可以試試這個:'df.set_index(pd.to_datetime(DF [ '日期'],單位=的'),inplace = True)' – EdChum

+0

我剛剛應用了你的代碼:現在,我在日期列中獲得這個:1970-04-05 08:26:47 –

+0

發佈原始數據,就像我之前詢問過的這個問題避免了含糊 – EdChum

回答

1

OK,我看到你的問題你read_csv行改成這樣:

df = pd.concat((pd.read_csv(f, sep='|', header=None, names=['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', 'date', '15', '16', '17', '18', '19', '20', '21', '22'], index_col=None, dtype={'date':str}) for f in glob.glob('/home/jayaramdas/anaconda3/Thesis/FEC_data/itpas2_data/itpas2**.txt'))) 

這將設置你的列名,並強制日期列被視爲一個str D型,之前把它當作一個int所以它刪除了前導0,那麼你可以轉換類型:

df.set_index(pd.to_datetime(df['date'], format='%m%d%Y), inplace=True) 

例子:

In [336]: 
import pandas as pd 
import io 
t="""C00112250|N|Q3|G|27931381854|24K|ORG|HILLARY CLINTON FOR PRESIDENT EXP. COMM.|WASHINGTON|DC|20013|||08152007|2000|C00431569|P00003392|71006.E7975|307490|||4101720071081637544""" 
df = pd.read_csv(io.StringIO(t), sep='|', header=None, names=['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', 'date', '15', '16', '17', '18', '19', '20', '21', '22'], index_col=None, dtype={'date':str}) 
df.info() 

<class 'pandas.core.frame.DataFrame'> 
Int64Index: 1 entries, 0 to 0 
Data columns (total 22 columns): 
1  1 non-null object 
2  1 non-null object 
3  1 non-null object 
4  1 non-null object 
5  1 non-null int64 
6  1 non-null object 
7  1 non-null object 
8  1 non-null object 
9  1 non-null object 
10  1 non-null object 
11  1 non-null int64 
12  0 non-null float64 
13  0 non-null float64 
date 1 non-null object 
15  1 non-null int64 
16  1 non-null object 
17  1 non-null object 
18  1 non-null object 
19  1 non-null int64 
20  0 non-null float64 
21  0 non-null float64 
22  1 non-null int64 
dtypes: float64(4), int64(5), object(13) 
memory usage: 184.0+ bytes 

In [337]:  
df['date'] = pd.to_datetime(df['date'], format='%m%d%Y') 
df.info() 

<class 'pandas.core.frame.DataFrame'> 
Int64Index: 1 entries, 0 to 0 
Data columns (total 22 columns): 
1  1 non-null object 
2  1 non-null object 
3  1 non-null object 
4  1 non-null object 
5  1 non-null int64 
6  1 non-null object 
7  1 non-null object 
8  1 non-null object 
9  1 non-null object 
10  1 non-null object 
11  1 non-null int64 
12  0 non-null float64 
13  0 non-null float64 
date 1 non-null datetime64[ns] 
15  1 non-null int64 
16  1 non-null object 
17  1 non-null object 
18  1 non-null object 
19  1 non-null int64 
20  0 non-null float64 
21  0 non-null float64 
22  1 non-null int64 
dtypes: datetime64[ns](1), float64(4), int64(5), object(12) 
memory usage: 184.0+ bytes 

In [338]: 
df['date'] 

Out[338]: 
0 2007-08-15 
Name: date, dtype: datetime64[ns] 
+0

@ EdChum:再次感謝!這個社區及其成員會讓我迷失方向。 –

+0

爲了將來的參考,您應該始終嘗試發佈原始數據,代碼,錯誤和期望的輸出。關鍵是原始數據 – EdChum