Here is a simplified version of my actual code:
<code>import pandas as pd
from typing import Dict
df = pd.DataFrame(
{"year": [2024, 2025], "my_output": [1, 2], "foo": ["a", "b"]}
)
df.set_index("year", inplace=True)
row_dict = df.to_dict("index")
ouput_dict: Dict[int, int] = {
key: val["my_output"] for key, val in row_dict.items()
}
</code>
<code>import pandas as pd
from typing import Dict
df = pd.DataFrame(
{"year": [2024, 2025], "my_output": [1, 2], "foo": ["a", "b"]}
)
df.set_index("year", inplace=True)
row_dict = df.to_dict("index")
ouput_dict: Dict[int, int] = {
key: val["my_output"] for key, val in row_dict.items()
}
</code>
import pandas as pd
from typing import Dict
df = pd.DataFrame(
{"year": [2024, 2025], "my_output": [1, 2], "foo": ["a", "b"]}
)
df.set_index("year", inplace=True)
row_dict = df.to_dict("index")
ouput_dict: Dict[int, int] = {
key: val["my_output"] for key, val in row_dict.items()
}
When I run mypy over this I get the following error:
Key expression in dictionary comprehension has incompatible type “Hashable”; expected type “int”
How can I tell mypy that the keys I set using the “year” column are integers?
Many thanks for your help.
1