2013-07-20 58 views
2

我在Mac OS X上使用熊貓0.11。我試圖用熊貓導入一個csv文件read_csv,其中一列是完整的時間戳,值如下:防止大熊貓read_csv截斷完整的時間戳

fullts 
1374087067.357464 
1374087067.256206 
1374087067.158231 
1374087067.074162 

我很感興趣,讓後續的時間戳之間的時間差,所以我將其導入指定dtype

data = read_csv(fn, dtype={'fullts': float64}) 

然而,大熊貓似乎截斷數量的整數部分:

data.fullts.head(4) 

產量:

1374087067 
1374087067 
1374087067 
1374087067 

有什麼建議?

謝謝!

補充:嘗試使用pd.to_datetime的建議,並得到這個錯誤:

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-8-37ed0da45608> in <module>() 
---> 1 pd.to_datetime(sd1.fullts) 

/Users/user/anaconda/lib/python2.7/site-packages/pandas-0.11.0-py2.7-macosx-10.5-x86_64.egg/pandas/tseries/tools.pyc in to_datetime(arg, errors, dayfirst, utc, box, format) 
    102   values = arg.values 
    103   if not com.is_datetime64_dtype(values): 
--> 104    values = _convert_f(values) 
    105   return Series(values, index=arg.index, name=arg.name) 
    106  elif isinstance(arg, (np.ndarray, list)): 

/Users/user/anaconda/lib/python2.7/site-packages/pandas-0.11.0-py2.7-macosx-10.5-x86_64.egg/pandas/tseries/tools.pyc in _convert_f(arg) 
    84    else: 
    85     result = tslib.array_to_datetime(arg, raise_=errors == 'raise', 
---> 86             utc=utc, dayfirst=dayfirst) 
    87    if com.is_datetime64_dtype(result) and box: 
    88     result = DatetimeIndex(result, tz='utc' if utc else None) 
/Users/user/anaconda/lib/python2.7/site-packages/pandas-0.11.0-py2.7-macosx-10.5-x86_64.egg/pandas/tslib.so in pandas.tslib.array_to_datetime (pandas/tslib.c:15411)() 

TypeError: object of type 'float' has no len() 
+1

它在我的電腦中正常工作,你使用哪種熊貓版本? – waitingkuo

+0

我在MAC OS X Lion上使用pandas 0.11和anaconda python Lion – Fra

回答

2

你並不需要指定D型時從CSV閱讀(它應該默認使用float64)。

在大熊貓0.12可以使用的to_datetime單位參數整數或浮點數(的信號出現時間)轉換成大熊貓時間戳的然後隱蔽列:

In [11]: df 
Out[11]: 
     fullts 
0 1.374087e+09 
1 1.374087e+09 
2 1.374087e+09 
3 1.374087e+09 

In [12]: pd.to_datetime(df.fullts) # default unit is ns 
Out[12]: 
0 1970-01-01 00:00:01.374087067 
1 1970-01-01 00:00:01.374087067 
2 1970-01-01 00:00:01.374087067 
3 1970-01-01 00:00:01.374087067 
Name: fullts, dtype: datetime64[ns] 

In [13]: pd.to_datetime(df.fullts, unit='s') 
Out[13]: 
0 2013-07-17 18:51:07.357464 
1 2013-07-17 18:51:07.256206 
2 2013-07-17 18:51:07.158231 
3 2013-07-17 18:51:07.074162 
Name: fullts, dtype: datetime64[ns] 

當文檔字符串狀態:

unit : unit of the arg (D,s,ms,us,ns) denote the unit in epoch
              (e.g. a unix timestamp), which is an integer/float number

+0

嘿Andy,謝謝你的回答。你認爲這個問題可能是我安裝了熊貓0.11嗎? – Fra

+0

@Fra在0.11這不佔用浮點值(但我認爲它需要整數,所以要麼乘以使它成爲納秒int或稍候等待0.12-out v)) –

+0

Pandas 0.12出來了,效果很好。謝謝 – Fra