1

I am trying to write a function to write and read transaction details to/from a .h5 file. I want to effectively use one file to store some transaction details, and when necessary, derive the details. Here's my code:

import h5py
import numpy as np
import pandas as pd

from datetime import datetime
from os import listdir
from pandas import HDFStore


def maintainLedger(mode, tick, lastBuyy = 0, lastSell = 0, quan = 0, prof = 0):
    """THIS FUNCTION WRITES AND READS TRANSACTION DETAILS.
       mode = 0 - IF FILE EXITS, READ FILE
       mode = 1 - IF FILE EXITS, APPEND TO FILE"""

    # CHECK IF LEDGER FILE EXISTS, IF NOT CREATE A LEDGER FILE FOR THE FIRST TIME
    path = r'ledger'
    suff = r'h5'
    flie = listdir(path)
    flie = [item for item in flie if item.endswith(suff)]

    if len(flie) == 0:
        HDF5Data = HDFStore('ledger/ledger.h5')

        # GENERATE NEW VALUES OF DATE/TIME
        mi = int(datetime.now().minute)
        ho = int(datetime.now().hour)
        da = int(datetime.now().day)
        we = int(datetime.now().isocalendar()[1])
        mo = int(datetime.now().month)
        ye = int(datetime.now().year)

        newwData = np.array([mode, mi, ho, da, we, mo, ye, tick, lastBuyy, lastSell, quan, prof]).reshape(1, 12)
        newwData = pd.DataFrame(newwData, columns = ['mode', 'mi', 'ho', 'da', 'we', 'mo', 'ye', 'tick', 'laBu', 'laSe', 'quan', 'prof'])
        HDF5Data.put('data', newwData, format = 'table', data_columns = True)
        HDF5Data.close()

    elif len(flie) == 1:
        if mode == 0:
            # READ PREVIOUSLY SAVED DATA AS PANDAS DATAFRAME
            readData = pd.read_hdf('ledger/ledger.h5', mode = 'r')

            # DO SOMETHING...

        elif mode == 1:
            # GENERATE NEW VALUES OF DATE/TIME
            mi = int(datetime.now().minute)
            ho = int(datetime.now().hour)
            da = int(datetime.now().day)
            we = int(datetime.now().isocalendar()[1])
            mo = int(datetime.now().month)
            ye = int(datetime.now().year)

            # GATHER NEW DATA INTO NUMPY ARRAY AND CONVERT TO PANDAS DATAFRAME
            newwData = np.array([mode, mi, ho, da, we, mo, ye, tick, lastBuyy, lastSell, quan, prof]).reshape(1, 12)
            newwData = pd.DataFrame(newwData, columns = ['mode', 'mi', 'ho', 'da', 'we', 'mo', 'ye', 'tick', 'laBu', 'laSe', 'quan', 'prof'])

            # READ PREVIOUSLY SAVED DATA AS PANDAS DATAFRAME AND APPEND NEW DATA
            readData = pd.read_hdf('ledger/ledger.h5', mode = 'a')
            readData.append('data', newwData)

            tempData = pd.read_hdf('ledger/ledger.h5', mode = 'r')
            print(tempData)

        else:
            print('Please check input data for errors!')



if __name__ == '__main__':
    maintainLedger(1, "AAPL")

When I run the code, I am getting the following error:

TypeError: cannot concatenate object of type "<class 'str'>"; only pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid

I have tried looking for a solution, and a quick search led me to this, which didn't solve my problem. Is there something I am doing wrong? Any advice would be appreciated.

troymyname00
  • 670
  • 1
  • 14
  • 32
  • Please, when you ask about an error, show where it occurs! Some readers are magicians and can guess where, but most of us need some help. – hpaulj Oct 22 '18 at 01:39
  • I don't see a `concatenate` call, but see a `readData.append` that looks like it might use that. Looking up its docs, I see: `DataFrame.append(other, ...`, where `other` is supposed to be a dataframe or series. You give a string, 'data', instead. – hpaulj Oct 22 '18 at 01:44
  • 1
    If my guess is right, then your subject line is wrong. It isn't a `HDFStore append` but a `Dataframe append` that's raising the error. – hpaulj Oct 22 '18 at 01:48
  • @hpaulj I think it's the way I am appending the data is the issue. I have found the solution the the issue. See below for updated code. – troymyname00 Oct 22 '18 at 02:04
  • @hpaulj You're correct about the DataFrame append step raising the error. – troymyname00 Oct 22 '18 at 02:07

1 Answers1

1
import h5py
import numpy as np
import pandas as pd

from datetime import datetime
from os import listdir
from pandas import HDFStore


def maintainLedger(mode, tick = 'QUERY', lastBuyy = 0, lastSell = 0, quan = 0, prof = 0):
    """THIS FUNCTION WRITES AND READS TRANSACTION DETAILS.
       mode = 0 - IF FILE EXITS, READ FILE
       mode = 1 - IF FILE EXITS, APPEND TO FILE"""

    # CHECK IF LEDGER FILE EXISTS, IF NOT CREATE A LEDGER FILE FOR THE FIRST TIME
    path = r'ledger'
    suff = r'h5'
    flie = listdir(path)
    flie = [item for item in flie if item.endswith(suff)]

    if len(flie) == 0:
        # GENERATE NEW VALUES OF DATE/TIME
        mi = int(datetime.now().minute)
        ho = int(datetime.now().hour)
        da = int(datetime.now().day)
        we = int(datetime.now().isocalendar()[1])
        mo = int(datetime.now().month)
        ye = int(datetime.now().year)

        # GATHER NEW DATA INTO NUMPY ARRAY AND CONVERT TO PANDAS DATAFRAME
        newwData = np.array([mode, mi, ho, da, we, mo, ye, tick, lastBuyy, lastSell, quan, prof]).reshape(1, 12)
        newwData = pd.DataFrame(newwData, columns = ['mode', 'mi', 'ho', 'da', 'we', 'mo', 'ye', 'tick', 'laBu', 'laSe', 'quan', 'prof'])

        # SAVE ALL DATA INTO .H5 FORMAT
        HDF5Data = HDFStore('ledger/ledger.h5')
        HDF5Data.put('data', newwData, format = 'table', data_columns = True)
        HDF5Data.close()

    elif len(flie) == 1:
        if mode == 0:
            """THIS OPTION ENABLES CODE TO READ DATA."""

            # READ PREVIOUSLY SAVED DATA AS PANDAS DATAFRAME
            readData = pd.read_hdf('ledger/ledger.h5', mode = 'r')

            # DO SOMETHING...
            print(readData)

        elif mode == 1:
            """THIS OPTION ENABLES CODE TO APPEND DATA."""

            # GENERATE NEW VALUES OF DATE/TIME
            mi = int(datetime.now().minute)
            ho = int(datetime.now().hour)
            da = int(datetime.now().day)
            we = int(datetime.now().isocalendar()[1])
            mo = int(datetime.now().month)
            ye = int(datetime.now().year)

            # GATHER NEW DATA INTO NUMPY ARRAY AND CONVERT TO PANDAS DATAFRAME
            newwData = np.array([mode, mi, ho, da, we, mo, ye, tick, lastBuyy, lastSell, quan, prof]).reshape(1, 12)
            newwData = pd.DataFrame(newwData, columns = ['mode', 'mi', 'ho', 'da', 'we', 'mo', 'ye', 'tick', 'laBu', 'laSe', 'quan', 'prof'])

            # READ PREVIOUSLY SAVED DATA AS PANDAS DATAFRAME AND APPEND NEW DATA
            readData = pd.read_hdf('ledger/ledger.h5', mode = 'r')
            readData = readData.append(newwData)

            # SAVE ALL DATA INTO .H5 FORMAT
            HDF5Data = HDFStore('ledger/ledger.h5')
            HDF5Data.put('data', readData, format = 'table', data_columns = True)
            HDF5Data.close()

        else:
            print('Please check input data for errors!')



if __name__ == '__main__':
    maintainLedger(1, 'MSFT')
troymyname00
  • 670
  • 1
  • 14
  • 32