簡體   English   中英

熊貓時間序列重采樣的問題

[英]Issues with Pandas Timeseries Resample

我正在使用python 3.5.1和Pandas 0.18.0,並嘗試使用此筆記本來修改財務報價數據,因為這些練習對我很感興趣:

我對某些命令有疑問,想知道這是否歸因於python和pandas的版本?

例如:

這是我正在讀取的文件,具有相關的輸出:

data = pd.read_csv('test30dayes2tickforpython.csv',index_col=0,        header=0,parse_dates={"Timestamp" : [0,1]})
data.dtypes
Out[80]:
 Open              float64
 High              float64
 Low               float64
 Last              float64
 Volume              int64
 NumberOfTrades      int64
 BidVolume           int64
 AskVolume           int64
dtype: object

當我嘗試創建另一個這樣的對象時:

ticks = data.ix[:, ['High','Volume']]
ticks

我得到NaN值:

    High    Volume
Timestamp       
2015-12-27 23:00:25.000 NaN NaN
2015-12-27 23:01:11.000 NaN NaN

但是,如果我使用列引用而不是名稱,它將起作用:

ticks = data.ix[:, [1,4]]
ticks


High    Volume
Timestamp       
2015-12-27 23:00:25.000 2045.25 1
2015-12-27 23:01:11.000 2045.50 2

為什么是這樣?

同樣,筆記本顯示了另一個創建的對象:

bars = ticks.Price.resample('1min', how='ohlc')
bars

當我嘗試這個我得到這個錯誤:

bars = ticks.High.resample('60min', how='ohlc')
bars

1小節= ticks.High.resample('60min',how ='ohlc')
AttributeError:“ DataFrame”對象沒有屬性“ High”

如果我不調用“高”列,它將起作用:

bars = ticks.resample('60min', how='ohlc')
bars

FutureWarning:在.resample()中如何棄用新語法為.resample(...)。ohlc()

High    Volume
open    high    low close   open    high    low close
Timestamp                               
2015-12-27 23:00:00 2045.25 2047.75 2045.25 2045.25 1.0 7.0 1.0 5.0

請問正確的命令是什么?

我很高興看到該筆記本可能不適用於所使用的Python / Pandas Im版本,但作為一個新手,它對我非常有用,因此希望它能在我的數據上正常工作。

列名稱中存在問題spaces

print (data.columns)
Index(['Timestamp', ' Open', ' High', ' Low', ' Last', ' Volume',
       ' NumberOfTrades', ' BidVolume', ' AskVolume'],
      dtype='object')

您可以strip以下空格:

data.columns = data.columns.str.strip()
print (data.columns)
Index(['Timestamp', 'Open', 'High', 'Low', 'Last', 'Volume', 'NumberOfTrades',
       'BidVolume', 'AskVolume'],
      dtype='object')

ticks = data.ix[:, ['High','Volume']]
print (ticks.head())
      High  Volume
0  2045.25       1
1  2045.50       2
2  2045.50       2
3  2045.50       2
4  2045.50       2

現在您可以使用:

print (ticks.Price.resample('1min', how='ohlc'))

如果您不想刪除空格,請在列名稱中添加空格:

print (ticks[' Price'].resample('1min', how='ohlc'))

但最好將Resampler.ohlc使用,如果pandas版本高於0.18.0

print (ticks.Price.resample('1min').ohlc())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM