0

After a lot of try and error and searching on forums (including StackOverflow Similar Question), I am finally here to ask this question. Let me quickly explain what I am trying to achieve.

I have daily data on options contracts. Data is stored in CSV format; Every day have one CSV file.

Data Format:

Ticker, Date, Time, Open, High, Low, Close, Volume, Open Interest.

Our Operation: Add data to a Dataframe side-by-side on Date and time.

How our DF should look:

---------------- contract 1 ---------- ------contract 2--------------- [...]
 
Ticker, Date, Time, Open, High,[...]  Ticker, Date, Time, Open, High,[...]

Sample code where we are performing merging operation

# we have stored our data in df. Everyday df gets updated with that day's data.
# The contains options contracts of different companies. We extract company names and ask the user to select one from that list of companies. Then we ask the user to select the start and end date. After that, we iterate for that range and merge dataframes.

raw_date=pd.read_csv("DAily Data")
while start_date < end_date:

   for con in available_options:
      df_options=raw_data[raw_data["Ticker"]=con]
      if df_merged.empty:
          df_merged=df_options
      else:
          df_merged=df_merged.merge(df_options,how="outer",on=["Date","Time"])

   start_date=start_date + day_delay(1) 


I am getting following error:

reindexing only valid with uniquely valued index objects

My questions:

  • How should we merge multiple datasets with the same columns?
  • How should I resolve the error that I am facing?
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • try pd.concat , beside having asample data in a text form helps in reproducing and providing the solution. refer to https://stackoverflow.com/help/minimal-reproducible-example – Naveed Aug 08 '22 at 15:00

1 Answers1

0

Try pd.concat(dataframe, axis=0)

for con in available_options:
      df_options=raw_data[raw_data["Ticker"]=con]
      df_merged = pd.concat(df_options, axis=0)

Pls make sure raw_data dataframe should have datetime.index

ref link- Calculating RSI using "finta", getting error

Divyank
  • 811
  • 2
  • 10
  • 26