【问题标题】:Please explain the mistake I am making in the piece of code? [closed]请解释我在这段代码中犯的错误? [关闭]
【发布时间】:2020-05-30 18:05:36
【问题描述】:

请参考以下代码:

import pandas as pd
import numpy as np
import math

class ElNinoData(object):
    # Your implementation here 
    # class variables
    add = 0
    count = 0
    average = 0

    # class methods
    def __init__(self , object):
        self.object = object

    def get_humidity():
        return self.object['humidity']

    def conversion():
        self.object = self.object.astype({'zon_winds':'float64','mer_winds':'float64','humidity':'float64','air_temp':'float64','s_s_temp':'float64'})
        return self.object

    def replace_null():
        if not math.isnan(self.object['humidity']):
            global add
            global count
            global average

            add += pd.to_numeric(self.object['humidity'])
            count += 1
            average = add / count
        else:
            self.object['humidity'] = average
        return self.object['humidity']

def get_obj_it():
    lst = ['bouy','day','latitude','longitude','zon_winds','mer_winds','humidity','air_temp','s_s_temp']
    df = pd.read_csv('C:\\Users\\suel.abbasi\\Downloads\\elnino.gz', sep='\s+', header=None)
    df.columns = lst
    df = df.replace({'.':np.nan})

    for i , j in df.iterrows():        
        yield ElNinoData(j)


def average_humidity():    
    e_n_generator = get_obj_it()

    count = 0;
    hum_sum = 0;

    for e_n_row in e_n_generator:
        e_n_row = e_n_row.conversion()
        count += 1
        hum_sum += e_n_row.get_humidity()

    print("Mean Humidity is '{}' Percent".format(hum_sum/count))
average_humidity()

代码不断抛出错误,我无法理解错误。我正在尝试实现一个类来以编程方式表示数据条目。

错误如下:

TypeError:conversion() 接受 0 个位置参数,但 1 个是

【问题讨论】:

标签: python pandas class oop


【解决方案1】:

在您的类定义函数中,您需要在定义它们时将 self 作为参数传递。所以,不要尝试def conversions():,而是尝试def conversion(self)。对 get_humidity() 和 replace_null() 执行相同操作。

【讨论】:

    猜你喜欢
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多