1

I'm creating multi-level columns for dataframes:

lst_of_tuples = [('A', 'X'), ('A', 'Y'), ('B', 'X'), ('B', 'Y')]
columns = pd.MultiIndex.from_tuples(lst_of_tuples, names=[None, None])

Which seems to match the documenation, example below (though I'm building multi-level columns, not an index):

index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])

and I'm getting a warning from PyCharm about an expected type, specifically on the "names" variable:

Expected type 'Optional[Sequence[Hashable]]', got 'list[None]' instead

I'm getting the results I wanted, but I'd love to clear this warning up anyway. I'm not sure what type of object should be entered here, since the documentation seems clear. Is this a PyCharm bug? Thanks in advance for any insights!

emmartin
  • 105
  • 12

1 Answers1

0

create a dataframe using multiIndex.from_tubles for the columns. The columns include the one to many parent child relationship

 df = pd.DataFrame(data=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
                  columns=pd.MultiIndex.from_tuples(
                      [('a', 'b'), ('a', 'c'), ('d', 'b')],name=['first','second']))

output

 first   a     d
 second  b  c  b
 0       1  2  3
 1       4  5  6
 2       7  8  9
Golden Lion
  • 3,840
  • 2
  • 26
  • 35
  • it must be a problem with pycharm because python 3.8 works – Golden Lion Oct 29 '21 at 14:21
  • what PyCharm and the error is essentially warning you about is that the parameter has to either be None or Sequence[Hash] https://stackoverflow.com/questions/39447741/pycharm-expected-type-optionalbytes-got-str-instead – Golden Lion Oct 29 '21 at 17:09
  • https://stackoverflow.com/questions/14535730/what-does-hashable-mean-in-python Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally. Therefore, none must not be hashable – Golden Lion Oct 29 '21 at 17:12