Task: write a function to change the timeframe on the encodings.

The body of the program:

import pandas as pd import numpy as np from future_functions import * # Load CSV Data data = pd.read_csv('Data/Hours.csv') data.columns = ['Date','open','high','low','close','AskVol'] data = data.set_index(pd.to_datetime(data.Date)) data = data[['open','high','low','close','AskVol']] prices = data.drop_duplicates(keep=False) hkaprices = prices.copy() hkaprices['Symbol'] = 'SYMB' HKA = OHLCresample(hkaprices,'15H') 

where future_functions is a connected file with a function.

The function itself:

 def OHLCresample(DataFrame,TimeFrame,column='ask'): grouped = DataFrame.groupby('Symbol') if np.any(DataFrame.columns == 'Ask'): if column == 'ask': ask = grouped['Ask'].resample(TimeFrame).ohlc() askVol = grouped['AskVol'].resample(TimeFrame).count() resampled = pd.DataFrame(ask) resampled['AskVol'] = askVol elif column == 'bid': bid = grouped['Bid'].resample(TimeFrame).ohlc() bidVol = grouped['BidVol'].resample(TimeFrame).count() resampled = pd.DataFrame(bid) resampled['BidVol'] = bidVol else: raise ValueError('Column must be a string. Either ask or bid') elif np.any(DataFrame.columns == 'close'): open = grouped['open'].resample(TimeFrame).ohlc() close = grouped['close'].resample(TimeFrame).ohlc() high = grouped['high'].resample(TimeFrame).ohlc() low = grouped['low'].resample(TimeFrame).ohlc() askVol = grouped['AskVol'].resample(TimeFrame).ohlc() resampled = pd.DataFrame(open) resampled['high'] = high resampled['low'] = low resampled['close'] = close resampled['AskVol'] = askVol resampled = resampled.dropna() return resampled 

I get the error:

KeyError: 'AskVol'

ValueError: Wrong number of items passed 4, placement implies 1

I understand that the problem is in dimension, but I do not know how to solve it.

  • I do not know how you can help without an example of data that will help reproduce the error - MaxU
  • I don’t know how to attach a file, but it looks like this: 6 columns -> Gmt time, Open, High, Low, Close, Symbol with the corresponding data -> 12.06.2018 00: 00: 00.000.1.17729.1.17736.1.17413.1.17531 , 10137.79 ........ In fact, you can take the encoding from any place - Dmitry Trifonov
  • you can specify the URL to the data ... you can put it on the file sharing service ... I also advise you to read: How to most effectively ask a question about Pandas / Numpy / SciPy / SciKit Learn / SQL / etc.? - MaxU 5:59 pm

1 answer 1

ValueError: Wrong number of items passed 4, placement implies 1 error ValueError: Wrong number of items passed 4, placement implies 1 says that you are trying to assign a four column column / DataFrame to one column.

Reproducible example:

 In [22]: df = pd.DataFrame({'a':[1,2,3], 'b':[4,5,6]}) In [23]: df Out[23]: ab 0 1 4 1 2 5 2 3 6 In [24]: new_data = np.random.randint(10, size=(3,4)) In [25]: new_data Out[25]: array([[8, 3, 6, 1], [6, 8, 5, 1], [4, 6, 9, 2]]) In [26]: new_data.shape Out[26]: (3, 4) In [27]: df['new'] = new_data ... skipped ... ValueError: Wrong number of items passed 4, placement implies 1