簡體   English   中英

無法將 numpy 數組分配給 pandas dataframe

[英]Can't assign a numpy array to pandas dataframe

I'm unable to assign a numpy array from a tuple of numpy arrays to a pandas dataframe. 有人能幫我嗎? 我正在使用第三方庫來獲取數據並生成 output numpy 數組。

代碼:

import time

import numpy as np
import pandas as pd

import ccxt # crypto exchange library
import tulipy as ti # financial indicators

def currentTimeMillis(self):
    return int(round(time.time() * 1000))

currentMillisMinus250min  = currentTimeMillis() - 30000000

bitmex = ccxt.bitmex()

ohlcvDf = pd.DataFrame(
            bitmex.fetch_ohlcv(symbol="BTC/USD", limit=500, timeframe="1m", since=currentMillisMinus250min),
            columns = ["timestamp", "open", "high", "low", "close", "volume"])

macdNp = ti.macd(ohlcvDf["close"].to_numpy(), 12, 26, 9)

ohlcvDf["macd"] = macdNp[0]
ohlcvDf["signal"] = macdNp[1]

錯誤:

ValueError: Length of values does not match length of index

ndarrays 元組:

(
 array([ 6.68695914e+00,  6.12108219e+00,  5.64479926e+00,  4.94431782e+00,
         4.33794071e+00,  3.81237487e+00,  3.46875952e+00,  3.15956840e+00]), 
 array([ 6.68695914,   6.57378375,   6.38798685,   6.09925304,
         5.74699058,   5.36006744,   4.98180585,   4.61735836]),
 array([ 0.00000000e+00, -4.52701560e-01, -7.43187590e-01, -1.15493523e+00,
        -1.40904987e+00, -1.54769256e+00, -1.51304634e+00, -1.45778996e+00])
)
import time

import numpy as np
import pandas as pd

import ccxt # crypto exchange library
import tulipy as ti # financial indicators

def currentTimeMillis():
    return int(round(time.time() * 1000))

currentMillisMinus250min  = currentTimeMillis() - 30000000

bitmex = ccxt.bitmex()

ohlcvDf = pd.DataFrame(
        bitmex.fetch_ohlcv(symbol="BTC/USD", limit=500, timeframe="1m", since=currentMillisMinus250min),
        columns = ["timestamp", "open", "high", "low", "close", "volume"])

macdNp = ti.macd(ohlcvDf["close"].to_numpy(), 12, 26, 9)

nan_arr = np.nan * np.ones(shape=(25,))
ohlcvDf["macd"] = np.append(nan_arr, macdNp[0])
ohlcvDf["signal"] = np.append(nan_arr, macdNp[1])

在您的代碼中,您使用了self,self.macdPeriodFast, self.macdPeriodSlow, self.macdSignal ,但沒有在任何地方引用它,因為我剛剛閱讀了這些庫並嘗試生成元組(可能與您的不同)。 但我能夠生成元組並打印這些 arrays。

您能否提供您在此特定代碼中使用的任何其他內容!

在這里閱讀了有關圖書館的信息

更好的方法是使用 np.array 而不僅僅是數組。

import numpy as np

arrays = (
 np.array([ 6.68695914e+00,  6.12108219e+00,  5.64479926e+00,  4.94431782e+00,
         4.33794071e+00,  3.81237487e+00,  3.46875952e+00,  3.15956840e+00]), 
 np.array([ 6.68695914,   6.57378375,   6.38798685,   6.09925304,
         5.74699058,   5.36006744,   4.98180585,   4.61735836]),
 np.array([ 0.00000000e+00, -4.52701560e-01, -7.43187590e-01, -1.15493523e+00,
        -1.40904987e+00, -1.54769256e+00, -1.51304634e+00, -1.45778996e+00])
)

print(arrays[0])

Output:

[6.68695914 6.12108219 5.64479926 4.94431782 4.33794071 3.81237487
 3.46875952 3.1595684 ]

暫無
暫無

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

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