简体   繁体   中英

Getting a TypeError: 'Series' objects are mutable, thus they cannot be hashed when using a function to convert a int in a column to a season

I have a data-frame called 'temperatures' that contains a month column with the values of 1 - 12 and I have written a function to take the month number and return the season for the Southern Hemisphere where the data is taken from.

def find_season(month):

    season_month = {
    12:'Summer', 1:'Summer', 2:'Summer',
    3:'Autumn', 4:'Autumn', 5:'Autumn',
    6:'Winter', 7:'Winter', 8:'Winter',
    9:'Spring', 10:'Spring', 11:'Spring'}
    return season_month.get(month)

I would like to achieve the following output

Year    Month   Season                          
1955    2       Summer
1955    3       Autumn
1955    7       Winter
1955    12      Summer

But when I go to fill the Season column using my formula

temperatures['Season'] = temperatures.apply(find_season(temperatures.Month))
OR
temperatures['Season'] = find_season(temperatures.Month)

I get the error

TypeError: 'Series' objects are mutable, thus they cannot be hashed.

I would rather use a function as I feel this code could be useful for other analyses I am doing but cannot figure out how to get around the issue I am facing.

The error is saying that the key in a dict has to be a constant, immutable object, like the strings in your example or a number.

I can't see how you would get that error from that code, so I would guess it's an example and in your real code you were doing temperatures[x] and x is a variable that points to a Series object and that's the problem; objects of type Series are not constants and therefore can't be used as keys in a dict.

Here is the solution:

def find_season(month):
    season_month = {
    12:'Summer', 1:'Summer', 2:'Summer',
    3:'Autumn', 4:'Autumn', 5:'Autumn',
    6:'Winter', 7:'Winter', 8:'Winter',
    9:'Spring', 10:'Spring', 11:'Spring'}
    return season_month.get(month)

temperatures={}
temperatures["Month"]=[1,2,3,4,5,6,7,8,9,10,11,12]
seasonlist=[]
for month in temperatures["Month"]:
    season = find_season(month)
    seasonlist.append(season)

temperatures["Season"] = seasonlist
print(temperatures)

Output:

{'Month': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 'Season': ['Summer', 'Summer', 'Autumn', 'Autumn', 'Autumn', 'Winter', 'Winter', 'Winter', 'Spring', 'Spring', 'Spring', 'Summer']}

So you have several errors in your code, one is that you are trying to get a Month property from the temperatures dictionary ( temperatures.Month ), you should refer to its key value instead: temperatures["Month"]

The other issue is that temperatures["Month"] is a list, and you are passing it to a function that takes a single number instead. You need to iterate through the array of month in order to get the season for each month.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM