Filtering code isn't reading data from my select box


Share a clear and concise description of the issue. Aim for 2-3 sentences.
-The code returns a blank dataframe instead of returning filtered data for the specific year selected in the selectbox.

Steps to reproduce

Code snippet:

add code here
```import streamlit as st
import pandas as pd
import yfinance as yf

data ="FB AMZN AAPL NFLX GOOGL", start="2012-01-01", end="2022-12-31")
data = data['Adj Close']
data = data.reset_index().set_index('Date', drop=False)
data = data.reset_index(drop=True)

year = st.selectbox('Year', list(reversed(range(2012,2023))))
data2 = data[data['Date'].dt.strftime('%Y') == 'year']

If applicable, please provide the steps we should take to reproduce the error or specified behavior.
- just run the above code on streamlit
**Expected behavior:**

Explain what you expect to happen when you run the code above.
-I expect the code to return filtered dataframe of the selected year.
**Actual behavior:**

Explain the undesired behavior or error you see when you run the code above.
If you're seeing an error message, share the full contents of the error message here.
-The code will run perfectly if you replace the 'year' on the second last line with a specific year eg '2018' the code will return data for 2018 but once you select the year from the selectbox the code returns blank
### Debug info

- Streamlit version: (get it with `$ streamlit version`)
- Python version: (get it with `$ python --version`)
- Using Conda? PipEnv? PyEnv? Pex?
- OS version:
- Browser version:

### Requirements file

Using Conda? PipEnv? PyEnv? Pex? Share the contents of your requirements file here.
Not sure what a requirements file is? Check out [this doc]( and add a requirements file to your app.

### Links

* Link to your GitHub repo:
* Link to your deployed app:

### Additional information

If needed, add any other context about the problem here.
if you replace the 'year' on the second last line with a specific year let's say 2020 the code will run smoothly. however i want to be selecting the year from the selectbox

Your code is looking in your dataframe for where the date is the four-letter string β€˜year’, not the value of your variable year. If you want to compare a year as a string to a year as a string the line should be:

data2 = data[data['Date'].dt.strftime('%Y') == str(year)]

Alternatively, you can work with integers:

data2 = data[data['Date'].dt.year == year]

It worked. Thank you!

1 Like