0

I'm trying to make a temporary dataframe that is created by filtering an existing dataframe (stock_data) based on two criteria;

  1. The stock_data ticker column is matching the tick_id variable
  2. The stock_data date column is within a range from start to end (the variables are created using pd.to_datetime)

I've attempted this using two different solutions

First:

temp = stock_data[(stock_data.ticker == tick_id) & (stock_data["date"].isin(pd.date_range(start, end)))]

Second:

mask = (stock_data.ticker == tick_id) & ((stock_data.date > start) & (stock_data.date <= end))

temp = stock_data.loc[mask]

Both solutions result in the same error:

ValueError: Can only compare identically-labeled Series objects

  • Does this help? https://stackoverflow.com/questions/18548370/pandas-can-only-compare-identically-labeled-dataframe-objects-error – Punker Jan 26 '21 at 21:15
  • 1
    Seems like your tick_id is an issue. Are you sure you've defined it correct? – Punker Jan 26 '21 at 21:16
  • @GlebV It looks like my tick_id was inaccurate upon creation, I initially made my tick_id as a way to look up an individual transactions ticker in a for loop, but the indexing on my table was incorrect. I used .reset_index() before creating my tick_id and it fixed the issue, thanks for the help – De'Roun Williams Jan 26 '21 at 21:32

1 Answers1

0

The error's telling you your tick_id Series has different labels to stock_data['ticker']. I'm guessing one is a ticker name like 'AAPL', the other is a numerical ticker-id? (or even worse, just auto-indexed 0,1,2... from the output of some previous operation)

Solution: make ticker or tick_id the index to both series/dataframes. Do this everywhere, if you can. No numerical indices. Then joins, aggregations etc. are trivially easy.

(PS this is better for mose use-cases, also makes exporting CSV or pickle more intuitive.)

Anyway, this looks like a bad attempt to do a join operation.

smci
  • 32,567
  • 20
  • 113
  • 146
  • 1
    It looks like my tick_id was inaccurate upon creation, I initially made my tick_id as a way to look up an individual transactions ticker in a for loop, but the indexing on my table was incorrect. I used .reset_index() before creating my tick_id and it fixed the issue, thanks for the help – De'Roun Williams Jan 26 '21 at 21:32