salvationishere
salvationishere

Reputation: 3511

Python TypeError: str, int, float

I am getting the

TypeError: 'float' object is not subscriptable 

for the last line below. I do not understand why. Is this erroring on the values or the indices?

Assuming it's because of the values, I tried casting these values as float and str, but neither of these approaches fixes this error and instead I get TypeErrors of type int or str, depending on how I cast them.

I looked at several other Questions on StackOverflow with the same description, but none of these resolved my problems. Can anyone help? Am I being clear enough?

def initialize_combined_scores(self):
    sum_weights = self.dist_weight + self.coc_weight + self.optimization_weight + self.productivity_weight
    normalized_dist_weight = self.dist_weight / sum_weights
    normalized_coc_weight = self.coc_weight / sum_weights
    normalized_optimization_weight = self.optimization_weight / sum_weights
    normalized_productivity_weight = self.productivity_weight / sum_weights
    self.combined_scores = normalized_dist_weight
    self.combined_scores_df = pandas.DataFrame({'WorkerID':self.worker_ids})
    self.combined_scores_df['WorkerClusterLat'] = [xx for xx,yy in self.worker_ll]
    self.combined_scores_df['WorkerClusterLon'] = [yy for xx,yy in self.worker_ll]
    for col_name in self.worker_col_keepers:
        self.combined_scores_df[col_name] = self.worker_df[col_name]

    for pidx, patient_id in enumerate(self.patient_ids):
        self.combined_scores_df[str(patient_id)] = self.combined_scores[:, pidx]

Upvotes: 0

Views: 109

Answers (1)

Eike
Eike

Reputation: 2213

self.combined_scores might be a float. I see the following lines in your code:

normalized_dist_weight = self.dist_weight / sum_weights
...
self.combined_scores = normalized_dist_weight

The names suggest that both self.dist_weight and sum_weights could both very well be float. The result of the division would then be a float.

Edit:

The last line can only work, if self.combined_scores is a multidimensional container. A pandas.DataFrame probably.

I think the error is in:

self.combined_scores = normalized_dist_weight

Something seems to be missing here. The similarly named self.combined_scores_df is bound to a pandas.DataFrame in the line below. I believe, a line that created a pandas.DataFrame and bound it to self.combined_scores was erroneously deleted.

Upvotes: 1

Related Questions