-1

while i am trying to resample my dataframe in python using pandas. i am getting builtins.KerError: 'Date' when i am trying to split my index in the first dataframe.


I am very new to development and any answer with a reason why it was getting generated would be of lot of help.


this is how my dataframe looks like with multiple entries for each second, on which i wanted to do ohlc resampling.

                 Time   Token     LTP   Volume
0 2019-01-18 15:29:59  779521  294.95  9074206
0 2019-01-18 15:29:59  779521  294.95  9074206
0 2019-01-18 15:29:59  779521  294.95  9074206
0 2019-01-18 15:40:02  779521  294.95  9074723
0 2019-01-18 15:40:02  779521  294.95  9074723
0 2019-01-18 15:40:03  779521  294.95  9074725
0 2019-01-18 15:40:03  779521  294.95  9074725
0 2019-01-18 15:40:03  779521  294.95  9074725
0 2019-01-18 15:40:05  779521  294.95  9074736
0 2019-01-18 15:40:05  779521  294.95  9074736
0 2019-01-18 15:40:05  779521  294.95  9074736
0 2019-01-18 15:40:11  779521  294.95  9074986
0 2019-01-18 15:40:11  779521  294.95  9074986
0 2019-01-18 15:40:11  779521  294.95  9074986
0 2019-01-18 15:40:13  779521  294.95  9075386
0 2019-01-18 15:40:13  779521  294.95  9075386
0 2019-01-18 15:40:25  779521  294.95  9075586
0 2019-01-18 15:40:25  779521  294.95  9075586
0 2019-01-18 15:40:25  779521  294.95  9075586
0 2019-01-18 15:40:25  779521  294.95  9075586
0 2019-01-18 15:40:32  779521  294.95  9075686
0 2019-01-18 15:40:32  779521  294.95  9075686
0 2019-01-18 15:40:32  779521  294.95  9075686
0 2019-01-18 15:40:40  779521  294.95  9075687
0 2019-01-18 15:40:40  779521  294.95  9075787
0 2019-01-18 15:40:40  779521  294.95  9075787
0 2019-01-18 15:40:40  779521  294.95  9075787
0 2019-01-18 15:40:40  779521  294.95  9075787

My Code

df_cols = ["Time", "Token", "LTP", "Volume"]

data_frame = pd.DataFrame(data=[],columns=df_cols)

timeframe = '1min'

def on_ticks(ws, ticks): #retrive continius ticks in JSON format
    global data_frame, df_cols

    data = dict()

    for tick in ticks:
        Time = tick['last_trade_time']
        Token = tick['instrument_token']
        LTP = tick['last_price']
        Volume = tick['volume']

        data = [Time, Token, LTP, Volume]

    tick_df = pd.DataFrame([data], columns=df_cols)
    data_frame = data_frame.append(tick_df)

    data_frame['Time'] = pd.to_datetime(data_frame['DATE'] + ' ' + data_frame['TIME'])
    data_frame.set_index('Time', inplace=True)

i am getting the following error builtins.KerError: 'Date'

Stop harming Monica
  • 12,141
  • 1
  • 36
  • 56
K G
  • 27
  • 8

1 Answers1

0

Looks like you are calling the column "Date" from you dataframe but the dataframe does not have a column with the name.

data_frame['Time'] = pd.to_datetime(data_frame['DATE'] + ' ' + data_frame['TIME'])
ilamaaa
  • 421
  • 2
  • 8
  • Thanks, your pointing out worked. i commented out the Date code. But now i am seeing a new issue.. The data from tick_df which is getting appended into the final data frame. The date column is getting converted to NaT. Can you please solve this as well. would be of much help. The data frame after commenting out the line you mentioned. NaT 293.00 779521 9189017 NaT 293.00 779521 9189017 2019-01-21 15:11:51 292.85 779521 9202831 – K G Jan 21 '19 at 09:45
  • Nat is used to represent missing datetime values. General info https://pandas.pydata.org/pandas-docs/stable/missing_data.html here is a thread on how to filter Nats out https://stackoverflow.com/questions/23747451/filtering-all-rows-with-nat-in-a-column-in-dataframe-python – ilamaaa Jan 21 '19 at 10:36